数据结构:循环双链表的基本操作(不带头结点)C语言
废话不多说!直接上代码!
#include <stdio.h>
#include <stdlib.h>#define bool char
#define true 1
#define false 0typedef int ElemType;/*[1].定义数据结构*/
// <1>定义结点的数据结构
typedef struct Node {ElemType data;struct Node *prior, *next;
}Node, *PNode;
// <2>定义链表的数据结构
typedef struct CLinkList {PNode head;PNode tail;int length;
} CLinkList; /*[2].初始化不带头结点的循环双链表*/
bool InitList(CLinkList *L) {L->head = NULL;L->tail = NULL;L->length = 0;
}/*[3].遍历*/
bool PrintList(CLinkList *L) {if (L->head == NULL && L->tail == NULL && L->length == 0) {printf("the list is null!\n");return 0;}PNode p = L->head;printf("HEADNODE-->");for (int i=1; i<=L->length; i++) {printf("%d-->", p->data);p=p->next;if (i == L->length) {printf("HEADNODE\n"); }}return 1;
}/*[5].创建新结点*/
Node *CreateNewNode(ElemType e) {Node *new_node = (Node *)malloc(sizeof(Node));if (new_node == NULL) {return 0;} new_node->data = e;new_node->next = NULL;new_node->prior = NULL;return new_node;
} /*[4].前插法建立链表*/
bool HeadInsertList(CLinkList *L) {ElemType new_node_data;printf("please input the elem(end 9999): "); scanf("%d", &new_node_data);while(new_node_data != 9999) {PNode new_node = CreateNewNode(new_node_data);if (new_node == NULL) {return 0;}if (L->head==NULL && L->tail==NULL && L->length ==0) {L->head = new_node;L->tail = new_node;L->length++;} else {new_node->next = L->head;L->head->prior = new_node;L->tail->next = new_node;new_node->prior = L->tail;L->head = new_node;L->length++;}printf("please input the elem(end 9999): "); scanf("%d", &new_node_data); }return 1;
} /*[5].尾插法建立链表*/
bool TailInsertList(CLinkList *L) {ElemType new_node_data;printf("please input the elem(end 9999): "); scanf("%d", &new_node_data);while(new_node_data != 9999) {PNode new_node = CreateNewNode(new_node_data);if (new_node == NULL) {return 0;}if (L->head==NULL && L->tail==NULL && L->length ==0) {L->head = new_node;L->tail = new_node;L->length++;} else {PNode p = L->tail;new_node->next = p->next;p->next->prior = new_node;p->next = new_node;new_node->prior = p;L->tail=new_node;L->length++;}printf("please input the elem(end 9999): "); scanf("%d", &new_node_data); }return 1;
} /*[6].按位查找*/
PNode GetElem(CLinkList *L, int i) {if (i < 1 || i > L->length) {printf("the position of i is invalid!\n");return 0;}if (L->head==NULL && L->tail==NULL && L->length ==0) {printf("the list is null!\n");return 0;}PNode p = L->head;int j=1;while(p != NULL && j < i) {p = p->next;j++;}return p;
} /*[7].按值查找*/
PNode LocateElem(CLinkList *L, ElemType e) {if (L->head==NULL && L->tail==NULL && L->length ==0) {printf("the list is null!\n");return 0;}PNode p = L->head;for (int i = 1; i <= L->length; i++) {p=p->next;if (p->data == e) {return p;}}
} /*[7].按位插入*/
bool InsertList(CLinkList *L, int i, ElemType e) {if (i < 1 || i > L->length+1) {printf("the position of i is invalid!\n");return 0;}if (L->head==NULL && L->tail==NULL && L->length ==0) {printf("the list is null!\n");return 0;} PNode new_node = CreateNewNode(e); if (new_node == NULL) {return 0;}PNode p = L->head;int j = 1;while (p != NULL && j < i-1) {p = p->next;j++;}new_node->next = p->next;p->next->prior = new_node;p->next = new_node;new_node->prior = p;L->length++;return 1;
} /*[8].按位删除*/
bool DeleteList(CLinkList *L, int i) {if (i < 1 || i > L->length) {printf("the position of i is invalid!\n");return 0;}if (L->head==NULL && L->tail==NULL && L->length ==0) {printf("the list is null!\n");return 0;} PNode p = L->head;int j = 1;while (p != NULL && j < i-1) {p = p->next;j++;}PNode q = p->next; // p指向待删除的结点q->next->prior = p;p->next = q->next;free(q);return 1;
} /*[9].求链表长度*/
int LengthList(CLinkList *L) {int len = 1;PNode p = L->head;while (p->next != L->head) {p = p->next;len++;}return len;
} /*[10].销毁链表*/
bool DestoryList(CLinkList *L) {if (L->head==NULL && L->tail==NULL && L->length ==0) {printf("the list is null!\n");return 0;} PNode p = L->head;PNode q = NULL;int i=1;while (i <= L->length) {q = p->next;free(p);p = q;i++;}free(L->tail);L->head = NULL;L->tail = NULL;L->length = 0;return 1;
} int main() {CLinkList L;InitList(&L);PrintList(&L);HeadInsertList(&L);PrintList(&L);TailInsertList(&L);PrintList(&L);PNode p = GetElem(&L, 4);printf("按位查找到值为: %d\n", p->data);PNode p1 = LocateElem(&L, 23);printf("按值查找到结果为: %d\n", p1->data);InsertList(&L, 3, 666);PrintList(&L);DeleteList(&L, 5);PrintList(&L);int len = LengthList(&L);printf("表长为:%d\n", len);DestoryList(&L);PrintList(&L);return 0;
}
至此,线性表的相关代码更新完毕!喜欢的朋友们可以点赞关注,后续会继续更新栈、队列、树、图相关数据结构代码。