循环双链表(头插、尾插、删除)

循环双链表(头插、尾插、删除)

    • 题目问题描述:
    • 思路分析:
    • 结果截图:
    • 实现代码:

题目问题描述:

          循环双链表(头插、尾插、删除)。

结果截图:

循环双链表(头插、尾插、删除)_第1张图片

 

实现代码:

//循环双链表(头插、尾插、删除)
#include
#include

typedef struct Node{
	int data;
	struct Node* next;
	struct Node* pre;
}Node,*Linklist;
Node* initList();
void headInsert(Linklist L,int data);
void tailInsert(Linklist L , int data);
void printList(Linklist L);
void delx(Linklist L, int data);


int main(){
	Linklist L = initList();
	headInsert(L,1);
	headInsert(L,2);
	headInsert(L,3);
	tailInsert(L,6);
	tailInsert(L,7);
	tailInsert(L,8);
	printList(L);
	delx(L,2);
	delx(L,8);
	printf("处理后: \n");

  printList(L);


}

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

void headInsert(Linklist 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(Linklist L , int data){
	Node* node = L;
	Node* p = (Node*)malloc(sizeof(Node));
	p->data = data;
	while(node->next != L){
		node = node->next;
	}
	p->next = L;
	node->next = p;
	p->pre = node;
	L->pre = p;
	L->data++;
}

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

}

void delx(Linklist L, int data){
	Node* node = L->next;
	Node* r = NULL;
	while(node != L){
		if(node->data == data){	

				r = node->next;
				node->pre->next = r;
				r->pre = node->pre;
				free(node);
				node = r;	
			L->data--;
		}
		else{
			node = node->next;
		}
	}
}

你可能感兴趣的:(基础练习,算法,c++,图论,数据结构,链表)