双链表与双循环链表的C语言实现

双链表与双循环链表的C语言实现

双链表的增删查改

/*双链表*/

#include<stdio.h>
#include<stdlib.h>

typedef struct Node{
	int data;
	struct Node* pre;
	struct Node* next;
}Node;

Node* initList(){
	Node* node = (Node*)malloc(sizeof(Node));
	node -> data = 0;
	node -> pre = NULL;
	node -> next = NULL;
	return node;
}

void headInsert(Node* L, int data){
	Node* node = (Node*)malloc(sizeof(Node));
	node -> data = data;
	if(L -> next){				
		node -> next = L -> next;
		node -> pre = L;
		L -> next = node;
	}else{
			node -> next = L -> next;
			node -> pre = L;
			L -> next = node;
			L -> next -> pre = node;//就是原链表第二个的pre指向新插来的node,如果原来为空,这个就不需要了
	}
	L -> data ++;
}

void printList(Node*L){
	Node* node = (Node*)malloc(sizeof(Node));
	node = L -> next;
	while(node){
		printf("%d\n",node -> data);
		node = node -> next;
	}printf("\n");
}

void tailInsert(Node* L, int data){
	Node* tail = L;
	Node* node = (Node*)malloc(sizeof(Node));
	node -> data = data;
	while(tail -> next){
		tail = tail -> next;
	}
	node -> next = tail -> next;
	tail -> next = node;
	node -> pre = tail;
	L -> data ++;
}

void deleList(Node* L, int data){
	Node* node = L -> next;
	int flag = 0;
	while(node){
		if(node -> data == data){
			node -> pre -> next = node -> next;
			if(node -> next){
				node -> next -> pre = node -> pre;//如果被删的是最后一个就不存在下一个了,所以需要判断
			}
			L -> data --;
			free(node);
			printf("已经把%d从双链表里删除\n",data);
			flag = 1;
		}
		node = node -> next;
	}
	if(!flag){
		printf("没有在双链表里找到%d\n",data);
	}
}

void findList(Node*L, int data){
	int flag = 0;
	Node* node = L -> next;
	while(node){
		if(node -> data == data){
			printf("已经在双链表里找到%d\n",data);
			flag = 1;
		}
		node = node -> next;
	}
	if(!flag){
		printf("没有在双链表里找到%d\n",data);
	}
}

void changeList(Node*L, int data,int newdata){
	int flag = 0;
	Node* node = L -> next;
	while(node){
		if(node -> data == data){
			printf("已经把双链表里的%d改成%d\n",data,newdata);
			flag = 1;
		}
		node = node -> next;
	}
	if(!flag){
		printf("没有在双链表里找到%d,无法改成%d\n",data,newdata);
	}
}

int main (){
	Node* L = initList();
	headInsert(L,22);
	headInsert(L,24);
	headInsert(L,22);
	headInsert(L,22);
	tailInsert(L,33);
	tailInsert(L,34);
	tailInsert(L,33);
	tailInsert(L,36);
	printList(L);
	deleList(L,33);
	deleList(L,13);
//	
	findList(L,24);
	findList(L,240);
	changeList(L,24,11);
	changeList(L,10,34);
	printList(L);
	return 0;
}


alt text

/*双链表 复习*/

#include<stdio.h>
#include<stdlib.h>

typedef struct Node{
	int data;
	struct Node* pre;
	struct Node* next;
}Node;

Node* initList(){
	Node* node = (Node*)malloc(sizeof(Node));
	node -> data = 0;
	node -> pre = NULL;
	node -> next = NULL;
	return node;
}

void headInsert(Node* L, int data){
	Node* node = (Node*)malloc(sizeof(Node));
	node -> data = data;
	node -> next = L -> next;
	node -> pre = L;
	L -> next = node;
	if(!(L -> next)){
		L -> next -> pre = node;
	}
	L -> data ++;
}

void printList(Node* L){
	Node* node = L -> next;
	while(node){
		printf("%d\n",node -> data);
		node = node -> next;
	}
	printf("\n");
}

void tailInsert(Node* L, int data){
	Node* tail = L;
	Node* node = (Node*)malloc(sizeof(Node));
	node -> data = data;
	while(tail -> next){
		tail = tail -> next;
	}
	node -> pre = tail;
	node -> next = tail -> next;
	tail -> next = node;
}

void deleList(Node* L, int data){
	Node* node = L -> next;
	int flag = 0;
	while(node){
		if(node -> next == data){
			node -> pre -> next = node -> next;
			if(node -> next){
				node ->next -> pre = node -> pre;
			}
			free(node);
			L -> data --;
			printf("已经把%d从双链表删除\n",data);
			flag = 1;
		}
		node = node -> next;
	}
	if(!flag){
		printf("无法删除,没有找到%d\n",data);
	}
}

