• 链表转置


    #include<stdio.h>
    #include<malloc.h>
    #include<conio.h>
    #define NULL 0
    #define OVERFLOW -2
    #define LEN sizeof(Linklist)
    typedef int Number;
    typedef struct list
    {
     Number data;
     struct list *next;
    }Linklist,*Link;
    int n;

    //创建链表
    Link creat()
    {
     Link head,p1,p2;
     head=NULL;
     n=0;
     p1=p2=malloc(LEN);
     printf("Input:\n");
     scanf("%d",&p1->data);
     while(p1->data!=0)
     {
      n++;
      if(n==1)
      {
       head=p1;
      }
      else
      {
       p2->next=p1;
      }
      p2=p1;
      p1=malloc(LEN);
      scanf("%d",&p1->data);
     }
     p2->next=NULL;
     return head;
    }
    //头插法逆转单链表
    Link Reverse(Link head)
    {
     Link p,q;   //定义两个指什p,q
     p=head;   //将头指针赋值给p
     head=NULL;   //设逆转后的链表状态为空
     while(p)    //遍历需转置的链表
     {
      q=p; //头插法将值依次插入新的表中
      p=p->next;
      q->next=head;
      head=q;
     }
     return head;
    }
    //输出链表
    void print(Link head)
    {
     Link p;
     p=head;
     printf("list node:\n");
     while(p)
     {
      printf("%d\n",p->data);
      p=p->next;
     }
     printf("the end!\n");
    }

    void main()
    {
     Link head;
     int a;
     head=creat();
     getch();
     print(head);
     printf("\nenter 1 to reverse:");
     scanf("%d",&a);
     if(a==1)
     {
      head=Reverse(head);
      print(head);
     }
     getch();
    }

  • 相关阅读:
    中产阶级_百度百科
    第二天--设置一个数据模型
    请大家支持珊瑚虫~~请大家帮帮soff~~
    Linux Socket学习(十二)
    Debian下调整时间
    Emacs 牵手 scim
    Linux Socket学习(十三)
    mplayer解码
    创建第一个Symfony工程
    页面的基本创建
  • 原文地址:https://www.cnblogs.com/calm/p/1151849.html
Copyright © 2020-2023  润新知