C++链表

1,定义节点

typedef struct Node{

int data;

struct Node *next;

};

2,主函数中初始化节点

Node *p=new Node;

3,创建链表(尾插法)

void Create(Node *&p){

    int x;

    p->next=NULL;

    Node *s,*r=p;

    cin>>x;

    while(x!=-1){

        s=new Node;

        s->data=x;

        r->next=s;

        r=s;

        cin>>x;

    }

    r->next=NULL;

}

4,展示链表数据

void Print(Node *&p){

    Node *l=p->next;

    while(l!=NULL){

        cout<<l->data<<" ";

        l=l->next;

    }

    cout<<endl;

}

5,从小到大排序

void Sort(Node *&p){

    Node *l=p->next,*pre;

    Node *r=l->next;

    l->next=NULL;

    l=r;

    while(l!=NULL){

        r=l->next;

        pre=p;

        while(pre->next!=NULL && pre->next->data<l->data){

            pre=pre->next;

        }

        l->next=pre->next;

        pre->next=l;

        l=r;

    }

}

6,插入链表

void Insert_Node(Node *&p){

    Node *l=p->next;

    Node *s,*t;

    int x;

    cin>>x;

    s=new Node;

    s->data=x;

    t=l->next;

    l->next=s;

    s->next=t;

    Sort(p);

    Print(p);

}

7,清空链表

void Delete_list(Node *&l){

    Node *p=l;

    Node *q=p->next;

    while(p!=NULL){

        delete p;

        p=q;

        if(q!=NULL){

            q=q->next;

        }

    }

}

8,链表长度

int Length_list(Node *&p){

    int sum=0;

    if(p==NULL){

        return 0;

    }

    Node *l=p->next;

    while(l!=NULL){

        sum++;

        l=l->next;

    }

    return sum;

}

完整代码:

#include <iostream>

using namespace std;

//定义节点

typedef struct Node{

    int data;

    struct Node *next;

};

void Create(Node *&p){

    int x;

    p->next=NULL;

    Node *s,*r=p;

    cin>>x;

    while(x!=-1){

        s=new Node;

        s->data=x;

        r->next=s;

        r=s;

        cin>>x;

    }

    r->next=NULL;

}

void Print(Node *&p){

    Node *l=p->next;

    while(l!=NULL){

        cout<<l->data<<" ";

        l=l->next;

    }

    cout<<endl;

}

//从小到大排序

void Sort(Node *&p){

    Node *l=p->next,*pre;

    Node *r=l->next;

    l->next=NULL;

    l=r;

    while(l!=NULL){

        r=l->next;

        pre=p;

        while(pre->next!=NULL && pre->next->data<l->data){

            pre=pre->next;

        }

        l->next=pre->next;

        pre->next=l;

        l=r;

    }

}

//插入

void Insert_Node(Node *&p){

    Node *l=p->next;

    Node *s,*t;

    int x;

    cin>>x;

    s=new Node;

    s->data=x;

    t=l->next;

    l->next=s;

    s->next=t;

    Sort(p);

    Print(p);

}

//清空链表

void Delete_list(Node *&l){

    Node *p=l;

    Node *q=p->next;

    while(p!=NULL){

        delete p;

        p=q;

        if(q!=NULL){

            q=q->next;

        }

    }

}

//链表长度

int Length_list(Node *&p){

    int sum=0;

    if(p==NULL){

        return 0;

    }

    Node *l=p->next;

    while(l!=NULL){

        sum++;

        l=l->next;

    }

    return sum;

}

int main()

{

    int length;

    Node *p=new Node;

    cout<<"请输入链表中的数据:"<<endl;

    Create(p);

    Print(p);

    cout<<"链表长度:";

    length=Length_list(p);

    cout<<length<<endl;

    cout<<"从小到大排序:"<<endl;

    Sort(p);

    Print(p);

    cout<<"插入数据:"<<endl;

    Insert_Node(p);

    cout<<"清空链表"<<endl;

    Delete_list(p);

    return 0;

}

结果截图:


©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • "use strict";function _classCallCheck(e,t){if(!(e instanc...
    久些阅读 2,054评论 0 2
  • 问题:设有n个人围成一个圆圈,现从第s个人开始报数,数到第m的人出列,然后从出列的下一个人重新开始报数,数到第m的...
    Hclle阅读 562评论 0 0
  • 存储方式的分类: 顺序存储结构:静态存储,很容易找到前驱和后续元素,但必须分配最大存储空间,在插入和删除时又会浪费...
    单袍猪皮阅读 596评论 0 0
  • 本文来自本人著作《趣学数据结构》 链表是线性表的链式存储方式,逻辑上相邻的数据在计算机内的存储位置不一定相邻,那么...
    rainchxy阅读 3,787评论 6 20
  • 寒风 凛冽如刀 催残老树剩枯枝 江瘦 无奈渔翁闲 可惜小船搁滩头 迎春 争艳三九天 谁说梅花独傲娇 火棘 果红透枝...
    如果没有温暖阅读 795评论 11 25