#include<stdlib.h>
#include<stdio.h>
#include <malloc.h>
#include<iostream>
using namespace std;
#define ok 1
#define error 0
#define null 0
typedef struct node//define a structure;
{ int data;
struct node * next;//pointer to the subsequents
struct node * front;//precursor point
char name;
int order;
}lnode,*linklist;
void initlist(linklist &head)//initialize hte list
{ head=new lnode;
head->next=null;
}
int creat(linklist &head)//creat your linked list;
{ int n,e;
linklist temp=(linklist)malloc(sizeof(lnode));
temp=head;
head->front=temp;
cout<<"please enter the count of your list:\n";
cin>>n;
temp->order=0;
for(int i=1;i<=n;i++)
{ linklist p=new lnode;
cout<<"the data of the "<<i<<"th member in the list :\t";
cin>>e;
p->data=e;
temp->next=p;
p->next=null;
p->front=temp;
p->order=p->front->order+1;
temp=p;//Replace the old temp with the new one; Then cycle again;
}
return 1;
}
void printlist(linklist head)//traverse your list
{ linklist p=head->next;
while(p!=null)
{ cout<<"the ["<< p->order<<"th] member is :\t"<<p->data<<"\n";
p=p->next;
}
cout<<"\b"<<"finish"<<"\n";
}
int insertlist(linklist &head,int i,int e)//insert
{ linklist p=head;
int j=0;
while(p&&j<i-1)//lookint for the insert position
{ p=p->next;
++j;
}
if(!p||j<i-1) return 0;
linklist temp=new lnode;
temp->data=e;
p->next->front=temp;
temp->next=p->next;
p->next=temp;
temp->front=p;
temp->order=p->order+1;
if(i!=1)// normal
{ while(p)
{ p->order=p->front->order+1;
p=p->next;
}
}
if(i==1)//special circumstances insert first
{ p=p->next;
while(p)
{ p->order=p->front->order+1;
p=p->next;
}
}
return 1;
}
int insert(linklist &head)
{ int i,e,flag;
cout<<"insert the position and element";
cin>>i>>e;
flag=insertlist(head,i,e);
if(flag)
{ cout<<"success\n";
printlist(head);
}
return 1;
}
int deletelist(linklist &head,int i)
{ linklist p=head;
int j=0;
while(p->next&&j<=i-1)
{ p=p->next;
++j;
}
p->front->next=p->next;
p->next->front=p->front;
p=p->next;
while(p!=null)
{ p->order=p->front->order+1;
p=p->next;
}
}
int deleteuy(linklist &head)
{ int i;
cout<<"the position : ";
cin>>i;
deletelist(head ,i);
printlist(head);
return 1;
}
void locate_data_list(linklist head)
{ int i,j=0;
linklist p=head;
cout<<"the position : ";
cin>>i;
while(j<i)
{ p=p->next;
j++;
}
cout<<"the data which belong to the "<<i<<"th member is"<< p->data<<"\n";
}
void locate_order_list(linklist head)
{ int e,i=1;
linklist p=head;
cout<<"the element : ";
cin>>e;
while(p->data!= e)
{ p=p->next;
}
cout<<"the psition which element is ["<< e<<"] is the "<<p->order<<"th\n";
}
int menu()
{ linklist l;
initlist(l);
cout<<"please enter your instructions \n";
cout<<"a or A : creat,b or B : insert,c or C : delete,d or D : inquire data or order,e or E : output,ctrl+Z : finish\n";
char choice;
while(scanf("%c",&choice)!=EOF)
{ switch(choice)
{ case 'a':
case 'A':{creat(l);break;}
case 'b':
case 'B':insert(l);break;
case 'c':
case 'C':deleteuy(l);break;
case 'd':locate_data_list(l);
case 'D':locate_order_list(l); break;
case 'E':
case 'e':printlist(l);break;
default :cout<<"error or the next instructions";break;
}
}
return 1;
}
int main()
{ menu();
system("pause");
return 0;
}