基础数据结构——顺序表(1)
Time Limit: 1000 MS Memory Limit: 10240 K
Description
模拟顺序表操作。
Input
一个班级10位同学的姓名,年龄。姓名为一个字符串,不超过20个字符。姓名和年龄直接用一个空格隔开。
Output
原样输出。本题一组测试数据。
Sample Input
freeleon 21
zeropointer 21
xiaohou 20
xiange 7
wind 21
symons 20
chenyu 19
lujunda 21
zhenhai 19
shuishen 19
Sample Output
freeleon 21
zeropointer 21
xiaohou 20
xiange 7
wind 21
symons 20
chenyu 19
lujunda 21
zhenhai 19
shuishen 19
代码
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<stdlib.h>
using namespace std;
typedef struct sqlist lnode;
#define list_size 1000
struct sqlist //sqlist是一种数据类型
{
int *elem;
string *str;
int listsize;
} ;
/*struct list
{
int *elem;
string *str;
int listsize;
} sqlist; //这里的sqlist是一个结构体变量,与上面的sqlist不一样;
*/
int Initializer_list(sqlist &L) //建表初始化
{
L.elem = (int *)malloc(sizeof(int));
L.str=new string; //string不能用malloc函数;
if(!L.elem) return -1;
L.listsize = list_size;
return 0;
} //可以没有;
int creatnewlist(sqlist &L) // 建立顺序表
{
int i;
L.elem = (int *)malloc(sizeof(int)*list_size);
L.str =new string[list_size];
if(!L.elem) return -1;
else
{
for(i = 1; i<=10; i++)
{
cin>>L.str[i];
scanf("%d",&L.elem[i]);
}
}
return 0;
}
void show(sqlist &L) //输出顺序表
{
int i;
for(i = 1; i<=10; i++)
{
cout<<L.str[i]<<" "<<L.elem[i]<<endl;
}
}
int main()
{
sqlist l;
Initializer_list(l);
creatnewlist(l);
show(l);
}
注意:
string型不能用scanf输入,可以用cin>>;
cin有判断输入数据的是什么类型的功能;