第四周 项目三——单链表的应用(3)

来源:互联网 发布:dna条形码数据库 编辑:程序博客网 时间:2024/06/05 17:05

问题及代码:

main.cpp:

[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. /*          
  2. Copyright (c)2016,烟台大学计算机与控制工程学院          
  3. All rights reserved.          
  4. 文件名称:单链表应用(3).cpp          
  5. 作    者:陈朋   
  6. 完成日期:2016年9月22日          
  7. 版 本 号:v1.0             
  8. 问题描述:     
  9. 输入描述:无         
  10. 程序输出:若干数据。       
  11. */     
  12. #include <stdio.h>  
  13. #include <malloc.h>  
  14. #include "linklist.h"  
  15.   
  16. bool increase(LinkList *L)  
  17. {  
  18.     LinkList *p = L->next, *q;  //p指向第1个数据节点  
  19.     if(p != NULL)  
  20.     {  
  21.         while(p->next != NULL)  
  22.         {  
  23.             q = p->next;   //q是p的后继  
  24.             if (q->data > p->data)   //只要是递增的,就继续考察其后继  
  25.                 p = q;  
  26.             else  
  27.                 return false;    //只要有一个不是后继大于前驱,便不是递增  
  28.         }  
  29.     }  
  30.     return true;  
  31. }  
  32.   
  33. int main()  
  34. {  
  35.     LinkList *A, *B;  
  36.     int i;  
  37.     ElemType a[]= {1, 3, 2, 9};  
  38.     ElemType b[]= {0, 4, 5 ,6, 7, 8};  
  39.     InitList(A);  
  40.     for(i=3; i>=0; i--)  
  41.         ListInsert(A, 1, a[i]);  
  42.     InitList(B);  
  43.     for(i=5; i>=0; i--)  
  44.         ListInsert(B, 1, b[i]);  
  45.     printf("A: %c\n", increase(A)?'Y':'N');  
  46.     printf("B: %c\n", increase(B)?'Y':'N');  
  47.     DestroyList(A);  
  48.     DestroyList(B);  
  49.     return 0;  
  50. }  

LinkList.h:

[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. #ifndef LINKLIST_H_INCLUDED  
  2. #define LINKLIST_H_INCLUDED  
  3.   
  4. typedef int ElemType;  
  5. typedef struct LNode        //定义单链表结点类型  
  6. {  
  7.     ElemType data;  
  8.     struct LNode *next;     //指向后继结点  
  9. }LinkList;  
  10. void CreateListF(LinkList *&L,ElemType a[],int n);//头插法建立单链表  
  11. void CreateListR(LinkList *&L,ElemType a[],int n);//尾插法建立单链表  
  12. void InitList(LinkList *&L);  //初始化线性表  
  13. void DestroyList(LinkList *&L);  //销毁线性表  
  14. bool ListEmpty(LinkList *L);  //判断线性表是否为空  
  15. int ListLength(LinkList *L);  //求线性表长度  
  16. void DispList(LinkList *L);  //输出线性表  
  17. bool GetElem(LinkList *L,int i,ElemType &e);  //求线性表某个数据元素值  
  18. int LocateElem(LinkList *L,ElemType e);  //按元素值查找  
  19. bool ListInsert(LinkList *&L,int i,ElemType e);  //插入数据元素  
  20. bool ListDelete(LinkList *&L,int i,ElemType &e);  //删除数据元素  
  21.   
  22. #endif // LINKLIST_H_INCLUDED  
LinkList.cpp

[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. #include <stdio.h>  
  2. #include <malloc.h>  
  3. #include "linklist.h"  
  4.   
  5.   
  6. void CreateListF(LinkList *&L,ElemType a[],int n)//头插法建立单链表  
  7. {  
  8.     LinkList *s;  
  9.     int i;  
  10.     L=(LinkList *)malloc(sizeof(LinkList));     //创建头结点  
  11.     L->next=NULL;  
  12.     for (i=0; i<n; i++)  
  13.     {  
  14.         s=(LinkList *)malloc(sizeof(LinkList));//创建新结点  
  15.         s->data=a[i];  
  16.         s->next=L->next;            //将*s插在原开始结点之前,头结点之后  
  17.         L->next=s;  
  18.     }  
  19. }  
  20.   
  21. void CreateListR(LinkList *&L,ElemType a[],int n)//尾插法建立单链表  
  22. {  
  23.     LinkList *s,*r;  
  24.     int i;  
  25.     L=(LinkList *)malloc(sizeof(LinkList));     //创建头结点  
  26.     L->next=NULL;  
  27.     r=L;                    //r始终指向终端结点,开始时指向头结点  
  28.     for (i=0; i<n; i++)  
  29.     {  
  30.         s=(LinkList *)malloc(sizeof(LinkList));//创建新结点  
  31.         s->data=a[i];  
  32.         r->next=s;          //将*s插入*r之后  
  33.         r=s;  
  34.     }  
  35.     r->next=NULL;           //终端结点next域置为NULL  
  36. }  
  37.   
  38. void InitList(LinkList *&L)  
  39. {  
  40.     L=(LinkList *)malloc(sizeof(LinkList));     //创建头结点  
  41.     L->next=NULL;  
  42. }  
  43. void DestroyList(LinkList *&L)  
  44. {  
  45.     LinkList *p=L,*q=p->next;  
  46.     while (q!=NULL)  
  47.     {  
  48.         free(p);  
  49.         p=q;  
  50.         q=p->next;  
  51.     }  
  52.     free(p);    //此时q为NULL,p指向尾结点,释放它  
  53. }  
  54. bool ListEmpty(LinkList *L)  
  55. {  
  56.     return(L->next==NULL);  
  57. }  
  58. int ListLength(LinkList *L)  
  59. {  
  60.     LinkList *p=L;  
  61.     int i=0;  
  62.     while (p->next!=NULL)  
  63.     {  
  64.         i++;  
  65.         p=p->next;  
  66.     }  
  67.     return(i);  
  68. }  
  69. void DispList(LinkList *L)  
  70. {  
  71.     LinkList *p=L->next;  
  72.     while (p!=NULL)  
  73.     {  
  74.         printf("%d ",p->data);  
  75.         p=p->next;  
  76.     }  
  77.     printf("\n");  
  78. }  
  79. bool GetElem(LinkList *L,int i,ElemType &e)  
  80. {  
  81.     int j=0;  
  82.     LinkList *p=L;  
  83.     while (j<i && p!=NULL)  
  84.     {  
  85.         j++;  
  86.         p=p->next;  
  87.     }  
  88.     if (p==NULL)            //不存在第i个数据结点  
  89.         return false;  
  90.     else                    //存在第i个数据结点  
  91.     {  
  92.         e=p->data;  
  93.         return true;  
  94.     }  
  95. }  
  96. int LocateElem(LinkList *L,ElemType e)  
  97. {  
  98.     LinkList *p=L->next;  
  99.     int n=1;  
  100.     while (p!=NULL && p->data!=e)  
  101.     {  
  102.         p=p->next;  
  103.         n++;  
  104.     }  
  105.     if (p==NULL)  
  106.         return(0);  
  107.     else  
  108.         return(n);  
  109. }  
  110. bool ListInsert(LinkList *&L,int i,ElemType e)  
  111. {  
  112.     int j=0;  
  113.     LinkList *p=L,*s;  
  114.     while (j<i-1 && p!=NULL) //查找第i-1个结点  
  115.     {  
  116.         j++;  
  117.         p=p->next;  
  118.     }  
  119.     if (p==NULL)    //未找到位序为i-1的结点  
  120.         return false;  
  121.     else            //找到位序为i-1的结点*p  
  122.     {  
  123.         s=(LinkList *)malloc(sizeof(LinkList));//创建新结点*s  
  124.         s->data=e;  
  125.         s->next=p->next;                        //将*s插入到*p之后  
  126.         p->next=s;  
  127.         return true;  
  128.     }  
  129. }  
  130. bool ListDelete(LinkList *&L,int i,ElemType &e)  
  131. {  
  132.     int j=0;  
  133.     LinkList *p=L,*q;  
  134.     while (j<i-1 && p!=NULL)    //查找第i-1个结点  
  135.     {  
  136.         j++;  
  137.         p=p->next;  
  138.     }  
  139.     if (p==NULL)                //未找到位序为i-1的结点  
  140.         return false;  
  141.     else                        //找到位序为i-1的结点*p  
  142.     {  
  143.         q=p->next;              //q指向要删除的结点  
  144.         if (q==NULL)  
  145.             return false;           //若不存在第i个结点,返回false  
  146.         e=q->data;  
  147.         p->next=q->next;        //从单链表中删除*q结点  
  148.         free(q);                //释放*q结点  
  149.         return true;  
  150.     }  
  151. }  
  152. 运行结果:

0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 网贷暂时没钱还怎么办 华泰倒闭了汽车怎么办 猫躲起来找不到了怎么办 狗生病了不吃饭怎么办 猫猫托运后害怕怎么办 新来的猫害怕怎么办 升工资老板不公平对待怎么办 自酿啤酒苦味重怎么办 自酿啤酒酸味重怎么办 微信电话费充错了怎么办 支付宝电话费充错了怎么办 在淘宝上充错电话费了怎么办 话费1000充错了怎么办 东西掉在地铁上怎么办 高铁安检丢东西怎么办 东西掉成都地铁上怎么办 东西掉在成都地铁上怎么办 成都地铁上掉东西了怎么办 地铁站丢了东西怎么办 在地铁站丢了东西怎么办 没有签劳动合同不发工资怎么办 没有劳动合同辞职不给工资怎么办 地铁安检要交押金怎么办 在广州地铁上人走丢了怎么办 海尔全自动洗衣机程系乱了怎么办 河南危险化学品经营许可证怎么办 甲方不给付监理费怎么办 甲方不按合同付工程款怎么办 撞车对方全责不赔钱怎么办 电梯坏了没人修怎么办 电工超作证丢了怎么办 设计师直接找电梯厂家怎么办 研究生补助申请期限过了怎么办 我的电脑图标没了怎么办 苹果锁频密码忘了怎么办 孕妇被降职降薪怎么办 公司降职降薪员工不同意怎么办 企业因为经营不善要降薪该怎么办 调岗不降薪我该怎么办? 怀孕后强制调岗怎么办 有限公司法人变更后债务怎么办