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;
    }
  • 相关阅读:
    删除linux系统中的eth0.bak与多余的网卡 枯木
    linux下netstat详解 枯木
    世事无常中渐渐长大 枯木
    Redhat enterprise linux6.0的yum源配置 枯木
    yum的常用命令 枯木
    AWStats简介:Apache/Windows IIS的日志分析工具的下载,安装,配置样例和使用(含6.9中文定义补丁) 枯木
    shell简单管理iptables脚本 枯木
    RHEL6 下Cfengine V3 安装测试1 枯木
    存储过程事务
    C#加密方法汇总
  • 原文地址:https://www.cnblogs.com/bewolf/p/4753236.html
Copyright © 2011-2022 走看看