C语言版本:循环单链表的实现
SClist.h
1 #ifndef __SCLIST_H__ 2 #define __SCLIST_H__ 3 4 #include<cstdio> 5 #include<malloc.h> 6 #include<assert.h> 7 typedef int ElemType; 8 typedef struct Node { 9 ElemType data; 10 struct Node *next; 11 }Node,*PNode; 12 13 typedef struct List { 14 PNode first; 15 PNode last; 16 size_t size; 17 }List; 18 19 void InitSClist(List *list);//初始化循环单链表 20 void push_back(List *list, ElemType x);//在循环单链表的末尾插入元素 21 void push_front(List *list, ElemType x);//在循环单链表的头部插入元素 22 void show_list(List *list);//打印循环单链表 23 void pop_back(List *list);//删除循环单链表的最后一个元素 24 void pop_front(List *list);//删除循环单链表的第一个元素 25 void insert_val(List *list, ElemType val);//将数据元素插入到循环单链表中(要求此时循环单链表中的数据元素顺序排列) 26 Node* find(List *list, ElemType x);//查找循环单链表中数据值为x的结点 27 int length(List *list);//求循环单链表的长度 28 void delete_val(List *list, ElemType x);//按值删除循环单链表中的某个数据元素 29 void sort(List *list);//对循环单链表进行排序 30 void reverse(List *list);//逆置循环单链表 31 void clear(List *list);//清除循环单链表 32 void destroy(List *list);//摧毁循环单链表 33 34 //优化 35 Node* _buynode(ElemType x);//创建结点 36 37 #endif
SClist.cpp
1 #include"SClist.h" 2 3 Node* _buynode(ElemType x) { 4 Node *s = (Node*)malloc(sizeof(Node)); 5 assert(s != NULL); 6 s->data = x; 7 s->next = NULL; 8 return s; 9 } 10 11 void InitSClist(List *list) { 12 Node *s = (Node*)malloc(sizeof(Node)); 13 assert(s != NULL); 14 list->first = list->last = s; 15 list->last->next = list->first;//让链表最后一个结点的指针域指向头结点,从而时链表循环 16 list->size = 0; 17 } 18 19 void push_back(List *list, ElemType x) { 20 Node *s = _buynode(x); 21 list->last->next = s; 22 list->last = s; 23 list->last->next = list->first; 24 list->size++; 25 } 26 27 void push_front(List *list, ElemType x) { 28 Node *s = _buynode(x); 29 s->next = list->first->next; 30 list->first->next = s; 31 if (list->last == list->first) 32 list->last = s; 33 list->size++; 34 } 35 36 void show_list(List *list) { 37 Node *p = list->first->next; 38 while (p != list->first) { 39 printf("%d->", p->data); 40 p = p->next; 41 } 42 printf("Nul.\n"); 43 } 44 45 void pop_back(List *list) { 46 if (list->size == 0) return; 47 Node *p = list->first; 48 while (p->next != list->last) 49 p = p->next; 50 free(list->last); 51 list->last = p; 52 list->last->next = list->first; 53 list->size--; 54 } 55 56 void pop_front(List *list) { 57 if (list->size == 0) return; 58 Node *p = list->first->next; 59 list->first->next = p->next; 60 if (list->size == 1) 61 list->last = list->first; 62 free(p); 63 list->size--; 64 } 65 66 void insert_val(List *list, ElemType x) { 67 Node *p = list->first; 68 while (p->next != list->last && p->next->data < x) 69 p = p->next; 70 if (p->next == list->last && p->next->data < x) 71 push_back(list, x); 72 else { 73 Node *s = _buynode(x); 74 s->next = p->next; 75 p->next = s; 76 list->size++; 77 } 78 } 79 80 Node* find(List *list, ElemType key) { 81 if (list->size == 0) return NULL; 82 Node *p = list->first->next; 83 while(p != list->first && p->data != key) 84 p = p->next; 85 if (p == list->first) 86 return NULL; 87 return p; 88 } 89 90 91 int length(List *list) { 92 return list->size; 93 } 94 95 void delete_val(List *list, ElemType x) { 96 if (list->size == 0) return; 97 Node *p = find(list, x); 98 if (p == NULL) { 99 printf("没有要删除的数据!\n"); 100 return; 101 } 102 if (p == list->last) 103 pop_back(list); 104 else { 105 Node *q = p->next; 106 p->data = q->data; 107 p->next = q->next; 108 free(q); 109 list->size--; 110 } 111 } 112 113 void sort(List *list) { 114 if (list->size == 0 || list->size == 1) return; 115 Node *s = list->first->next; 116 Node *q = s->next; 117 list->last->next = NULL; 118 list->last = s; 119 list->last->next = list->first; 120 while (q != NULL) { 121 s = q; 122 q = q->next; 123 Node *p = list->first; 124 while (p->next != list->last && p->next->data < s->data) 125 p = p->next; 126 if (p->next == list->last &&p->next->data < s->data) { 127 list->last->next = s; 128 list->last = s; 129 list->last->next = list->first; 130 } 131 else { 132 s->next = p->next; 133 p->next = s; 134 } 135 } 136 } 137 138 void reverse(List *list) { 139 if (list->size == 0 || list->size == 1) return; 140 Node *p = list->first->next; 141 Node *q = p->next; 142 list->last->next = NULL; 143 list->last = p; 144 list->last->next = list->first; 145 while (q != NULL) { 146 p = q; 147 q = q->next; 148 p->next = list->first->next; 149 list->first->next = p; 150 } 151 } 152 153 void clear(List *list) { 154 Node *p = list->first->next; 155 while (p != list->first) { 156 list->first->next = p->next; 157 free(p); 158 p = list->first->next; 159 } 160 list->last = list->first; 161 list->last->next = list->first; 162 list->size = 0; 163 } 164 165 void destroy(List *list) { 166 clear(list); 167 free(list->first); 168 list->first = list->last = NULL; 169 }
main.cpp
1 #include"SClist.h" 2 3 void main() { 4 List mylist; 5 InitSClist(&mylist); 6 7 ElemType item; 8 Node *p = NULL; 9 int select = 1; 10 while (select) { 11 printf("*******************************************\n"); 12 printf("*[1] push_back [2] push_front *\n"); 13 printf("*[3] show_list [4] pop_back *\n"); 14 printf("*[5] pop_front [6] insert_val *\n"); 15 printf("*[7] find [8] length *\n"); 16 printf("*[9] delete_val [10] sort *\n"); 17 printf("*[11] reverse [12] clear *\n"); 18 printf("*[13*] destroy [0] quit_system *\n"); 19 printf("*******************************************\n"); 20 printf("请选择:>>"); 21 scanf("%d", &select); 22 if (select == 0) break; 23 switch (select) { 24 case 1: 25 printf("请输入要插入的数据(-1结束):>"); 26 while (scanf("%d", &item), item != -1) { 27 push_back(&mylist, item); 28 } 29 break; 30 case 2: 31 printf("请输入要插入的数据(-1结束):>"); 32 while (scanf("%d", &item), item != -1) { 33 push_front(&mylist, item); 34 } 35 break; 36 case 3: 37 show_list(&mylist); 38 break; 39 case 4: 40 pop_back(&mylist); 41 break; 42 case 5: 43 pop_front(&mylist); 44 break; 45 case 6: 46 printf("请输入要插入的数据:>"); 47 scanf("%d", &item); 48 insert_val(&mylist, item); 49 break; 50 case 7: 51 printf("请输入要查找的数据:>"); 52 scanf("%d", &item); 53 p = find(&mylist, item); 54 if (p == NULL) 55 printf("要查找的数据在单链表中不存在!\n"); 56 break; 57 case 8: 58 printf("单链表的长度为%d\n", length(&mylist)); 59 break; 60 case 9: 61 printf("请输入要删除的值:>"); 62 scanf("%d", &item); 63 delete_val(&mylist, item); 64 break; 65 case 10: 66 sort(&mylist); 67 break; 68 case 11: 69 reverse(&mylist); 70 break; 71 case 12: 72 clear(&mylist); 73 break; 74 //case 13: 75 //destroy(&mylist); 76 //break; 77 default: 78 printf("选择错误,请重新选择!\n"); 79 break; 80 } 81 } 82 destroy(&mylist); 83 }