您的位置:首页 > 编程语言 > C语言/C++

C++实现链表的创建、插入、删除

2014-04-02 22:13 246 查看
#include <iostream>

using namespace std;

struct node

{

        int data;

        node *next;

};

class linklist 

{

private:

        node *head;

public:

        linklist();

        void create();

        void listlength();

        void addlast();

        void insert();

        void delheadnext();
void delposnext();

        void display();

        ~linklist();

};

linklist::linklist()

{

        head = new node;

        head->next = NULL;

}

void linklist::create()

{

        node *p, *q;

        p = head;

        int temp = 0;

        cin>>temp;

        

        while (temp != -1)

        {

                q = new node;

                q->data=temp;

                q->next=NULL;

                p->next = q;

                p = q;

                cin>>temp;

        }

}

void linklist::listlength()

{

        int len = 0;

        node *p = head;

        while (p->next)

        {

                len++;

                p = p->next;

        }

        cout<<endl<<"现在链表的长度是:"<<len<<endl;

}

void linklist::addlast()

{

        node *p, *q;

        int val;

        cout<<"输入一个值:"<<'\t';

        cin>>val;

        cout<<"在最后插入值val:"<<val<<endl;

        p = head;

        q = new node;

        q->data = val;

        q->next = NULL;

        while (p->next)

        {

                p = p->next;

        }

        p->next = q;

}

void linklist::insert()

{

        node *p, *q;

        int i, val, pos;

        i = 0;

        cout<<"输入val的值:"<<'\t';

        cin>>val;

        cout<<"输入pos的值:(pos不能等于尾部值)"<<'\t';

        cin>>pos;

        cout<<"在"<<pos<<"后插入值:"<<val<<endl;

        p = head;

        while(pos > 0 && i< pos) {

                while (p && i < pos)

                {

                        p = p->next;

                        ++i;

                }

                q = new node;

                q->data = val;

                q->next = p->next;

                p->next = q;

        }

}

void linklist::delheadnext()

{

        cout<<"删除头结点之后的后继结点之后:"<<endl;

        node *p;

        //p=NULL

        while(head->next) 

        {

                p=head->next;

                head->next= head->next->next;

                delete p;

                p=NULL;

        }

}

void linklist::delposnext()

{
node *p,*q;
int pos,i=0;
cout<<"输入pos的值"<<endl;
cin>>pos;
cout<<"删除"<<pos<<"位的结点"<<endl;
p=head;

while(i<pos-1)
{
p=p->next;
i++;
}
if(p->next->next)
{
q=p->next;
p->next=p->next->next;
delete q;
q=NULL;
}
else
{
q=p->next;
delete q;
q=NULL;
p->next=NULL;
}

}

void linklist::display()

{

        node *p=head;

        while (p->next)

        {

                cout<<p->next->data;

                p = p->next;

                cout<<'\t';

        }

        listlength();

        cout<<endl;

}

linklist::~linklist()

{

        node *p;

        while (head->next)

        {

                p = head;

                head = head->next;

                delete p;

                p=NULL;

        }

        delete head;

        head=NULL;

}

int main()

{

        linklist list;

        cout<<"输入链表:"<<endl;

        list.create();

        list.display();

        list.addlast();

        list.display();

        list.insert();

        list.display();

                

        list.delheadnext();

        list.display();

list.delposnext();
list.display();

        return 0;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