文章目錄
實驗題目(共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 }