// classlinklist.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "iostream.h"
struct student
{
  int num;
  char name;
  student *next;
};
class linklist
{
private:
    int n;
    student *head;
public:
    linklist();
    ~linklist();
    void creat();
    void add();
    void del();
    void show();
};
linklist::linklist(){}
linklist::~linklist()
{
   student*p1,*p2;
   p1=p2=head;
   while(n>=0)
   {
      p2=p1;
      p1=p1->next;
      delete p2;
      n--;
   }
}
void linklist::creat()
{
    student*p1,*p2;
    head=NULL;
    n=0;
    p2=p1=new student;
    cout<<" input num and name:" <<endl;
    cin>>p1->num>>p1->name ;
    while(p1->num!=0)
    {   
        n++;
        if(n==1) 
            head=p1;
        else
            p2=p1;
         p1=new student;
         cout<<" input num and name:" <<endl;
         cin>>p1->num>>p1->name ;
         p2->next=p1;
    }
    p1->next=NULL;
}
void linklist::add()
{
    student*p1,*p2;
    int k,m;
    cout<<"please choose: top=1 , mid=2, last=3, exit=4"<<endl;
    while(k<4)
    {
        cin>>k;
        p1=p2=head;
        if(k==1)
        {
            p1=new student;
            cout<<"input num and name"<<endl;
             cin>>p1->num>>p1->name ; 
            head=p1;
            p1->next=p2;
            n++;
        }
        else if(k==2) 
        {
            cout<<"please choose address:"<<endl;
            cin>>m;
            while(p1->next!=NULL&&m!=0)
            {
                p2=p1;
                p1=p1->next;
                m--;
            }
            if(m==0)
            {
                p2=new student;
                cout<<"input num and name"<<endl;
                 cin>>p2->num>>p2->name ;
                p2->next=p1->next;
                p1->next=p2;
                n++;
            }
            else 
                cout<<"not been found"<<endl;
        }
        else if(k==3)
        {
            while(p1->next!=NULL)
            {
                p2=p1;
                p1=p1->next;
            }
            p2=new student;
            cout<<"input num and name"<<endl;
             cin>>p2->num>>p2->name ;
            p2->next=NULL;
            n++;
        }
    }
}
void linklist::del()
{
    student*p1,*p2;
    int i;
    p1=p2=head;
    cout<<"will delete num(exit=0):"<<endl;
    while(i!=0)
    {
        cin>>i;
        if(i==1)
        {
            head=p1->next;
            delete p2;
             n-- ;
        }
        else if(i-1<=n)
        {
            while(i>1&&p1->next!=NULL)
            {
                p2=p1;
                p1=p1->next;
                i--;
            }
            if(i==1)
            {
                p2->next=p1->next;
                delete p1;
                n--;
            }
            else cout<<"not been found";
        }
    }
}
void linklist::show()
{
    student*p1, *p2;
    p1=p2=head;
    while(p1->next !=NULL)
    {
        p2=p1;
        cout<<"num: "<<p2->num<<"\t name: "<<p2->name<<endl;
        p1=p1->next;
    }
}
int main(int argc, char* argv[])
{
    linklist user;
    int inti;
    user.creat();
    while(inti!=4)
    {
        cout<<"add=1, delete=2, show=3, exit=4" <<endl;
        cin>>inti;
        if(inti==1)
            user.add();
        else if(inti==2)
            user.del();
        else 
            user.show();
    }
    return 0;
}

 
											





 
	    


