From 5ab059aaeace5cd5f46c53a6151446d2a924d422 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=AF=B7=E4=B8=8D=E8=A6=81=E6=94=BE=E5=A7=9C=E5=A5=B3?= =?UTF-8?q?=E5=A3=AB?= <3483447616@qq.com> Date: Fri, 22 Dec 2023 06:32:26 +0000 Subject: [PATCH] add 2224020139/chapter2/exp2-2. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 请不要放姜女士 <3483447616@qq.com> --- 2224020139/chapter2/exp2-2 | 159 +++++++++++++++++++++++++++++++++++++ 1 file changed, 159 insertions(+) create mode 100644 2224020139/chapter2/exp2-2 diff --git a/2224020139/chapter2/exp2-2 b/2224020139/chapter2/exp2-2 new file mode 100644 index 00000000..8c55002a --- /dev/null +++ b/2224020139/chapter2/exp2-2 @@ -0,0 +1,159 @@ +#include +#include +typedef char ElemType; +typedef struct LNode +{ + ElemType data; + struct LNode *next; +}LinkNode; +void InitList(LinkNode *&L)//初始化单链表 1) +{ + L=(LinkNode *)malloc(sizeof(LinkNode)); + L->next=NULL; +} +bool ListInsert(LinkNode *&L,int i,ElemType e)//插入数据元素 2) +{ + int j=0; + LinkNode *p=L,*s; + if(i<=0) + return false; + while(jnext; + } + if(p==NULL) + return false; + else + { + s=(LinkNode *)malloc(sizeof(LinkNode)); + s->data=e; + s->next=p->next; + p->next=s; + return true; + } +} +void DispList(LinkNode *L)//输出线性表 3) 9) 11) +{ + LinkNode *p=L->next; + while(p!=NULL) + { + printf("%c",p->data); + p=p->next; + } + printf("\n"); +} +int ListLength(LinkNode *L)//求线性表的长度 4) +{ + int n=0; + LinkNode *p=L; + while (p->next!=NULL) + { + n++; + p=p->next; + } + return (n); +} +bool ListEmpty(LinkNode *L)//判断单链表是否为空5) +{ + return (L->next==NULL); +} +bool GetElem(LinkNode *L,int i,ElemType &e)//某个数据元素值6) +{ + int j=0; + LinkNode *p=L; + if(i<=0) + return false; + while(jnext; + } + if(p==NULL) + return false; + else + { + e=p->data; + return true; + } +} +int LocateElem(LinkNode *L,ElemType e)//元素的位置7) +{ + int i=1; + LinkNode *p=L->next; + while(p!=NULL&&p->data!=e) + { + p=p->next; + i++; + } + if(p==NULL) + return (0); + else + return (i); +} +bool ListDelete(LinkNode *&L,int i,ElemType &e) +{ + int j=0; + LinkNode *p=L,*q; + if(i<=0) + return false; + while(jnext; + } + if(p==NULL) + return false; + else + { + q=p->next; + if(q==NULL) + return false; + e=q->data; + p->next=q->next; + free(q); + return true; + } +} +void DestroyList(LinkNode *&L)//释放单链表12) +{ + LinkNode *pre=L,*p=L->next; + while(p!=NULL) + { + free(pre); + pre=p; + p=pre->next; + } + free(pre); +} +int main() +{ + LinkNode *h; + ElemType e; + printf("单链表的基本运算如下:\n"); + printf("(1)初始化单链表h.\n"); + InitList(h); + printf("(2)依次采用尾插法插入a,b,c,d,e元素.\n"); + ListInsert(h,1,'a'); + ListInsert(h,2,'b'); + ListInsert(h,3,'c'); + ListInsert(h,4,'d'); + ListInsert(h,5,'e'); + printf("(3)输出单链表h:"); + DispList(h); + printf("(4)输出单链表h的长度:%d\n",ListLength(h)); + printf("(5)判断单链表h是否为空:%s\n",(ListEmpty(h)?"空":"非空")); + GetElem(h,3,e); + printf("(6)输出单链表h的第3个元素:%c\n",e); + printf("(7)输出元素a的位置:%d\n",LocateElem(h,'a')); + printf("(8)在第4个元素位置上插入f元素.\n"); + ListInsert(h,4,'f'); + printf("(9)输出单链表h:"); + DispList(h); + printf("(10)删除单链表h的第3个元素.\n"); + ListDelete(h,3,e); + printf("(11)输出单链表h:"); + DispList(h); + printf("(12)释放单链表h\n"); + DestroyList(h); +} -- Gitee