From 8ffc6b0c24a6ed47204a1474269165d3c2c3fa79 Mon Sep 17 00:00:00 2001 From: xtaodada Date: Wed, 16 Mar 2022 23:42:04 -0400 Subject: [PATCH] =?UTF-8?q?=E4=B8=8A=E4=BC=A0=E6=96=87=E4=BB=B6=E8=87=B3?= =?UTF-8?q?=20''?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ex2_2.c | 247 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 247 insertions(+) create mode 100644 ex2_2.c diff --git a/ex2_2.c b/ex2_2.c new file mode 100644 index 0000000..b652304 --- /dev/null +++ b/ex2_2.c @@ -0,0 +1,247 @@ +#include +#include +#include +#define MAX 100 +typedef int DataType; +typedef struct node{ + DataType data; + struct node *next; +}Node; +void CreateListHead(Node *L, int n){ + Node *p; + L->next = NULL; + for(int i = 0; i < n; i++){ + // 创建结点 p + p = (Node *)malloc(sizeof(Node)); + printf("请输入第 %d 个元素:",i+1); + scanf("%d",&(p->data)); + // 插入 + p->next = L->next; + L->next = p; + } +}; +void CreateListEnd(Node *L, int n){ + Node *p,*end=L; + for(int i=0;idata)); + // 插入 + end->next = p; + end = p; + } + end->next = NULL; +}; +void insertList(Node *L, int n, DataType e){ + Node *p=L; + // 首先找到要插入位置的上一个结点 + for (int i=1; inext; + } + // 创建插入结点 new + Node *new=(Node*)malloc(sizeof(Node)); + new->data=e; + // 向链表中插入结点 + new->next=p->next; + p->next=new; +}; +void EditList(Node *L, int n){ + Node *p=L->next; + DataType e; + // 首先找到要修改的位置 + for (int i=1; inext; + } + printf("请输入修改后的值:"); + scanf("%d",&e); + p->data=e; + printf("修改完成。\n"); +} +int FindList(Node *L, DataType e){ + Node *p = L->next; + int i=0; + while(p != NULL){ + if(p->data == e){ + return i; + } + i++; + p = p->next; + } + return -1; +}; +int DeleteList1(Node *L, DataType e){ + Node *p = L->next, *last = L; + while(p != NULL){ + if(p->data == e){ + // 删除 + last->next = p->next; + // 释放空间 + free(p); + return 1; + } + last = last->next; + p = p->next; + } + return 0; +} +int DeleteList2(Node *L, int n){ + Node *p = L->next, *last = L; + int i=0; + while(p != NULL){ + if(i == n){ + // 删除 + last->next = p->next; + // 释放空间 + free(p); + return 1; + } + last = last->next; + p = p->next; + i++; + } + return 0; +} +void DeleteList3(Node *L, DataType start, DataType end){ + // 确认 start <= end + int temp; + if(start>end){ + temp=end; + end=start; + start=temp; + } + Node *p = L->next, *last = L, *temp_; + while(p != NULL){ + if(p->data < start){ + last = last->next; + p = p->next; + } + if(p->data >= start || p->data <= end){ + last->next = NULL; + // 释放空间 + temp_=p; + p = p->next; + free(temp_); + } + if(p->data > end){ + // 改变指针 + last->next = p; + p = p->next; + } + } +}; +void OutputList(Node *L){ + Node *p = L->next; + while(p != NULL){ + printf("%d\n", p->data); + p = p->next; + } +}; +int main(){ + Node *L1; + L1 = (Node *)malloc(sizeof(Node)); + int n,choice,x,y; + DataType e; + + for(;;){ + printf("\t\t1.头插法输入链表\n"); + printf("\t\t2.尾插法输入链表\n"); + printf("\t\t3.在链表中插入元素\n"); + printf("\t\t4.查找元素是否存在于单链表\n"); + printf("\t\t5.删除指定值的元素\n"); + printf("\t\t6.删除指定位置的元素\n"); + printf("\t\t7.删除位于区间的元素\n"); + printf("\t\t8.修改指定位置的元素\n"); + printf("\t\t9.输出线性表\n"); + printf("\t\t0.退出\n"); + + printf("请选择:"); + scanf("%d",&choice); + if(choice==0) break; + switch (choice) { + case 1: + printf("请输入链表长度:"); + scanf("%d",&n); + CreateListHead(L1, n); + OutputList(L1); + break; + case 2: + printf("请输入链表长度:"); + scanf("%d",&n); + CreateListEnd(L1, n); + OutputList(L1); + break; + case 3: + printf("请输入需要插入的位置:"); + scanf("%d",&n); + printf("请输入需要插入的元素:"); + scanf("%d",&e); + insertList(L1,n,e); + OutputList(L1); + break; + case 4: + printf("请输入需要查找的元素:"); + scanf("%d",&e); + n=FindList(L1,e); + if(n>-1){ + printf("该元素在 %d\n",n); + } else { + printf("未找到该元素\n"); + } + break; + case 5: + printf("请输入需要删除的元素的值:"); + scanf("%d",&e); + n=DeleteList1(L1,e); + if(n==0){ + printf("删除失败,元素不存在。\n"); + } else { + printf("删除成功\n"); + } + OutputList(L1); + break; + case 6: + printf("请输入需要删除的元素的位置:"); + scanf("%d",&n); + n=DeleteList2(L1,n-1); + if(n==0){ + printf("删除失败,位置不存在。\n"); + } else { + printf("删除成功\n"); + } + OutputList(L1); + break; + case 7: + printf("请输入需要删除的元素值区间 x y:"); + scanf("%d %d",&x,&y); + DeleteList3(L1,x,y); + printf("操作成功\n"); + OutputList(L1); + break; + case 8: + printf("请输入需要修改的元素的位置:"); + scanf("%d",&n); + EditList(L1,n); + OutputList(L1); + break; + case 9: + OutputList(L1); + break; + default: + break; + } + printf("请按任意键继续..."); + getch(); + system("cls"); + } + printf("感谢使用,再见\n"); + return 0; +};