zoukankan      html  css  js  c++  java
  • 编程菜鸟的日记-初学尝试编程-单链表

    //单链表的头插法和尾插法建立,以及直接插入法递增排序

    #include <iostream>

    #include <string>

    #include <stdlib.h>

    #define ElemType int

    using namespace std;

    typedef struct List

    {

        ElemType data;

        struct List *next;

     }List;

    void CreatListF(List *L,ElemType a[],int n)

    {

       List *s;

       L->next=NULL;

       int i;

       for(i=0;i<n;i++)

         {

            s=(List *)malloc(sizeof(struct List));

            s->data=a[i];

            s->next=L->next;

            L->next=s;

           }

    }

    void CreatListR(List *L, ElemType a[],int n)

    {

       List *r,*s;

       L->next=NULL;

       r=L;

       int i;

       for(i=0;i<n;i++)

          {

             s=(List *)malloc(sizeof(struct List));

             s->data=a[i];

             r->next=s;

             r=s;

            }

             r->next=NULL;

    }

    void SortList(List *L)

    {

         List *p=L->next,*q,*r;

         if(p!=NULL)

         {

            r=p->next;

            p->next=NULL;

            p=r;

            while(p!=NULL)

              {

                   r=p->next;

                   q=L;

                   while(q->next!=NULL && q->next->data<p->data)

                    {

                         q=q->next;

                     }

                   p->next=q->next;

                   q->next=p;

                   p=r;

               }

          }

    }

    void Display(List *L)

    {

         List *p=L->next;

          while(p!=NULL)

          {

               cout<<p->data<<" ";

                p=p->next;

            }

         cout<<endl;

    }

    int main()

    {

       List *L1,*L2;

       L1=(List *)malloc(sizeof(struct List));

       L2=(List *)malloc(sizeof(struct List));

       ElemType a[10]={7,6,1,4,9,10,5,8,2,3};

       int n=sizeof(a)/sizeof(ElemType);

       CreatListF(L1,a,n);

       Display(L1);

       CreatListR(L2,a,n);

       Display(L2);

       SortList(L1);

       Display(L1);

       SortList(L2);

       Display(L2);

       system("pause");

       return 0;
    }

     

  • 相关阅读:
    分布式任务调度 xxl-job
    【线上】 select * from xxoo where 1=1应用挂掉
    【死磕ES】七、基础检索
    【死磕ES】四、基本操作
    【死磕ES】三、基本概念
    【死磕ES】二、搭建环境
    Mac共享文件夹
    微信小程序下拉刷新,上拉加载
    微信小程序textarea输入框出现[object Object]
    微信小程序official-account的使用
  • 原文地址:https://www.cnblogs.com/lynnycy/p/3388011.html
Copyright © 2011-2022 走看看