题目描述





输入格式
输入数据只有一组,有很多行。每行的格式可能是下列一种:
insert a name
delete name
show
search name
其中 a 是一个整数,代表在第a个名字前插入名字。name是一个姓名,只包含英文字母的大小写,每个名字不超过30个字符。
输入保证不会插入列表中已经存在的姓名,不会删除列表中不存在的姓名,也不会搜索列表中不存在的姓名。
输出
起始时,列表是空的。只输出show和search name 的结果。show将列表中的姓名全部输出,search只输出找到该名字的序号(从1开始)。每次输出占一行,姓名间用空格隔开。如果列表中没有名字了,show时也要输出一个空行。
样例输入
insert 1 Stuart
insert 2 Bernadette
show
search Stuart
delete Stuart
show
insert 2 Stuart
show
insert 1 Amy
insert 2 Leslie
insert 3 Stephanie
show
delete Leslie
show
search Stuart
样例输出
Stuart Bernadette
1
Bernadette
Bernadette Stuart
Amy Leslie Stephanie Bernadette Stuart
Amy Stephanie Bernadette Stuart
4
#include<iostream>
#include<string.h>
#include<malloc.h>
#include<stdio.h>
using namespace std;
#define ERROR 0;
#define OK 1;
int len;
typedef int Status;
typedef struct pepo
{
char name[40];
struct pepo *next;
}Lnode,*LinList;
Status CreateList(LinList *L)
{
(*L)=(LinList )malloc(sizeof(Lnode));
(*L)->next=NULL;
len=0;
return OK;
}
Status InsertLin(LinList L,int i,char name[])
{
LinList q,p;
int j=0;
q=L;
p=(LinList )malloc(sizeof(Lnode));
strcpy(p->name,name);
if(!(q->next))
{
p->next=L->next;
L->next=p;
len++;
return OK;
}
while(q->next&&j<i-1)
{
q=q->next;
j++;
}
if(!q||j>i-1)
return ERROR;
p->next=q->next;
q->next=p;
len++;
return OK;
}
void DeleteLin(LinList L,char name[])
{
LinList q,p;
q=L->next;
p=L;
while(q&&strcmp(q->name,name)!=0)
{
p=q;
q=q->next;
}
p->next=q->next;
free(q);
len--;
}
Status SearchLin(LinList L,char name[])
{
LinList q;
int j=1;
q=L->next;
while(q&&strcmp(q->name,name)!=0)
{
j++;
q=q->next;
}
if(!q)
return ERROR;
return j;
}
void ShowList(LinList L)
{
LinList q;
q=L->next;
cout<<q->name;
while(q->next)
{
q=q->next;
cout<<' '<<q->name;
}
cout<<endl;
}
int main()
{
char nam[40],str[40];
int n,k;
LinList L;
CreateList(&L);
while(cin>>str)
{
if(strcmp(str,"insert")==0)
{
cin>>n>>nam;
InsertLin(L,n,nam);
}
else if(strcmp(str,"delete")==0)
{
cin>>nam;
DeleteLin(L,nam);
}
else if(strcmp(str,"search")==0)
{
cin>>nam;
k=SearchLin(L,nam);
cout<<k<<endl;
}
else
{
if(len!=0)
ShowList(L);
else
cout<<endl;
}
}
}