From 70efee39f13aa7eb3e1cfdcde3ee228cb0126f7c Mon Sep 17 00:00:00 2001 From: ViolentAyang <76544389+ViolentAyang@users.noreply.github.com> Date: Wed, 9 Mar 2022 10:10:07 +0800 Subject: [PATCH] =?UTF-8?q?Create=20=E5=B8=A6=E5=A4=B4=E7=BB=93=E7=82=B9?= =?UTF-8?q?=E5=8D=95=E9=93=BE=E8=A1=A8=E6=8C=89=E4=BD=8D=E5=BA=8F=E5=88=A0?= =?UTF-8?q?=E9=99=A4.c?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 链表/带头结点单链表按位序删除.c | 81 +++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 链表/带头结点单链表按位序删除.c diff --git a/链表/带头结点单链表按位序删除.c b/链表/带头结点单链表按位序删除.c new file mode 100644 index 0000000..b03cc1a --- /dev/null +++ b/链表/带头结点单链表按位序删除.c @@ -0,0 +1,81 @@ +#include +#include +#include + +typedef struct LNode{ + int data; + struct LNode *next; +}LNode,*LinkList; + +bool InitList(LinkList *L){ + (*L) = (LNode*)malloc(sizeof(LNode)); + if(*L==NULL){ + return false; + } + (*L)->next = NULL; + return true; +} +bool DeleteNode(LinkList *L,int i ,int *e){ + if(i<1){ + return false; + } + int j = 0; + LNode *p; + p = *L; + while(p&&jnext; + j++; + } + if(!p){ + return false; + } + if(!p->next){ + return false; + } + LNode *q = p->next; + *e = q->data; + p->next = q->next; + free(q); + return true; +} +bool InsertList(LinkList *L,int i,int e){ + if(i<1){ + return false; + } + LNode *p; + p = *L; + int j = 0; + while(p&&jnext; + j++; + } + if(!p){ + return false; + } + LNode *s = (LNode*)malloc(sizeof(LNode)); + s->data = e; + s->next = p->next; + p->next = s; + return true; +} +void PrintList(LinkList L){ + LinkList p = L->next; + while(p){ + printf("%d\n",p->data); + p = p->next; + } +} +int main(){ + LinkList L; + InitList(&L); + for(int i = 0;i < 10;i ++){ + InsertList(&L,i+1,i); + } + PrintList(L); + int m; + printf("----测试----\n"); + DeleteNode(&L,2,&m); + printf("删除的数据为%d\n",m); + PrintList(L); + return 0; +}