本文主要是介绍1325:算法2-3~2-6:Big Bang,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
题目描述
输入格式
输入数据只有一组,有很多行。每行的格式可能是下列一种:
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<stdio.h>
#include<stdlib.h>
#include<string.h>
#define list_init_size 100
#define listincrement 10
typedef int status;
#define ok 1;
#define error 0;
typedef struct
{
char name[100];
}elemtype;
typedef struct
{
elemtype *elem;
int length;
int listsize;
}sqlist;
status initlist_sq(sqlist &l)
{
l.elem=(elemtype*)malloc(list_init_size*sizeof(elemtype));
if(!l.elem)
return ok;
l.length=0;
l.listsize=list_init_size;
return ok;
}
status listinsert_sq(sqlist &l,int i,elemtype e)
{
elemtype *p;
if(i<1||i>l.length+1)
return error;
if(l.length>=l.listsize)
{
elemtype *newbase;
newbase=(elemtype*)realloc(l.elem,(l.listsize+listincrement)*sizeof(elemtype));
if(!newbase)
return error;
l.elem=newbase;
l.listsize+=listincrement;
}
elemtype *q=&(l.elem[i-1]);
for (p=&(l.elem[l.length-1]);p>=q;--p)
*(p+1)=*p;
*q=e;
++l.length;
return ok;
}
status listdelete_sq(sqlist &l,int i,elemtype e)
{
elemtype *q,*p;
if(i<1 || i>l.length)
return error;
p=&(l.elem[i-1]);
e=*p;
q=l.elem+l.length-1;
for(++p;p<=q;++p)
*(p-1)=*p;
--l.length;
return ok;
}
int locateelem_sq(sqlist l,elemtype e,status(*compare)(elemtype,elemtype))
{
int i;
elemtype *p;
i=1;
p=l.elem;
while(i<=l.length && !(*compare)(*p++,e))
++i;
if(i<=l.length)
return i;
else
return 0;
}
void listshow(sqlist l)
{
int i;
for(i=0;i<l.length;i++)
{
if(i)
putchar(' ');
printf("%s",l.elem[i].name);
}
putchar('\n');
}
status cmp(elemtype e1, elemtype e2)
{
return (status)!strcmp(e1.name, e2.name);
}
int main()
{
sqlist namelist;
initlist_sq(namelist);
char mingling[10];
int d;
elemtype e;
while(scanf("%s",mingling)!=EOF)
{
if(strcmp(mingling,"insert")==0)
{
scanf("%d %s",&d,e.name);
listinsert_sq(namelist,d,e);
}
else if(strcmp(mingling,"show")==0)
listshow(namelist);
else if(strcmp(mingling,"delete")==0)
{
scanf("%s",e.name);
d=locateelem_sq(namelist,e,cmp);
listdelete_sq(namelist,d,e);
}
else if(strcmp(mingling,"search")==0)
{
scanf("%s",e.name);
printf("%d\n",locateelem_sq(namelist,e,cmp));
}
}
return 0;
}
这篇关于1325:算法2-3~2-6:Big Bang的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!