单链表的方法函数实现-(严蔚敏版)

这学期我们学数据结构课程,这些代码是平时的练习代码。
这个是室友大神写的,写的真好看!
我感觉重点是 1 链表的逆置,不用开辟新的空间,很好用。
2 用pair类型接受两个返回值,起到确保的作用。
主要有 逆置,删偶数,输出,输入,合并非递减链表。等等

#include <bits/stdc++.h>
using namespace std;
typedef int ElemType;
typedef struct node
{
    ElemType data;
    struct node *next;
}Node,*linklist;//左面是结构体变量的名字,右边是结构体指针的新名字。
pair<linklist,bool> creatlist()    //创建操作
{  //该创建函数返回两个变量,一个是是否创建成功的头结点,一个是证明成功的bool型。
    //本质没有啥区别。只是更稳妥一点,并且需要c++对pair类型的支持。。
    pair<linklist,bool> m;
    linklist head,p,q;
    ElemType num;
    head=(linklist)malloc(sizeof(Node));
    if(head==NULL)
      {m.second=false;
       m.first=head;
       return m;
      }
    else
        m.second=true;
    p=head;
    scanf("%d",&num);
    while(num!=-1)
    {
        q=(linklist)malloc(sizeof(Node));
        q->data=num;
        p->next=q;
        p=q;
        scanf("%d",&num);
    }
    p->next=NULL;//封顶。
    m.second=true;
    m.first=head;
    return m;
}
bool  nizhi(linklist ha)/*将链表尼至*/
{//要求不能创建新的空间。如果可以的话直接来个数组存一下妥妥的。

    linklist p,q;
    p=ha->next;
    ha->next=NULL;
    while(p!=NULL)
    {
        q=p;
        p=p->next;
        q->next=ha->next;
        ha->next=q;
    }
    //haha=ha;
    return true;
}
void deleted(linklist h)    //删除结点
{
    linklist p,q;
    q=h;
    p=h->next;
    while(p!=NULL)
    {
        if((p->data%2==0))
        {q->next=p->next;
        p=q->next;}
      else
        {
            p=p->next;
            q=q->next;
        }
    }
}
bool display(linklist h)  /*输出链表*/
{
    linklist p=h->next;
    while(p!=NULL)
    {
       printf("%d ",p->data);
       p=p->next;
    }
    cout<<endl;
   return true;
}
void add(linklist h,linklist a)
{
   linklist p;
    p=h;
    while(p->next!=NULL)
    {
        if(p->next->data>=a->data)
        {
        a->next=p->next;
        p->next=a;
        break;
        }
            p=p->next;

    }
    if(p->next==NULL&&p->data<=a->data)
       {
           p->next=a;
           a->next=NULL;
       }


}
linklist creatlist_h()   //创建递增链表
{
    linklist h=(linklist)malloc(sizeof(Node));

    linklist q;
    q=(linklist)malloc(sizeof(Node));
    int x;
    cin>>x;
    q->data=x;
    h->next=q;
    q->next=NULL;
    scanf("%d",&x);
    while(x!=-1)
    {
        q=(linklist)malloc(sizeof(Node));
        q->data=x;
        add(h,q);
        scanf("%d",&x);
    }
    return h;
}
linklist MergeList()      //合并两个递增链表
{   //pair<linklist,bool> h,l;
     linklist h=creatlist_h();
    linklist l=creatlist_h();
    linklist ha,la,head,hd;
    ha=h->next;
    la=l->next;
    head=hd=h;
    while(ha&&la)
    {
        if(ha->data<=la->data)
        {
            hd->next=ha;
            hd=ha;
            ha=ha->next;
        }
        else
        {
            hd->next=la;
            hd=la;
            la=la->next;
        }
    }
    hd->next=ha?ha:la;
    free(l);
    return head;
}
void seq(linklist &h,linklist &l)   // 分离一个链表为奇偶两个链表
{   pair<linklist,bool>head;
     head=creatlist();
    linklist la,ha,hd;
    h=head.first;
    hd=head.first->next;
    l=(linklist)malloc(sizeof(Node));
    l->next=NULL;
    la=l;
    ha=h;
    while(hd!=NULL)
    {
        if(hd->data%2==0)
        {
            ha->next=hd;
            ha=hd;
            hd=hd->next;

        }
        else
        {
            la->next=hd;
            la=hd;
            hd=hd->next;
        }
    }
    ha->next=NULL;
    la->next=NULL;

}
int main()
{     int n;
       linklist head,h;
       linklist l=(linklist)malloc(sizeof(Node));
       pair<linklist,bool>haha;
    cin>>n;
       while(1)
       {


        system("pause");
        cout<<"要想关闭按crtl+z"<<endl;
    cout<<"***************功能菜单******************"<<endl;
    cout<<"创建一个无序链表按1"<<endl;
    cout<<"2删除偶数结点按2"<<endl;
    cout<<"逆置链表按3"<<endl;
    cout<<"在非递减有序链表中插入一个元素使链表元素仍有序按4"<<endl;
    cout<<"建立两个非递减有序单向链表,然后合并成一个非递增链表按5"<<endl;
    cout<<"分解链表,其中一个全部为奇数,另一个全部为偶数按6"<<endl;
    cout<<"输出按7"<<endl;
    cout<<"我不想选了88"<<endl;
    cout<<endl;
    int m;
    while(cin>>m)

    {switch(m)
    {
    case 1:cout<<"请输入链表元素,以“-1”结束"<<endl;

            haha=creatlist();
            if(!haha.second)
                cout<<"eror"<<endl;
            continue;
    case 2:deleted(haha.first);continue;
    case 3:nizhi(haha.first);continue;
    case 4:cout<<"请输入需要插入元素:"<<endl;
            cin>>l->data;
            add(haha.first,l);
            continue;
    case 5:cout<<"请输入两个链表,分别以“-1”结束:"<<endl;
            haha.first=MergeList();
            continue;
    case 6:cout<<"请输入链表元素,以“-1”结束"<<endl;
            seq(haha.first,h);
            display(haha.first);
            display(h);
            continue;
    case 7:
             display(haha.first);
            continue;
     case 8:goto modo;
    }
    }
    }
    modo:cout<<"祝您万事如意,hiahiahiahia"<<endl;
    return 0;
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值