• 链表的相关操作


    #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编程
    运维面试题之linux基础
    运维面试题之网络
    python 查找日志关键字
    python 数组中数字求和是否为零
    python 时间戳和时间格式互相转换
    Appium环境搭建说明(包括报错处理)
    python操作上级子文件
    去除ufeff的解决方法,python语言
  • 原文地址:https://www.cnblogs.com/bewolf/p/4753236.html
Copyright © 2020-2023  润新知