#include<bits/stdc++.h>
using namespace std;
struct node{
node *next;
char name[20];
char id[20];
int len;
};
node *head;
void init(node* &head)
{
head=(node*)malloc( sizeof (node ));
head->next=NULL;
head->len=0;
// head->name=NULL;
// head->id=NULL;
}
void insert(node* &head,char* name,char* id)
{
node * L=head;
while(L) {
// cout<<"--"<<endl;
L=L->next;
}
L=(node*)malloc(sizeof (node));
strcpy(L->name,name);
strcpy(L->id,id);
//cout<<L->name<<" "<<L->id<<endl;
head->len++;
L->next=head->next;
head->next=L;
}
void modify(node* &List,int k,char name[],char id[])
{
if(k>List->len) {
cout<<"len to short!"<<endl;
return ;
}
k=List->len-k+1;
node* L=List;
while(k--)
{
L=L->next;
}
strcpy(L->name,name);
strcpy(L->id,id);
}
void del(node* &List,int k)
{
if(k>List->len) {
cout<<"len to short!"<<endl;
return ;
}
k=List->len-k+1;
node*L=List;
while(--k)
L=L->next;
L->next=(L->next->next);
}
void print(node *L)
{
node *head=L;
head=head->next;
int cnt=0;
cout<<"print the list"<<endl;
while(head)
{
cnt++;
cout<<cnt<<" "<<head->id<<" "<<head->name<<endl;
head=head->next;
}
}
void query(node *L,int k)
{
node*x=L;
k=L->len-k+1;
if(k>L->len)
{
cout<<"len too short"<<endl;
return ;
}
for(int i=0;i<k;i++)
{
x=x->next;
}
cout<<k<<" "<<x->id<<" "<<x->name<<endl;
}
int main()
{
cout<<"welcome to here!"<<endl;
int opt;
while(cin>>opt)
{
if(opt==1)
{
init(head);
}
else if(opt==2)
{
char name[20];
char id[20];
cin >> name >> id;
insert(head,name,id);
// cout<<head->len<<endl;
//cout<<(head->next)->name<<endl;
}
else if(opt==3)
{
int k;
cin>>k;
query(head,k);
}
else if(opt==4)
{
int k;
cin>>k;
char name[20];
char id[20];
cin >> name >> id;
modify(head,k,name,id);
}
else if(opt==5)
{
int k;
cin>>k;
del(head,k);
}
else if(opt==6)
{
print(head);
}
else if(opt==7)
{
cout<<"Quit..."<<endl;
return 0;
}
}
return 0;
}
/*
1
2
myq
120
*/
mamn
最新推荐文章于 2022-12-26 13:09:34 发布