单链表的插入和删除(C++)

要求:实现单链表的插入和删除

 

分析:熟悉链表的基本操作。

 

代码如下:

 

 

#include<iostream>

 

using namespace std;

 

struct Node{

            int data;

            Node *next;

};

 

Node *p1,*p2=new Node;

Node *Linklist=new Node;                                           //Global variable

 

int main()

{

    void InitializeNode();

            void InsertNode();

            void DeleteNode();

            InitializeNode();

            InsertNode();

            DeleteNode();

    delete p1;

    delete p2;

            delete Linklist;

            return 0;

}


void InitializeNode()

{

            int n=0,i=1;

           

            p1=p2;

            cout<<"Please input numbers,Crtl+Z to end."<<endl;

            while (cin>>n){                                                                                                           //Dymatic linklist

                        if (i==1){

                                    Linklist=p1;

                                    p2->next =p1;

                                    p1->data =n;

                                    p2=p1;

                                    p1=new Node;

                                    i++;

                        }

                        else {

                                    p2->next =p1;

                                    p1->data =n;

                                    p2=p1;

                                    p1=new Node;

                        }

            }

            p2->next =NULL;                                             //Make the "next" element of tail node NULL

           

            p1=Linklist;

           

            while(p1 ){                                                                               //Output linklist

                        cout<<p1 ->data;

                        if ((p1->next) !=NULL){

                                    cout<<"->";

                                    p1=p1->next ;

                        }

                        else {

                                    cout<<endl;

                                    return;

                        }

            }

           

}

 

void InsertNode()

{

            cin.clear();

            int pos=0,i=0,n=0;

            cout<<"Please input the position you want to insert node:"<<endl;

            cin>>pos;

            cout<<"Please input the data:"<<endl;

            cin>>n;

            p1=Linklist;

            for (i=1;i<pos;++i){                                        //Move pointer to right position

                        p1=p1->next ;

            }

            p2=new Node;                                                             //Insert node

            p2->data =n;

            p2->next =p1->next ;

            p1->next=p2;

 

            p1=Linklist;

           

            while(p1 ){                                                                               //Output linklist

                        cout<<p1 ->data;

                        if ((p1->next) !=NULL){

                                    cout<<"->";

                                    p1=p1->next ;

                        }

                        else {

                                    cout<<endl;

                                    return;

                        }

            }

 

}

 

void DeleteNode()

{

            int pos=0,i=0;

            cout<<"Please input the position of the node you want to delete:"<<endl;

            cin>>pos;

            p1=Linklist;

            for (i=1;i<pos-1;++i){                         //Move pointer to right position

                        p1=p1->next;

            }

 

            p1->next=p1->next->next;                            //Delete node

 

            p1=Linklist;

            while(p1 ){                                                                               //Output linklist

                        cout<<p1 ->data;

                        if ((p1->next) !=NULL){

                                    cout<<"->";

                                    p1=p1->next ;

                        }

                        else {

                                    cout<<endl;

                                    return;

                        }

            }

}

 

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值