Document directory
- Lab questions (10 questions in total, 5th questions)
Lab questions (10 questions in total, 5th questions)
Title: |
Basic operations on ordered tables |
Time limit: |
1000 MS |
Memory limit: |
10000 K |
Total time limit: |
1000 MS |
Description: |
Perform basic operations in the ordered Storage Structure: initialization, creation, insertion, deletion, search, traversal, reverse, and merge operations. |
Input: |
Length of the input linear table la: N Element in the input linear table la: A1 A2 A3... an (numerical order, descending order) Input the element x to be inserted into the linear table la and the insert position I: x I Enter the location of the element to be deleted: I Enter the element to be searched: x Length of LB in the input linear table: m Element in the LB of the input linear table: B1 B2. .. BM (numerical order, ascending order) |
Output: |
Create a linear table LA = A1 a2... Insert an element to the linear table LA = A1 a2... an + 1 Delete the linear table LA = A1 a2... an after an element Search for an input element. If it is found, the output is "locate, X is at position I". If it is not found, the output is "not found. Inverse linear table an-1... A1 after LA Merge linear tables after La AND LB |
Input example: |
5 14 11 10 9 5 8 4 4 10 4 1 3 6 9 |
Output example: |
Created linear table LA = 14 11 10 9 5 Insert an element to the linear table LA = 14 11 10 8 9 5 Delete the linear table LA = 14 11 10 9 5 after an element Found. 10 is at 3rd locations. Inverse linear table LA = 5 9 10 11 14 Linear table after merging la and lB = 1 3 5 6 9 9 10 11 14 |
Tip: |
|
Source: |
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 }