问题:
输入
输入数据只有一组,第一行有n+1个整数,第一个整数是这行余下的整数数目n,后面是n个整数。这一行整数是用来初始化列表的,并且输入的顺序与列表中的顺序相反,也就是说如果列表中是1、2、3那么输入的顺序是3、2、1。
第二行有一个整数m,代表下面还有m行。每行有一个字符串,字符串是“get”,“insert”,“delete”,“show”中的一种。如果是“get”或者“delete”,则其后跟着一个整数a,代表获得或者删除第a个元素;如果是“insert”,则其后跟着两个整数a和e,代表在第a个位置前面插入e;“show”之后没有整数。
输出
如果获取成功,则输出该元素;如果删除成功则输出“delete OK”;如果获取失败或者删除失败,则输出“get fail”以及“delete fail”。如果插入成功则输出“insert OK”,否则输出“insert fail”。如果是“show”则输出列表中的所有元素,如果列表是空的,则输出“link list is empty”。注:所有的双引号均不输出。
样例输入 Copy
3 3 2 1
21
show
delete 1
show
delete 2
show
delete 1
show
delete 2
insert 2 5
show
insert 1 5
show
insert 1 7
show
insert 2 5
show
insert 3 6
show
insert 1 8
show
get 2
样例输出 Copy
1 2 3
delete OK
2 3
delete OK
2
delete OK
Link list is empty
delete fail
insert fail
Link list is empty
insert OK
5
insert OK
7 5
insert OK
7 5 5
insert OK
7 5 6 5
insert OK
8 7 5 6 5
7
代码:
#include<iostream> #include<cstdlib> #include<cstdio> #include<algorithm> using namespace std; typedef struct node{ int data; node *next; }Node; typedef struct list{ node *head; }List; void Add (List *plist,int a[]); void Show(List *plist); void Get(List *plist,int nun); void Insert(List *plist, int t, int m); void Delete(List *plist,int t); int n; int main(){ cin>>n; int a[100]; for(int i= 1;i<=n;i++){ cin>>a[i]; } List list; list.head->next = NULL;//有头结点 Add(&list,a); int m; cin>>m; string s; int num1,num2,num3; while(m--){ cin>>s; if(s=="get"||s=="delete"){ cin>>num1; }else if(s=="insert"){ cin>>num2>>num3; } if(s=="show"){ Show(&list); }else if(s=="get"){ Get(&list,num1); }else if(s=="insert"){ Insert(&list,num2,num3); }else{ Delete(&list,num1); } } return 0; } void Add (List *plist,int a[]){ Node *p,*pre; pre = plist->head; for(int i=n;i>=1;i--){ p = (node*)malloc(sizeof(node)); p->data = a[i]; p->next = NULL; pre->next=p; pre = p; } } void Show(List *plist){ Node *L; if(plist->head->next==NULL){ cout<<"link list is empty"<<endl; }else{ for (L = plist->head->next; L; L = L->next) { cout << L->data << " "; } cout<<endl; } } void Get(List *plist,int num){ Node *L; if(plist->head->next==NULL){ cout<<"get fail"<<endl; } int isfound = 0; int count = 0; for(L=plist->head->next;L;L=L->next){ count++; if(count == num){ cout<<L->data<<endl; isfound = 1; break; } } if(isfound == 0){ cout<<"get fail"<<endl; } } void Insert(List *plist,int t,int m){ if(plist->head->next==NULL){ if(t==1){ Node *newp = (node *)malloc(sizeof(node)); newp->data = m; newp->next = NULL; plist->head->next = newp; cout << "insert OK" << endl; }else{ cout << "insert fail" << endl; } }else{ Node *p = plist->head; for(int i=1;i<=t-1;i++){ p = p->next; } Node *newp = (node *)malloc(sizeof(node)); newp->data = m; newp->next = p->next; p->next = newp; cout << "insert OK" << endl; } } void Delete(List *plist,int t){ Node *p,*pre; int isdelete = 0; int count=0; for(pre=plist->head,p=plist->head->next;p;pre = p,p=p->next){ count++; if(count==t){ pre->next=p->next; free(p); cout<<"delete OK"<<endl; isdelete = 1; break; } } if(isdelete==0){ cout<<"delete fail"<<endl; } }