1, Purpose of the experiment:
1. Review the knowledge of C language programming.
2. Master the representation and implementation of sequential storage structure of linear table.
3. Master the algorithm implementation of the basic operation of the sequence table.
2, Experiment content:
1. Establish a sequence table.
2. Insert, delete and find operations on the sequence table.
3, Test requirements:
1. Write the function to realize the basic algorithm of sequence table (initialization, search, insert, delete, etc.), and design a main program on this basis to complete the following functions:
(1) initialize a sequence table L, and the data element type can be optional;
(2) establish sequence table L, requiring at least 10 data elements;
(3) length of output sequence table L;
(4) search by position: output the ith element of the sequence table L, such as the third element;
(5) search by content: output the position of a given element;
(6) insert the given element before the ith element;
(7) delete the i th element of sequence table L;
(8) traverse the sequence table and output the elements in the table in order.
Write a menu so that the user can select the appropriate action.
- Expansion experiment (optional)
Using the basic operation of the sequence table, the intersection, union and difference sets of two sets A and B are obtained.
- Experimental steps:
#include "stdio.h"
#include "stdlib.h"
#include "malloc.h"
#define msize 100
#define elemtype int
typedef struct
{elemtype elem[msize];
int last;
}seqlist;
1. Initialization of sequence table
seqlist *init_seqlist()
{seqlist *L;
L=(seqlist *)malloc(sizeof(seqlist));
if(L!=NULL)
{L->last=-1;
printf("initialization succeeded \ n");
}
return L;
}
2. Create sequence table and input sequence table elements
seqlist *indata_seqlist (seqlist *L )
{elemtype m;
scanf("%d",&m);
while (m!=0)
{L - > Last + = 1; / / access Last
L->elem[L->last]=m;
scanf("%d",&m);
}
return L;
}
3. Get sequence table elements
elemtype Getdata_seqlist (seqlist *L,int i)
{if(i<1||i>L->last+1)
{return 0;
}
else
return (L->elem[i-1]);
}
4. Length of output sequence table
int seqlist_Long (seqlist *L )
{printf("the length of the sequence table is% d \ n", L - > last + 1);
return 0;
}
five Find the location of the given element
int search_seqlist_1 (seqlist *L,elemtype key) / / find the location of a given element
{int i;
for(i=0;i<=L->last;i++)
{if(L->elem[i]==key)
{printf("the position of the given element is% d\n",i+1);
return (i+1);}}
if(key!=L->elem[i]&&i==L->last)
printf("no such element \ n");
return 0;
}
6. Find corresponding elements by location
elemtype search_seqlist_2 (seqlist *L,elemtype key) / / find the corresponding element by location
{int i;
if(key<=0||key>(L->last))
{printf("input error, search failed \ n");
}
for(i=0;i<L->last;i++)
{if(i+1==key)
printf("the value of the corresponding element is% d \ n", L - > elem [i]);}
return (L->elem[i]);
}
seven Traversal sequence table
int print_seqlist(seqlist *L)
{int i;elemtype t;
if(L->last==-1)
{return 0;
}int j;
printf("all elements of the sequence table are \ n");
for(i=0;i<=L->last;i++)
{
printf("a[%d]=%d\t",i,L->elem[i]);}
return 1;
}
8. Insert elements in the sequence table
int insert_seqlist(seqlist *L,int i,elemtype x )
{int j;
if(L->last>=msize-1)
{printf("the table is full and cannot be inserted \ n");
return 0;
}
if((i<1)||(i>L->last+2))
{printf("unreasonable insertion position \ n");
return -1;
}
for(j=L->last;j>=i-1;j--)
L->elem[j+1]=L->elem[j];
L->elem[i-1]=x;
L->last++;
print_seqlist(L);
return 1;
}
9. Delete sequence table elements
int Delete_seqlist(seqlist *L,int i,elemtype *w)
{int k;
if((i<1)||(i>L->last+1))
{printf("unreasonable deletion location \ n");
return 0;
}
else
{*w=L->elem[i-1];
for(k=i-1;k<L->last;k++)
{L->elem[k]=L->elem[k+1];
}
}
L->last--;
print_seqlist(L);
return 1;}
10 menu
void menu()
{int a;
printf("\n *************************************************************************;
Printf ("\ n ************* 1 initialization sequence table L ************\n");
Printf ("\ n ******************** 2 create sequence table L ************** \ n");
Printf ("\ n ************************************************************************;
Printf ("\ n ************* 4 find the ith element of output order table L by position ***************** \ n");
Printf ("\ n **************************************************************************;
Printf ("\ n *************** 6 insert the given element before the ith element *********** \ n");
Pr i ntf ("\ n **************************************************************************;
Printf ("\ n ************************************************************************;
Printf ("\ n ************************************************ \ n");
Printf ("\ n ******************* 10 clean screen return menu ********************** \ n");
Printf ("\ n *************************************************** \ n");
Printf ("\ n *************************************************** \ n");
printf("please select 1-12:");}
11. Find the intersection of two sets A and B
void union_seqlist(seqlist *L1,seqlist *L2 )
{int i,len,k;
elemtype x;
len=seqlist_Long(L1);
for(i=1;i<=L2->last+1;i++)
{x=Getdata_seqlist (L2,i);
k=search_seqlist_1 (L1,x);
if(k==0)
{insert_seqlist(L1,len+1,x);
len=seqlist_Long(L1);
}}}
12. Find the union of two sets A and B
void union_seqlist_2(seqlist *L1,seqlist *L2 )
{int i,len,k;
elemtype x;
len=seqlist_Long(L1);
for(i=1;i<=L2->last+1;i++)
{x=Getdata_seqlist (L2,i);
k=search_seqlist_1 (L1,x);
if(k!=0)
{insert_seqlist(L1,len+1,x);
len=seqlist_Long(L1);
}}}
int main()
{menu();seqlist *L;seqlist *L1;seqlist*L2;int a;
while(1)
{
int t;scanf("%d",&t);
if(t<1||t>12)
{printf("input error, please re-enter: \ n");
scanf("%d",&t);}
switch(t)
{case 1:{L=init_seqlist();
break;}
case 2:{
printf("please enter an element and press 0 to end the input \ n");
L=indata_seqlist(L);
printf("this operation is completed, please continue to enter the next instruction \ n");
break;}
case 3:{
a=seqlist_Long (L);
printf("this operation is completed, please continue to enter the next instruction \ n");
break;}
case 5:{
int m;
printf("please enter the value of the element to be found \ n");
scanf("%d",&m);
search_seqlist_1(L,m);
printf("this operation is completed, please continue to enter the next instruction \ n");break;}
case 4:
{printf("enter the location of the element to be found \ n");
int d;
scanf("%d",&d);
search_seqlist_2 (L,d);
printf("this operation is completed, please continue to enter the next instruction \ n");break;
}
case 6:{printf("enter the position of the element to be inserted \ n");
int o;
scanf("%d",&o);
printf("enter the element to be inserted \ n");
int u;
scanf("%d",&u);
insert_seqlist(L,o,u);
printf("insert succeeded \ n");
printf("this operation is completed, please continue to enter the next instruction \ n");
break;
}
case 7:{printf("enter the position of the element to be deleted \ n");
int y;int h;
scanf("%d",&y);
Delete_seqlist(L,y,&h);
printf("deleted successfully \ n");
printf("this operation is completed, please continue to enter the next instruction \ n");
break;
}
case 8:{print_seqlist(L);
printf("this operation is completed, please continue to enter the next instruction \ n");break;}
case 9:{exit(0);break;}
case 10:{system("cls");menu();
printf("this operation is completed, please continue to enter the next instruction \ n");break;}
case 11:{
printf("please enter the data elements in sequence table A, ending with 0: \ n");
L1=init_seqlist();
L1=indata_seqlist(L1);
printf("please enter the data elements in sequence table B, ending with 0: \ n");
L2=init_seqlist();
L2=indata_seqlist(L2);
union_seqlist(L1,L2);
printf("AUB=");
print_seqlist(L1);
printf("this operation is completed, please continue to enter the next instruction \ n");
break;}
case 12:{
printf("please enter the data elements in sequence table A, ending with 0: \ n");
L1=init_seqlist();
L1=indata_seqlist(L1);
printf("please enter the data elements in sequence table B, ending with 0: \ n");
L2=init_seqlist();
L2=indata_seqlist(L2);
union_seqlist_2(L1,L2);
printf("AnB=");
print_seqlist(L1);
printf("this operation is completed, please continue to enter the next instruction \ n");
break;
}
default:{printf("input error, please re-enter \ n");
printf("please select 1-12:"); break;}
} }}
- Experimental results:
1. The menu is as follows:
2. The search traversal results of input data deletion are as follows:
3. The AUB results are as follows:
4. The result of AnB is as follows:
7, Attached (source code) #include "stdio.h" #include "stdlib.h" #include "malloc.h" #define msize 100 #define elemtype int typedef struct {elemtype elem[msize]; int last; }seqlist; seqlist *init_seqlist() {seqlist *L; L=(seqlist *)malloc(sizeof(seqlist)); if(L!=NULL) {L->last=-1; printf("Initialization succeeded\n"); } return L; } seqlist *indata_seqlist (seqlist *L ) {elemtype m; scanf("%d",&m); while (m!=0) {L->last+=1;//Access Last L->elem[L->last]=m; scanf("%d",&m); } return L; } elemtype Getdata_seqlist (seqlist *L,int i) {if(i<1||i>L->last+1) {return 0; } else return (L->elem[i-1]); } int seqlist_Long (seqlist *L ) {printf("The length of the sequence table is%d\n",L->last+1); return 0; } int search_seqlist_1 (seqlist *L,elemtype key)//Find the location of the given element {int i; for(i=0;i<=L->last;i++) {if(L->elem[i]==key) {printf("The position of the given element is%d\n",i+1); return (i+1);}} if(key!=L->elem[i]&&i==L->last) printf("No such element\n"); return 0; } elemtype search_seqlist_2 (seqlist *L,elemtype key )//Find corresponding elements by location {int i; if(key<=0||key>(L->last)) {printf("Input error, search failed\n"); } for(i=0;i<L->last;i++) {if(i+1==key) printf("The value of the corresponding element is%d\n",L->elem[i]);} return (L->elem[i]); } int print_seqlist(seqlist *L) {int i;elemtype t; if(L->last==-1) {return 0; }int j; printf("All elements of the sequence table are\n"); for(i=0;i<=L->last;i++) { printf("a[%d]=%d\t",i,L->elem[i]);} return 1; } int insert_seqlist(seqlist *L,int i,elemtype x ) {int j; if(L->last>=msize-1) {printf("The table is full and cannot be inserted\n"); return 0; } if((i<1)||(i>L->last+2)) {printf("Unreasonable insertion position\n"); return -1; } for(j=L->last;j>=i-1;j--) L->elem[j+1]=L->elem[j]; L->elem[i-1]=x; L->last++; print_seqlist(L); return 1; } int Delete_seqlist(seqlist *L,int i,elemtype *w) {int k; if((i<1)||(i>L->last+1)) {printf("Unreasonable deletion position\n"); return 0; } else {*w=L->elem[i-1]; for(k=i-1;k<L->last;k++) {L->elem[k]=L->elem[k+1]; } } L->last--; print_seqlist(L); return 1;} void menu() {int a; printf("\n************ Welcome to the basic operating system of the sequence table ************\n"); printf("\n************ 1 Initialization sequence table L ************\n"); printf("\n************ 2 Create sequence table L ************\n"); printf("\n************ 3 Output sequence table L Length of ************\n"); printf("\n************ 4 Find output order table by location L The first i Elements ************\n"); printf("\n************ 5 Find the location of the output given element by content ************\n"); printf("\n************ 6 In the first i Insert the given element before the first element ************\n"); printf("\n************ 7 Delete the second column of the sequence table i Elements ************\n"); printf("\n************ 8 All elements of the output sequence table ************\n"); printf("\n************ 9 Exit the system ************\n"); printf("\n************ 10 Clean screen return menu ************\n"); printf("\n************ 11 Seek set AUB ************\n"); printf("\n************ 12 Seek set AnB ************\n"); printf("Please select 1-12: ");} void union_seqlist(seqlist *L1,seqlist *L2 ) {int i,len,k; elemtype x; len=seqlist_Long(L1); for(i=1;i<=L2->last+1;i++) {x=Getdata_seqlist (L2,i); k=search_seqlist_1 (L1,x); if(k==0) {insert_seqlist(L1,len+1,x); len=seqlist_Long(L1); }}} void union_seqlist_2(seqlist *L1,seqlist *L2 ) {int i,len,k; elemtype x; len=seqlist_Long(L1); for(i=1;i<=L2->last+1;i++) {x=Getdata_seqlist (L2,i); k=search_seqlist_1 (L1,x); if(k!=0) {insert_seqlist(L1,len+1,x); len=seqlist_Long(L1); }}} int main() {menu();seqlist *L;seqlist *L1;seqlist*L2;int a; while(1) { int t;scanf("%d",&t); if(t<1||t>12) {printf("Input error, please re-enter:\n"); scanf("%d",&t);} switch(t) {case 1:{L=init_seqlist(); break;} case 2:{ printf("Please enter an element and press 0 to end the input\n"); L=indata_seqlist(L); printf("This operation is completed. Please continue to enter the next instruction\n"); break;} case 3:{ a=seqlist_Long (L); printf("This operation is completed. Please continue to enter the next instruction\n"); break;} case 5:{ int m; printf("Please enter the value of the element to be found\n"); scanf("%d",&m); search_seqlist_1(L,m); printf("This operation is completed. Please continue to enter the next instruction\n");break;} case 4: {printf("Enter the location of the element to be found\n"); int d; scanf("%d",&d); search_seqlist_2 (L,d); printf("This operation is completed. Please continue to enter the next instruction\n");break; } case 6:{printf("Enter the position of the element to be inserted\n"); int o; scanf("%d",&o); printf("Enter the element to be inserted\n"); int u; scanf("%d",&u); insert_seqlist(L,o,u); printf("Insert successful\n"); printf("This operation is completed. Please continue to enter the next instruction\n"); break; } case 7:{printf("Enter the location of the element to be deleted\n"); int y;int h; scanf("%d",&y); Delete_seqlist(L,y,&h); printf("Delete succeeded\n"); printf("This operation is completed. Please continue to enter the next instruction\n"); break; } case 8:{print_seqlist(L); printf("This operation is completed. Please continue to enter the next instruction\n");break;} case 9:{exit(0);break;} case 10:{system("cls");menu(); printf("This operation is completed. Please continue to enter the next instruction\n");break;} case 11:{ printf("Please enter the sequence table A Data elements in end with 0:\n"); L1=init_seqlist(); L1=indata_seqlist(L1); printf("Please enter the sequence table B Data elements in end with 0:\n"); L2=init_seqlist(); L2=indata_seqlist(L2); union_seqlist(L1,L2); printf("AUB="); print_seqlist(L1); printf("This operation is completed. Please continue to enter the next instruction\n"); break;} case 12:{ printf("Please enter the sequence table A Data elements in end with 0:\n"); L1=init_seqlist(); L1=indata_seqlist(L1); printf("Please enter the sequence table B Data elements in end with 0:\n"); L2=init_seqlist(); L2=indata_seqlist(L2); union_seqlist_2(L1,L2); printf("AnB="); print_seqlist(L1); printf("This operation is completed. Please continue to enter the next instruction\n"); break; } default:{printf("Input error, please re-enter\n"); printf("Please select 1-12: ");break;} } }}