From 5274e67d39119ed1a468d4156dcb4eac3ad069dd Mon Sep 17 00:00:00 2001 From: ViolentAyang <76544389+ViolentAyang@users.noreply.github.com> Date: Wed, 9 Mar 2022 10:17:46 +0800 Subject: [PATCH] =?UTF-8?q?Create=20=E6=8C=87=E5=AE=9A=E7=BB=93=E7=82=B9?= =?UTF-8?q?=E7=9A=84=E5=88=A0=E9=99=A4.c?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 链表/指定结点的删除.c | 70 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 链表/指定结点的删除.c diff --git a/链表/指定结点的删除.c b/链表/指定结点的删除.c new file mode 100644 index 0000000..7085f9b --- /dev/null +++ b/链表/指定结点的删除.c @@ -0,0 +1,70 @@ +#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(LNode *p){ + if(!p){ + return false; + } + LNode *q = p->next; + p->data = 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"); + LNode *t = L->next->next; + DeleteNode(t); + PrintList(L); + return 0; +}