第四周项目 third---单链表应用

来源:互联网 发布:les聊天软件 编辑:程序博客网 时间:2024/06/10 10:26
问题及代码:
/*         * 烟台大学计算机学院         * 作    者:刘泽齐       * 完成日期:2017年9月24日         * 问题描述:单链表的逆置、连接与递增判断     * 输入描述:标准函数输入         * 程序输出:标准函数输出     */  

1.

#include <stdio.h>  #include <malloc.h>  #include "linklist.h"  void Reverse(LinkList *&L)  {      LinkList *p=L->next,*q;      L->next=NULL;      while (p!=NULL)     //扫描所有的结点      {          q=p->next;      //让q指向*p结点的下一个结点          p->next=L->next;    //总是将*p结点作为第一个数据结点          L->next=p;          p=q;            //让p指向下一个结点      }  }    int main()  {      LinkList *L;      ElemType a[]= {1,3,5,7, 2,4,8,10};      CreateListR(L,a,8);      printf("L:");      DispList(L);      Reverse(L);      printf("逆置后L: ");      DispList(L);      DestroyList(L);      return 0;  }  


运行结果:

2.

#include <stdio.h>  #include <malloc.h>  #include "linklist.h"    void Link(LinkList *&L1, LinkList *&L2)  {      LinkList *p = L1;      while(p->next != NULL)   //找到L1的尾节点          p = p->next;      p->next = L2->next;  //将L2的首个数据节点连接到L1的尾节点后      free(L2);   //释放掉已经无用的L2的头节点  }    int main()  {      LinkList *A, *B;      int i;      ElemType a[]= {1,3,2,9};      ElemType b[]= {0,4,7,6,5,8};      InitList(A);      for(i=3; i>=0; i--)          ListInsert(A, 1, a[i]);      InitList(B);      for(i=5; i>=0; i--)          ListInsert(B, 1, b[i]);      Link(A, B);      printf("A:");      DispList(A);      DestroyList(A);      return 0;  }  

运行结果:

3.

#include <stdio.h>  #include <malloc.h>  #include "linklist.h"    bool increase(LinkList *L)  {      LinkList *p = L->next, *q;  //p指向第1个数据节点      if(p != NULL)      {          while(p->next != NULL)          {              q = p->next;   //q是p的后继              if (q->data > p->data)   //只要是递增的,就继续考察其后继                  p = q;              else                  return false;    //只要有一个不是后继大于前驱,便不是递增          }      }      return true;  }    int main()  {      LinkList *A, *B;      int i;      ElemType a[]= {1, 3, 2, 9};      ElemType b[]= {0, 4, 5 ,6, 7, 8};      InitList(A);      for(i=3; i>=0; i--)          ListInsert(A, 1, a[i]);      InitList(B);      for(i=5; i>=0; i--)          ListInsert(B, 1, b[i]);      printf("A: %c\n", increase(A)?'Y':'N');      printf("B: %c\n", increase(B)?'Y':'N');      DestroyList(A);      DestroyList(B);      return 0;  }  

运行结果:

原创粉丝点击