C++编写链表实现插入和删除等操作

【问题描述】

编写链表实现插入和删除等操作;
根据所给主函数以及输入输出格式完整代码

【样例输入输出格式】

请输入学生姓名:
zhang
wang
li
zhao
sun
qian
学生信息为:
2018001 li
2018002 wang
2018003 zhang
2018004 zhao
2018005 sun
2018006 qian
请输入要删除的学生学号:
2018004
学生信息为:
2018001 li
2018002 wang
2018003 zhang
2018005 sun
2018006 qian
请输入要插入的学生学号和姓名:
2018004 han
学生信息为:
2018001 li
2018002 wang
2018003 zhang
2018004 han
2018005 sun
2018006 qian

题目给出主函数为:

int  main()
{
    int  ID;
    LinkList  list1;
    string  name;
    cout  <<  "请输入学生姓名:"  <<  endl;
    for  (int  i=2018003;  i>=2018001;i--)            //依次输入名字
    {
        cin  >>  name;
        list1.addNodeAtHead(i,name);
    }
    for  (int  i  =  2018004;  i  <=  2018006;  i++)            //依次输入名字
    {
        cin  >>  name;
        list1.addNodeAtTail(i,name);
    }
    cout  <<"学生信息为:"<<  endl;
    list1.print();
    cout  <<  "请输入要删除的学生学号:"<<endl;
    cin  >>  ID;
    cout  <<  "学生信息为:"  <<  endl;
    list1.deleteNode(ID);
    list1.print();
    cout  <<  "请输入要插入的学生学号和姓名:"<<endl;
    cin  >>  ID  >>name;
    cout  <<  "学生信息为:"  <<  endl;
    list1.insertNode(ID,  name);
    list1.print();
    return  0;
}

最终总程序为:

#include<iostream>
#include<string>
using namespace std;
class Node
{
public:
    Node(int id,string name);//定义编号和姓名
    void showMessage();//输出数据
    friend class LinkList;//把LinkList作为自己的友元类
protected:
    int mId;//编号
    string mName;//姓名
    Node *next;//指针指向下一个
};
Node::Node(int id,string name) //接受编号和姓名
{
    this->mId = id;
    this->mName = name;
    this->next = NULL;
}
void Node::showMessage() //输出数据
{
    cout<<this->mId<<" "<<this->mName<<endl;
}
class LinkList
{
public:
    LinkList();
    void addNodeAtHead(int id,string name);
    void addNodeAtTail(int id,string name);
    void insertNode(int id,string name);
    void deleteNode(int id);
    void print();
protected:
    Node *head;//指针指向头
};
LinkList::LinkList()
{
    head = NULL;
}
void LinkList::addNodeAtHead(int id,string name) //头插入
{
    Node *p = new Node(id,name);
    if(head==NULL) //当链表为空时
      head=p;
    else
    {
        p->next = head;
        head = p;
    }
}
void LinkList::addNodeAtTail(int id,string name) //尾插入
{
    Node *p = new Node(id,name);
    if(head==NULL) //当链表为空时
      head=p;
    else
    {
        Node *q = head;
        while(q->next!=NULL)
            q = q->next;
        q->next = p;
    }
}
void LinkList::insertNode(int id,string name) //按ID顺序插入
{
    Node *p = new Node(id,name);
    if(head==NULL) //当链表为空时
        head=p;
    else
    {
        Node *q = head,*r = head;
        while(q && q->mId<p->mId)
        {
            r = q;
            q = q->next;
        }
        if(q!=NULL)
        {
            p->next = q;
            r->next = p;
        }
        else
        {
            p->next = NULL;
            r->next = p;
        }
    }
}
void LinkList::deleteNode(int id) //删除某个ID的信息
{
    if(head==NULL)
    {
        cout<<"链表为空,不能删除"<<endl;
    }
    else if(head->mId==id)
    {
        head=head->next;
    }
    else
    {
        int flag=1;
        while(flag)   //保证删除链表中所有值为item的数据
        {
            Node *p=head;
            Node *q;
            while(p&&p->mId!=id)
            {
                q=p;
                p=p->next;
            }
        if(p) //在链表中找到该元素
        q->next=p->next;
        else
            flag=0;
        }
    }
}
void LinkList::print() //打印该链表的基本信息
{
    Node *p;
    p=head;
    if(head==NULL)
    {
        cout<<"链表为空"<<endl;
    }
    else
    {
        while(p)
        {
            cout<<p->mId<<" "<<p->mName<<endl;
            p=p->next;
        }
    }
}
int  main()
{
    int  ID;
    LinkList  list1;
    string  name;
    cout  <<  "请输入学生姓名:"  <<  endl;
    for  (int  i=2018003;  i>=2018001;i--)            //依次输入名字
    {
        cin  >>  name;
        list1.addNodeAtHead(i,name);
    }
    for  (int  i  =  2018004;  i  <=  2018006;  i++)            //依次输入名字
    {
        cin  >>  name;
        list1.addNodeAtTail(i,name);
    }
    cout  <<"学生信息为:"<<  endl;
    list1.print();
    cout  <<  "请输入要删除的学生学号:"<<endl;
    cin  >>  ID;
    cout  <<  "学生信息为:"  <<  endl;
    list1.deleteNode(ID);
    list1.print();
    cout  <<  "请输入要插入的学生学号和姓名:"<<endl;
    cin  >>  ID  >>name;
    cout  <<  "学生信息为:"  <<  endl;
    list1.insertNode(ID,  name);
    list1.print();
    return  0;
}

输出结果如下图:
在这里插入图片描述

  • 6
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

芷汀若静

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值