头文件SCList,h
typedef int ElemType;
typedef struct Node {
ElemType data;
struct Node *next;
}Node, *PNode;
typedef struct List {
PNode first;
PNode last;
int size;
}List;
void InitList(Node *list);//初始化
Node *buy_node(ElemType x);//创建节点
void push_back(List *list, ElemType elem);//尾插
void push_front(List *list, ElemType elem);//头插
void show_list(List*list);//显示
void pop_back(List *list);//尾删
void pop_front(List *list);//头删
void insert_val(List *list, ElemType elem);//按值插入
Node* find_val(List *list, ElemType elem);//查找
int length(List *list);//长度
void delete_val(List *list, ElemType elem);//按值删除
void sort(List *list);//排序
void reserve(List *list);//逆序
void clear(List *list);//清除链表
void destroy(List *list);//销毁链表
源文件SCList.c(函数实现)
#include<stdio.h> #include"SCList.h" #include<assert.h> #include<malloc.h> Node *buy_node( ElemType x) { Node *s = (Node *)malloc(sizeof(Node)); assert(s != NULL); s->data = x; s->next = NULL; return s; } void InitList(List *list) { Node *s = (Node *)malloc(sizeof(Node)); assert(s != NULL); list->first = list->last = s; list->last->next = list->first;//该句表现出了循环单链表的性质 list->size = 0; } void push_back(List *list, ElemType elem) { Node *p = buy_node(elem); list->last->next = p; list->last = p; list->last->next = list->first; list->size++; } void push_front(List *list, ElemType elem) { Node *p = buy_node(elem); p->next = list->first->next; list->first->next = p; if (list->first == list->last) { list->last = p; } list->size++; } void show_list(List *list) { Node *p = list->first->next; while (p!= list->first) { printf("%d-->", p->data); p = p->next; } printf("Nul.\n"); } void pop_back(List *list) { Node *p = list->first; if (list->size == 0||list->first==list->last) return; else { while (p->next != list->last) { p = p->next; } //p->next = list->last->next; free(list->last); list->last = p; list->last->next = list->first; list->size--; } } void pop_front(List *list) { Node *p = list->first->next; if (list->size == 0) return; list->first->next = p->next; free(p); if (list->size == 1) {//不含头节点有一个数据结点,则此时last指向该唯一的数据结点 list->last = list->first; } list->size--; } void insert_val(List *list, ElemType elem) { Node *p = list->first; while (p->next != list->last&&elem >= p->next->data) {//!=list->last不是list->first p = p->next; } if(p->next == list->last&&elem >= p->next->data) push_back(list,elem); else { Node *s = buy_node(elem); s->next = p->next; p->next = s;//中间某一点插入 list->size++; } } Node* find_val(List *list, ElemType elem) { if (list->last == 0) return NULL; Node *p = list->first->next; while (p != list->first&&elem != p->data) p = p->next; if (p == list->first) return NULL; if (elem == p->data) return p; } int length(List *list) { return list->size; } void delete_val(List *list, ElemType elem) { if (list->size == 0) return; Node *p = find_val(list, elem); Node *q = p->next; if (p == NULL) { printf("要删除的数据不存在!\n"); } if (p == list->last) pop_back(elem); else { p->data = q->data; p->next = q->next; free(q); list->size--; } } void sort(List *list) { if (list->size == 0 || list->size == 1) return; Node *s = list->first->next; Node *q = s->next; list->last->next = NULL; list->last = s; list->last->next = list->first; while (q != NULL) { s = q; q = q->next; Node *p = list->first; while (p->next != list->last&&s->data >= p->next->data) { p = p->next; } if (p->next == list->last&&s->data >= p->next->data) { s->next = list->last->next; list->last->next = s; list->last = s; } else { s->next = p->next; p->next = s;//中间某一点插入 list->size++; } } } void reserve(List *list) { if(list->size == 0 || list->size == 1) return; Node *p = list->first->next; Node *q = p->next; list->last->next = NULL; list->last = p; list->last->next = list->first; while (q != NULL) { p = q; q = q->next; p->next = list->first->next; list->first->next = p; } } void clear(List *list) { Node *p = list->first->next; while (p != list->first) { list->first->next = p->next; free(p); p = list->first->next; } list->size = 0; list->last = list->first; list->last->next = list->first; } void destroy(List *list) { clear(list); free(list->first); list->first = list->last = NULL; }
测试函数Main.c
#include"SCList.h" #include<stdio.h> int main() { List mylist; ElemType elem; Node *p = NULL; InitList(&mylist); int select = 1; if (select == 0) return 0; while (select) { printf("*******************************\n"); printf("*[1] push_back***[2] push_front\n"); printf("*[3] show_list***[4] pop_back\n"); printf("*[5] pop_front***[6] insert_val\n"); printf("*[7] find_val ***[8] length\n"); printf("*[9] delete_val**[10] sort\n"); printf("*[11] resever****[12] clear\n"); printf("*[13*] destroy****[0] quit_syste\n"); printf("*******************************\n"); printf("请选择您要的操作:\n"); scanf_s("%d", &select); if (select == 0) break; switch (select) { case 1: printf("请输入你要插入的数据:\n"); while (scanf_s("%d", &elem),elem!= -1) { push_back(&mylist, elem); } break; case 2: printf("请输入要插入的数据(-1表示结束)\n"); while (scanf_s("%d", &elem), elem != -1) { push_front(&mylist, elem); } break; case 3: show_list(&mylist); break; case 4: pop_back(&mylist); break; case 5: pop_front(&mylist); break; case 6: printf("请输入要插入的数据:\n"); scanf_s("%d", &elem); insert_val(&mylist, elem); break; case 7: printf("请输入要查找的数据:\n"); scanf_s("%d", &elem); p = find_val(&mylist, elem);//返回该节点的地址 if (p == NULL) printf("要查找的数据在链表中不存在\n"); else printf("%d在循环链表中存在!\n", elem); break; case 8: printf("单链表的长度为%d:\n", length(&mylist)); break; case 9: printf("请输入要删除的值\n"); scanf_s("%d", &elem); delete_val(&mylist, elem); break; case 10: sort(&mylist); break; case 11: reserve(&mylist); break; case 12: clear(&mylist); break; default: printf("输入的命令错误,请重新输入!\n"); break; } } destroy(&mylist); }