void findList(Node* L, int data){
	int flag = 0;
	Node* node = L -> next;
	while(node){
		if(node -> data == data){
			printf("已经在上联表里找到%d\n",data);
			flag = 1;
		}
		node = node -> next;
	}
	if(!flag){
		printf("未在双链表找到%d\n",data);
	}
}

void changeList(Node* L, int data, int newdata){
	int flag = 0;
	Node* node = L -> next;
	while(node){
		if(node -> data == data){
			node -> data = newdata;
			printf("已经将双联表里的%d更新为%d\n",data,newdata);
			flag = 1;
		}
		node = node -> next;
	}
	if(!flag){
		printf("未在双链表找到%d,无法更新为%d\n",data,newdata);
	}
}

int main(){
	Node* L = initList();
	headInsert(L,4);
	headInsert(L,3);
	headInsert(L,2);
	headInsert(L,1);
	tailInsert(L,5);
	tailInsert(L,6);
	tailInsert(L,7);
	tailInsert(L,8);
	printList(L);
	deleList(L,12);
	deleList(L,2);
	findList(L,1);
	findList(L,7);
	changeList(L,4,40);
	changeList(L,9,22);
	printList(L);
	return 0;
}

alt text

双循环链表的增删查改

alt text

void headInsert(Node* L, int data){
	Node* node = (Node*)malloc(sizeof(Node));
	node -> data = data;
	node -> next = L -> next;
	node -> pre = L;
	L -> next = node;//先改变头节点的next,后面就变成 node的pre指向node,显然不对
	L -> next -> pre = node;
	L -> data ++;
}

alt text

void headInsert(Node* L, int data){
	Node* node = (Node*)malloc(sizeof(Node));
	node -> data = data;
	node -> next = L -> next;
	node -> pre = L;
	L -> next -> pre = node;//先改变头节点的pre指向!!!
	L -> next = node;
	L -> data ++;
}
/*双循环链表*/
#include<stdio.h>
#include<stdlib.h>

typedef struct Node{
	int data;
	struct Node* pre;
	struct Node* next;
}Node;

Node* initList(){
	Node* node = (Node*)malloc(sizeof(Node));
	node -> data = 0;
	node -> pre = node;
	node -> next = node;
	return node;
}

void headInsert(Node* L, int data){
	Node* node = (Node*)malloc(sizeof(Node));
	node -> data = data;
	node -> next = L -> next;
	node -> pre = L;
	L -> next -> pre = node;
	L -> next = node;
	L -> data ++;
}



void tailInsert(Node* L, int data){
	Node* node = (Node*)malloc(sizeof(Node));
	Node* tail = L -> next;
	while(tail -> next != L){
		tail = tail -> next;
	}
	node -> data = data;
	node -> next = L;
	L -> pre = node;
	node -> pre = tail;
	tail -> next = node;
	L -> data ++;
}

void printList(Node* L){
	Node* node = L -> next;
	while(node != L){
		printf("%d\n",node -> data);
		node = node -> next;
	}
}

void deleList(Node* L, int data){
	Node* node = L -> next;
	int flag = 0;
	while(node != L){
		Node* nodenext = node -> next;//!!!
		if (node -> data == data){
			node -> next -> pre = node -> pre;
			node -> pre -> next = node -> next;
			flag = 1;
			free(node);			
			printf("已经把%d从双链表删除\n",data);
			
			L -> data --;
		}
		//		node = node->next;  // 此时 node 已被释放,访问 node->next 导致未定义行为
				node = nodenext;//!!!
	}
	if(!flag){
		printf("无法删除,没有找到%d\n",data);
		
	}
}

void findList(Node* L, int data){
	int flag = 0;
	Node* node = L -> next;
	while(node != L){
		if(node -> data == data){
			printf("已经在双联表里找到%d\n",data);
			flag = 1;
		}
		node = node -> next;
	}
	if(!flag){
		printf("未在双链表找到%d\n",data);
	}
}

void changeList(Node* L, int data, int newdata){
	int flag = 0;
	
	Node* node = L -> next;
	while(node != L){
		
		if(node -> data == data){
			node -> data = newdata;
			printf("已经将双联表里的%d更新为%d\n",data,newdata);
			flag = 1;
		}
		node = node -> next;
	}
	if(!flag){
		printf("未在双链表找到%d,无法更新为%d\n",data,newdata);
	}
}

int main(){
	Node* L = initList();
	headInsert(L,4);
	headInsert(L,3);
	headInsert(L,2);
	headInsert(L,1);
	tailInsert(L,5);
	tailInsert(L,6);
	tailInsert(L,7);
	tailInsert(L,8);
	printList(L);
	deleList(L,12);
	deleList(L,2);
	findList(L,1);
	findList(L,7);
	changeList(L,4,40);
	changeList(L,9,22);
	printList(L);
	return 0;
}

alt text

posted @ 2025-02-26 19:00  GJ504b  阅读(15)  评论(0)    收藏  举报