• 复习一下单链表的常用操作


    复习一下单链表的常用操作,包括单链表的创建、插入、删除、排序、逆置以及打印输出等。

    #include<IOSTREAM>
    using namespace std;
    
    typedef struct Single_link
    {
        int data;
        struct Single_link *next;
    }node;
    
    //单链表的创建
    node *Create()
    {
        node *head,*p,*s;
        int x,cycle=1;
    
        head=(node *)malloc(sizeof(node));
        p=head;
    
        cout<<"Input the elements of the link:"<<endl;
        while (cycle)
        {
            
            cin>>x;
    
            if (x!=0)
            {
                s=(node *)malloc(sizeof(node));
                s->data=x;
                
                p->next=s;
                p=s;
            }
            else
                cycle=0;
        }
        head=head->next;
        p->next=NULL;
    
        return head;
    }
    //单链表测长
    int length(node *head)
    {
        node *p;
        p=head;
        int count=0;
        while (p!=NULL)
        {
            p=p->next;
            count++;
        }
        return count;
    }
    //////////////////////////////////单链表插入///////////////////////////////////////
    node *Insert(node *head,int element)
    {
        //p是遍历指针,q用于保存插入节点的位置节点,s是将要插入的节点
        node *p,*q,*s;
        p=head;
    
        //创建插入的新节点
        s=(node *)malloc(sizeof(node));
        s->data=element;
    
        while (s->data>p->data && p->next!=NULL)
        {
            q=p;
            p=p->next;
        }
        if (s->data<=p->data)
        {
            if (head==p)
            {
                s->next=p;
                head=s;
            }
            else
            {
                q->next=s;
                s->next=p;
            }
        }
        else
        {
            p->next=s;
            s->next=NULL;
        }
    
        return head;
    }
    /////////////////////////////单链表删除/////////////////////////////////////////////
    node *del(node *head,int element)
    {
        //p是遍历指针,q用于保存删除的位置节点
        node *p,*q;
        p=head;
        
        while (p->data!=element && p->next!=NULL)
        {
            q=p;
            p=p->next;
        }
        if (p->data==element)
        {
            if (head==p)
            {
                head=p->next;
                free(p);
            }
            else
            {
                q->next=p->next;
            }
        }
        else
        {
            cout<<" Not find the delete element."<<endl;
        }
    
        return head;
    }
    ///////////////////////////////单链表逆序///////////////////////////////////////////
    node *Reverse(node *head)
    {
        node *p,*q,*s;
    
        if (head==NULL && head->next==NULL)
        {
            return head;
        }
        p=head;
        q=p->next;
    
        while (q)
        {
            s=q->next;
            q->next=p;
            p=q;
            q=s;
        }
        head->next=NULL;
        head=p;
    
        return head;
    }
    
    ///////////////////////////////单链表排序///////////////////////////////////////////
    node *Sort(node *head)
    {
        node *p;
        int n;
        int temp;
        n=length(head);
    
        if(head==NULL && head->next==NULL)
        {
            return head;
        }
    
        p=head;
        for (int j=1;j<n;j++)
        {
            p=head;
            for (int i=0;i<n-j;i++)
            {
                if (p->data > p->next->data)
                {
                    temp=p->data;
                    p->data=p->next->data;
                    p->next->data=temp;
                }
                p=p->next;
            }
        }
    
        return head;
    }
    ////////////////////////合并两个单链表(类似于归并排序)/////////////////////////////////////////////////
    node *MergeLink(node *head1,node *head2)
    {
        if (head1==NULL)
        {
            return head2;
        }
        if (head2==NULL)
        {
            return head1;
        }
    
        node *head;
        head = NULL;
        if (head1->data<head2->data)
        {
            head=head1;
            head1=head1->next;
        }
        else
        {
            head=head2;
            head1=head2->next;
        }
    
        node *p=head;
        while(head1 && head2)
        {
            if (head1->data<head2->data)
            {
                p=head1;
                head1=head1->next;
            }
            else
            {
                p=head2;
                head1=head2->next;
            }
        }
        if (head1)
        {
            p->next=head1;
        }
        if (head2)
        {
            p->next=head2;
        }
        return head;
    }
    
    
    ////////////////////////打印单链表///////////////////////////////////////////////////////////
    void print(node *head)
    {
        node *p;
        p=head;
    
        int n=length(head);
        cout<<"链表中共有"<<n<<"记录."<<endl;
    
        if (head!=NULL)
        {
            cout<<"链表中元素为:"<<endl;
            while (p!=NULL)
            {
                cout<<p->data<<" ";
                p=p->next;
            }
            cout<<endl;
        }
    }
    int main()
    {
        node *head;
        int n;
    
        head=Create();
        print(head);
    
        head=Sort(head);
        print(head);
    
        cout<<"Input the insert number:"<<endl;
        cin>>n;
        Insert(head,n);
        print(head);
    
        cout<<"Input the delete number:"<<endl;
        cin>>n;
        del(head,n);
        print(head);
    
        head=Reverse(head);
        print(head);
    
        return 0;
    }

     

  • 相关阅读:
    POJ 1936 All in All
    Blue Jeans POJ 3080 寻找多个串的最长相同子串
    Spell checker POJ 1035 字符串
    密码锁
    luogu P1083 借教室
    BZOJ 1588: [HNOI2002]营业额统计
    BZOJ 1433: [ZJOI2009]假期的宿舍
    luogu P1231 教辅的组成
    luogu P2756 飞行员配对方案问题
    luogu P3386 【模板】二分图匹配
  • 原文地址:https://www.cnblogs.com/yvictoryr/p/3725205.html
Copyright © 2020-2023  润新知