//函数声明部分:#include"CirLinkList.h" #define _CRT_SECURE_NO_WARNINGS 1 #include<stdio.h> #include<stdlib.h> typedef int ElemType; typedef struct linknode { ElemType data; struct linknode *next; }node; void judgement_NULL(node * p); node * creat_order(); void insert(node *head, int i, ElemType x); void delete(node *head, int i); void find(node *head, ElemType x); void length_list(node *head); void output(node * head); void inits_linklist(node *head); //函数实现:#include"CirLinkList.c" void judgement_NULL(node * p) //判断动态内存是否开辟成功,如果失败,程序直接结束 { if (p == NULL) { perror("out of memory\n"); exit(EXIT_FAILURE); } } node * creat_order() //创建一个循环单链表 { printf("请创建循环链表:"); node *head, *p, *r; ElemType x; head = (node *)malloc(sizeof(node)); judgement_NULL(head); r = head; head->next = NULL; while (1) { scanf("%d", &x); if (x != 0) { p = (node *)malloc(sizeof(node)); judgement_NULL(p); p->data = x; r->next = p; r = p; } else break; } r->next = head; //让最后一个结点的指针域指向头结点 printf("创建成功\n"); return head; } void insert(node *head, int i, ElemType x) //在i位置上插入一个元素x { node *p, *r, *q; p = (node *)malloc(sizeof(node)); judgement_NULL(p); p->data = x; r = head; while (1) //因为是循环链表,所以输入的位置只要i>1,就一定会有一个位置可以插入 { if (i<=1) //顺着链表循环,直到i--到1 break; r = r->next; i--; } if (i < 1) printf("没有这个结点\n"); else { q = r->next; //从头结点之后开始插入 r->next = p; p->next = q; printf("插入成功\n"); } } void delete(node *head, int i) //删除链表中i位置的结点 { node *p, *q; p = head; while (1) //循环整个链表,直到i--到1 { if (i <= 1) break; p = p->next; i--; } if (i<1) printf("没有该结点\n"); else { if (p->next== head) //如果找到要删除的结点是头结点,则跳过删除下一个结点 { p = head->next; head->next = p->next; free(p); } else { q = p->next; p->next = q->next; free(q); } printf("删除成功\n"); } } void find(node *head, ElemType x) //查找值为x的元素所在结点位置 { head = head->next; node *p; int count = 1; if (head->data == x) printf("结点位置:1"); else { p = head->next; while (p != head) { count++; if (p->data == x) break; else p = p->next; } if (p == head) printf("没有这个结点\n"); else printf("结点位置:%d\n", count); } } void length_list(node *head) //求取循环链表的结点个数 { int len = 0; if (head->next == NULL) { printf("链表为NULL\n"); } else { node *p = head->next; while (p != head) { len++; p = p->next; } printf("链表长度:%d\n", len); } } void output(node * head) //打印循环链表 { node *p; if (head == NULL) printf("链表为空\n"); else { printf("打印链表:"); p = head->next; while (p != head) { printf("%d ", p->data); p = p->next; } printf("\n"); } } void inits_linklist(node *head) //初始化链表 { node *p = head->next; free(p); head->next = NULL; printf("初始化成功\n"); } //函数测试部分:#include"CirLinkList.h" #include"CirLinkList.h" int main() { printf("*****************************************\n"); printf("*****************************************\n"); printf("**1.Creat_LinkList 2.Insert_Element **\n"); printf("**3.Find 4.Delete_Element **\n"); printf("**5.Length_LinkList 6.Output_LinkList**\n"); printf("**7.Inits_LinkList 0.Exit **\n\n\n"); node *ret = NULL; ElemType x; int i = 0; int n = 0; while (1) //循环起来,直到选择0结束 { printf("请选择功能:"); scanf("%d", &n); if (n == 0) //当选择功能时,选择0的优先级最高,无条件退出 { free(ret); exit(1); } if (n == 1 && ret == NULL) //只有当前没创建链表,而且ret为空时,n==1时,创建链表才有效 { ret = creat_order(); } else if (n != 1 && ret == NULL) printf("\n链表为NULL,请建立链表\n\n"); else if (ret != NULL) //当创建链表之后这些语句才有效 { switch (n) { case 2: printf("请输入要插入的位置和要插入的元素\n"); scanf("%d", &i); scanf("%d", &x); insert(ret,i,x); break; case 3: printf("请输入要查找的元素:"); scanf("%d", &x); find(ret,x); break; case 4: printf("请输入要删除的位置:"); scanf("%d", &i); delete(ret, i); break; case 5: length_list(ret); break; case 6: //打印链表 output(ret); break; case 7: inits_linklist(ret); ret = NULL; break; default: printf("选择无效,请重新选择\n"); break; } //switch()语句结束 } } system("pause"); return 0; }
时间: 2024-11-03 21:33:43