单向链表 插入 删除节点代码

##单向链表 插入 删除 代码

 #include<iostream>
 using namespace std;
 typedef struct id
 {
     int number;
     char name[20];
     struct id  *next;
 } student;
int main(void)
{
     int n,i;
    student *head,*pre;
    student *p1,*p2;
    head=new student;  //do not forget assign a dynamic memory             figure out the difference between head=p2=new student;   and lay out
    p2=new student;
    head->next=p2;
    cout<<"please enter student data "<<endl<<"1.number  2.name"<<endl<<"number 1: ";
    cin>> p2->number >> p2->name;
    for(int i=0;i<2;i++)
    {
        p1=new student;
        cout<<"number "<<i+2<<":";
        cin>>p1->number>>p1->name;
        p2->next=p1;
        p2=p1;
    }
    p1->next=NULL;
    p1=head;
    cout<<"print out the concrete data of each student"<<endl;
    while(p1->next!=NULL)
    {
      p1=p1->next;
      cout<<"student id:"<<p1->number<<" "<<"student name:"<<p1->name<<endl;
    }
   pre=new student;   //  attention!
   cout<<"do you want to add new student?"<<endl;
    cout<<"first student number /  second student name"<<endl;
    cin>>pre->number>>pre->name;

    cout<<"want to be number:";
    cin>>n;
    p1=head;
    for(i=1;i<n;i++)
    {
      p1=p1->next;

    }
    pre->next=p1->next;
    p1->next=pre;
    p1=head;
   while(p1->next!=NULL)
    {
      p1=p1->next;
      cout<<"student id:"<<p1->number<<" "<<"student name:"<<p1->name<<endl;
    }
      p1=head;
    int number;
    cout<<"now you are allowed to withdraw some student id"<<endl;
    cout<<"the id number:";
    cin>>number;
    while(p1->number!=number)
    {
       p2=p1;
       p1=p1->next;
    }
     p2->next=p1->next;
     p1=head;
   while(p1->next!=NULL)
    {
      p1=p1->next;
    cout<<"student id:"<<p1->number<<" "<<"student name:"<<p1->name<<endl;
    }

    return 0;
                            //linked list
} */


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值