zoukankan      html  css  js  c++  java
  • 链表的相关操作

    #include<iostream>
    using namespace std;
    struct node
    {
        int data;
        node *next;
    };
    //链表的建立,创建有n个结点的链表
    node *create(int n)
    {
        node *head=NULL;
        node *p=NULL;
        head=new node();
        p=head;
        cin>>p->data;
        node *q;
        while(--n)
        {
            q=new node();
            cin>>q->data;
            p->next=q;
            p=q;
        }
        p->next=NULL;
        return head;
    }
    //打印结点
    void print(node *head)
    {
        node *p=head;
        while(p!=NULL)
        {
            cout<<p->data<<" ";
            p=p->next;
        }
        cout<<endl;
    }
    //插入结点
    //在指定位置weizhi上插入结点值为n的结点
    //这里注意返回值不能写成void因为当weizhi为0的时候,head的值变动了,如果不返回head虽然那么原函数中的head还在原来的位置
    node* insert(node *head,int weizhi,int n)
    {
        node *p=NULL;
        if(weizhi<0)
        {
            return 0;
        }
        if(weizhi==0)
        {
            p=new node();
            p->data=n;
            p->next=head;
            head=p;
        }
        if(weizhi>0)
        {
            p=head;
            int num=weizhi;
            while(--num)
            {
                p=p->next;
            }
            node *t=p->next;
            node *q=new node();
            q->data=n;
            p->next=q;
            q->next=t;
        }
        return head;
    }
    //删除第一个值为n的结点
    node *delete_node(node *head,int n)
    {
        if (head==NULL)
        {
            return NULL;
        }
        else if(head->data==n)
        {
            node *p=head;
            head=head->next;
            delete p;
            return head;
        }
        else 
        {
            node *p,*q;
            p=head;
            q=p->next;
            while(q!=NULL && q->data!=n)
            {
                p=p->next;
                q=q->next;
            }
            if(q==NULL)
            {
                return head;
            }
            else 
            {
                p->next=q->next;
                delete q;
                return head;
            }
        }
    }
    //链表的逆转
    node *reverse(node *head)
    {
        node *p=NULL;//指向要逆转指针的前一个结点
        node *r=head;//指向要逆转的结点
        node *q=NULL;//指向要逆转结点的后一个结点
        while(r!=NULL)
        {
            q=r->next;
            r->next=p;
            p=r;
            r=q;
        }
        head=p;     //注意全都逆转之后,结点r已经为NULL,结点p才是最后一个有数据的结点
        return head;
    }
    int main()
    {
        node *head=create(5);
        print(head);
        head=insert(head,2,100);
        print(head);
        head=insert(head,0,1000);
        print(head);
        head=delete_node(head,3);
        print(head);
        head=delete_node(head,5);
        print(head);
        head=delete_node(head,1000);
        print(head);
        head=reverse(head);
        print(head);
        return 0;
    }
  • 相关阅读:
    Luogu P2391 白雪皑皑 && BZOJ 2054: 疯狂的馒头 并查集
    Luogu P3391 文艺平衡树(Splay or FHQ Treap)
    [笔记] 平衡树合集(Treap,Splay,替罪羊,FHQ Treap)
    P1353_[USACO08JAN]跑步Running 我死了。。。
    Luogu P1436 棋盘分割 暴力DP
    Luogu P1131 [ZJOI2007]时态同步 树形DP
    Luogu P1282 多米诺骨牌 DP。。背包?
    Luogu P1273 有线电视网 树形DP
    Luogu P1272 重建道路 树形DP
    Luogu P1156 垃圾陷阱 DP
  • 原文地址:https://www.cnblogs.com/bewolf/p/4753236.html
Copyright © 2011-2022 走看看