顺序表上的基本操作实现 (c++)

实验题目(共10题, 第5题)


标题: 顺序表上的基本操作实现
时 限: 1000 ms
内存限制: 10000 K
总时限: 1000 ms
描述: 在顺序存储结构实现基本操作:初始化、创建、插入、删除、查找、遍历、逆置、合并运算。
输入: 输入线性表La的长度:n
输入线性表La中的元素:a1 a2 a3 ...an(数值有序,为降序)
输入要插入到线性表La中的元素x和插入的位置i:x i
输入要删除元素的位置:i
输入要查找的元素:x
输入线性表Lb的长度:m
输入线性表Lb中的元素:b1 b2...bm(数值有序,为升序)
输出: 创建好的线性表La=a1 a2...an
插入一个元素后的线性表La=a1 a2...an+1
删除一个元素后的线性表La=a1 a2...an
查找一个输入的元素,如果找到,输出"找到,x在第i个位置";如果没有找到,输出"没找到"的信息。
逆置La后的线性表an an-1...a1
合并La和Lb后的线性表
输入样例:

5
14 11 10 9 5
8 4
4
10
4
1 3 6 9

输出样例:

创建好的线性表La=14 11 10 9 5
插入一个元素后的线性表La=14 11 10 8 9 5
删除一个元素后的线性表La=14 11 10 9 5
找到,10在第3个位置
逆置后的线性表La=5 9 10 11 14
合并La和Lb后的线性表=1 3 5 6 9 9 10 11 14

提示:  
来源:
View Code
 1 #include<stdio.h>
2 #include<stdlib.h>
3 #include<malloc.h>
4
5 struct Lnode
6 {
7 char name[10];
8 char n[10];
9 char sex[3];
10 int age;
11 char classnumber[5];
12 char health[10];
13 int num;
14 struct Lnode * next;
15 };
16 typedef struct Lnode* Linklist;
17
18 void Creatlist(Linklist &L,int n);
19 void Operate(Linklist &L,int m);
20
21 int main()
22 {
23 int n,m;
24 scanf("%d%d",&n,&m);
25 Linklist L;
26 Creatlist(L,n);
27 Operate(L,m);
28
29 return 0;
30 }
31
32 void Creatlist(Linklist &L,int n)
33 {
34 int i=1;
35 Linklist p,q;
36 L=(Linklist)malloc(sizeof(Lnode));
37 p=L;
38 scanf("%s%s%s%d%s%s",p->name,p->n,p->sex,&p->age,p->classnumber,p->health);
39 p->num=i;
40 p->next=NULL;
41 for(i=1;i<n;i++)
42 {
43 q=(Linklist)malloc(sizeof(Lnode));
44 scanf("%s%s%s%d%s%s",q->name,q->n,q->sex,&q->age,q->classnumber,q->health);
45 q->num=i+1;
46 p->next=q;
47 p=q;
48 }
49 p->next=L;
50 }
51
52 void Operate(Linklist &L,int m)
53 {
54 int i=1;
55 Linklist pre,cur;
56 cur=pre=L;
57 while(cur->next!=cur)
58 {
59 for(;i<m;i++)
60 {
61 pre=cur;
62 cur=cur->next;
63 }
64 pre->next=cur->next;
65 i=1;
66 printf("%s %s %s %d %s %s\n",cur->name,cur->n,cur->sex,cur->age,cur->classnumber,cur->health);
67 free(cur);
68 cur=pre->next;
69 }
70 printf("%s %s %s %d %s %s\n",cur->name,cur->n,cur->sex,cur->age,cur->classnumber,cur->health);
71 free(cur);
72 }
posted @ 2011-05-24 17:52  itbird  Views(4503)  Comments(1Edit  收藏  举报