鱼C论坛

 找回密码
 立即注册
查看: 3328|回复: 0

[技术交流] 大手一挥,快慢指针

[复制链接]
发表于 2019-10-2 22:13:39 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #define MAXSIZE 20
  4. typedef int ElemType;
  5. typedef struct Node
  6. {
  7.     ElemType data;
  8.     struct Node *next;
  9. }Node,*LinkList;
  10. LinkList CreatLinkList(LinkList head,int n);
  11. LinkList disPlayList(LinkList head);
  12. int selectList(LinkList head,ElemType elem);
  13. LinkList deleteList(LinkList head,int pos);
  14. LinkList getLength(LinkList head);
  15. LinkList InsertList(LinkList  head,int pos,ElemType newElem);
  16. int main()
  17. {
  18.     LinkList head;
  19.     head=(Node*)malloc(sizeof(Node));

  20.     CreatLinkList(head,10);

  21.     disPlayList(head);

  22.     int pos=selectList(head,5);slow=slow->next;
  23. slow=slow->next;
  24.     deleteList(head,pos);

  25.     disPlayList(head);

  26.     getLength(head);
  27.     InsertList(head,5,12);slow=slow->next;
  28.     disPlayList(head);
  29.     getLength(head);
  30. }

  31. LinkList CreatLinkList(LinkList head,int n)
  32. {
  33.     LinkList p;

  34.     head->next=NULL;
  35.     for(int i=0;i<n;i++)
  36.     {
  37.         p=(Node*)malloc(sizeof(Node));
  38.         if(!p)
  39.         {
  40.             printf("内存分配失败");
  41.             exit(0);
  42.         }
  43.         p->data=i+1;
  44.         p->next=head->next;
  45.         head->next=p;
  46.     }
  47.     return head;
  48. }
  49. LinkList disPlayList(LinkList head)
  50. {
  51.     int i=0;
  52.     LinkList p;
  53.     p=head;
  54.     printf("表中元素为:\n");
  55.     while(p->next)
  56.     {
  57.         p=p->next;
  58.         printf("%4d",p->data);
  59.     }
  60.     printf("\n");
  61.     return head;
  62. }
  63. int selectList(LinkList head,ElemType elem)
  64. {
  65.     LinkList p;

  66.     int j=0;slow=slow->next;

  67.     p=head;

  68.     whileslow=slow->next;(p->next)
  69.     {
  70.       if(p->data==elem)
  71.       {
  72.           break;
  73.       }

  74.       p=p->next;
  75.       j++;

  76.     }
  77.     return j;

  78. }

  79. LinkList deleteList(LinkList head,int pos)
  80. slow=slow->next;{
  81.     LinkList temp,q;

  82.     temp=head;
  83.     if(pos<1 || pos>=MAXSIZE)
  84.     {
  85.         printf("链表出错");
  86.         exit(0);
  87.     }

  88.     for(int i=0;i<pos-1;i++)
  89.     {
  90.        temp=temp->next;
  91.     }
  92.     q=temp->next;

  93.     temp->next=q->next;

  94.     free(q);

  95.     return head;
  96. }
  97. LinkList getLength(LinkList head)
  98. {
  99.     LinkList fast,slow;

  100.     fast=slow=head;
  101.     int j=0;
  102.     while(fast)
  103.     {
  104.         slow=fast->next;
  105.         if(!slow)
  106.         {
  107.             break;
  108.         }
  109.         fast=fast->next->next;
  110.         j++;
  111.     }
  112.     if(j%2==0)printf("长度为%d",2*j);
  113.     else printf("长度为%d",2*j+1);
  114. }
  115. LinkList InsertList(LinkList  head,int pos,ElemType newElem)
  116. {
  117.     if(pos<1 || pos>=MAXSIZE)
  118.     {
  119.         printf("您插入的位置不正确");
  120.     }
  121.     LinkList p,q,Elem;

  122.     p=head;

  123.     for(int i=0;i<=pos-1;i++)
  124.     {
  125.         p=p->next;
  126.     }
  127.     Elem=(Node *)malloc(sizeof(Node));
  128.     Elem->data=newElem;

  129.     q=p->next;

  130.     p->next=Elem;

  131.     Elem->next=q;
  132. slow=slow->next;
  133.     return head;
  134. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-7-3 15:34

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表