Data structure -- c language implementation of sequence table (super detailed notes / experimental report)

Data structure - c language implementation of sequence table (super detailed notes / experimental report) ...
Knowledge review
Experimental topic
Experimental purpose
Experimental requirements
Experiment contents and steps
Data structure - c language implementation of sequence table (super detailed notes / experimental report)

Knowledge review

Linear list is one of the most basic and commonly used data structures. It has two storage structures - sequential list and linked list. The sequence table is realized by vectors with continuous addresses, which is convenient for random access. When inserting and deleting sequential tables, about half of the data elements in the table need to be moved on average, and the capacity is difficult to expand.

Experimental topic

Realize various basic operations of sequence table

Experimental purpose

  1. Familiar with the process of converting algorithm into program code;
  2. Understand the logical structure characteristics of sequence table, and master the C language description method of sequence table storage structure;
  3. Master the basic operation of sequence table: search, insert, delete, etc., and master the random access characteristics of sequence table.

Experimental requirements

  1. The sequential table is used as the storage structure;
  2. Realize the insertion operation of data elements on the sequence table;
  3. Realize the deletion operation of data elements on the sequence table;
  4. Realize the search operation of data elements on the sequence table.

Experiment contents and steps

1. Demand analysis

The menu is used as the interface between the user and the program, and the user inputs the menu number to implement the corresponding operation.

2. Outline design

Several functions are designed to implement initialization, insertion, deletion and lookup functions, and then the function is invoked in the main function to achieve operation.

3. Detailed design

Import some libraries and define the representation size and the type of elements in the table.

#include<stdio.h> #include<stdlib.h> //#include<malloc.h> //Defines the size of the table #define MaxSize 20 //Use typedef to define a name for int as ElemType, which means the type of the element in the table typedef int ElemType;

The sequential storage structure of linear table is represented by one-dimensional array in high-level programming language. The following table of one-dimensional array corresponds to the sequence number in the element re linear table.

//Sequential table structure definition, including the length of the table and an array typedef struct Seqlist { ElemType elem[MaxSize]; int length; }SeqList;

Initialize the linear table L as an empty table, that is, the length is 0

//Define sequence table initialization function int Init_SeqList(SeqList *L) { L->length=0; //Set length to 0, empty table //printf("%d",L->length); return 1; }

Returns the location of the input element

//Defines a function that finds the location of an element int Locate_SeqList(SeqList *L,int x) { int i=0; //From the 0 to the end, the end condition is ① the index has reached length ② the value found in the table is equal to the entered value for(i=0;i<L->length&&L->elem[i]!=x;i++) { ;; } /* while(i<L->length&&L->elem[i]!=x) i++;*/ //End cycle, first case if(i>=L->length) { printf("The element does not exist in the sequence table!\n"); return 0; } //In the second case, because this is an array, the ordinal number returned should be increased by 1 else return i+1; }

Function to define the insertion order table: insert before the ith element.
Note when writing: for users, the first is the 0 of the array.

//Function to define the insertion order table: insert x before the ith element //Note: for the user, the first is the 0 of the array int Insert_SeqList(SeqList *L,int i,int x) { int j; //printf("MaxSize\n"); //printf("%d",MaxSize); //printf("%d",L.length); if(L->length>=MaxSize) { printf("The sequence table is full, unable to insert!"); return 0; } else if(i<=0||i>L->length+1) { printf("Incorrect insertion position!"); return 0; } //Starting with the last element, move back one by one to make room for the elements to be inserted for(j=L->length-1;j>=i-1;j--) { L->elem[j+1]=L->elem[j]; } //Inserting before the ith element is to move the element starting from i back and assign it to the ith element, which is i-1 in the array L->elem[i-1]=x; L->length++; //Array length + 1 after insertion return 1; }

Delete the sequence table element function and delete the ith element.

//Define the function to delete the element of the sequence table and delete the ith element int Delete_SeqList(SeqList *L,int i) { int j; if((i<1)||(i>L->length))//And insertion are the same judgment conditions { printf("The deletion position is incorrect!"); return 0; } //To delete the ith element is to start from the ith element and overwrite it one by one from the back to the front for(j=i;j<L->length;j++) { L->elem[j-1]=L->elem[j]; } L->length--;//Array length - 1 return 1; }

Displays the functions of the linear table.

//Defines a function that displays a linear table int Display_SeqList(SeqList *L) { int i; //Print out one by one for(i=0;i<L->length;i++) { printf("%d",L->elem[i]); //return 1; Serious error. In fact, it has been inserted, but only one (a previous small bug) is displayed } return 1; }

In the main function part, the operation of the linear table is more clearly displayed in the form of a "menu".

int main() { SeqList L; ElemType e,x; int i=1,k,j,n,ii; Init_SeqList(&L); //printf("%d",Init_SeqList(&L)); //printf("initialization \ n establishment sequence table is as follows:"); //printf("%d",L.length); //Insert element 5201314 Insert_SeqList(&L,1,5); Insert_SeqList(&L,2,2); Insert_SeqList(&L,3,0); Insert_SeqList(&L,4,1); Insert_SeqList(&L,5,3); Insert_SeqList(&L,6,1); Insert_SeqList(&L,7,4); //printf("\n"); while(i)//Keep it going { printf("The current sequence is as follows: "); Display_SeqList(&L); printf("\n------------------------------------\n"); printf(" Main Menu \n"); printf(" 1 Find the specified element \n"); printf(" 2 Inserts the element into the specified location \n"); printf(" 3 Deletes an element at a specified location \n"); printf(" 4 Clear screen \n"); printf(" 0 End program \n"); printf("------------------------------------\n"); printf("Please enter the menu number you selected<1, 2, 3, 0>:\n"); scanf("%d",&i); switch(i) { case 1: printf("Please enter the lookup element:"); scanf("%d",&x); j=Locate_SeqList(&L,x); if(j!=0)//Find the element and return i+1. You can see the previous function { printf("The specified element location is %d",j); } printf("\n\n"); break; case 2: printf("Please enter the insert element location:"); scanf("%d",&k); printf("Please enter the insert element value:"); scanf("%d",&x); j=Insert_SeqList(&L,k,x); if(j!=0) { printf("The sequence table after insertion is shown as follows:\n"); Display_SeqList(&L); printf("\n"); } printf("\n\n"); break; case 3: printf("Please enter the location of the deleted element:"); scanf("%d",&k); j=Delete_SeqList(&L,k); if(j!=0) { printf("The sequence table after insertion is shown as follows:\n"); Display_SeqList(&L); printf("\n"); } printf("\n\n"); break; case 4: system("cls"); break; case 0: exit(0); break; default: printf("Incorrect input~"); } } return 0; } }

4. Commissioning analysis

Problems encountered and Solutions
  • The code given in the experimental instruction will report errors in the code blocks environment, and some sentence writing specifications are different. But just correct it.
Spatiotemporal analysis of algorithm
  • The sequence table is relatively simple, which is implemented by relying on arrays. What is worth improving is to insert functions. It will be better for users to input the elements of the sequence table themselves.
experimental result

The experimental results are very good. All operations can be performed normally, and the "clear screen" option is added to make the interface more tidy.

Experimental summary

This is the code implementation of the first data structure, which is mainly written in the experimental guide. It is not original, but it has also learned a lot of knowledge and built a good framework for later programming! Repeat more and refine everything into steel!

Finally, the complete code is attached

#include<stdio.h> #include<stdlib.h> //#include<malloc.h> //Defines the size of the table #define MaxSize 20 //Use typedef to define a name for int as ElemType, which means the type of the element in the table typedef int ElemType; //Sequential table structure definition, including the length of the table and an array typedef struct Seqlist { ElemType elem[MaxSize]; int length; }SeqList; //Define sequence table initialization function int Init_SeqList(SeqList *L) { L->length=0; //Set length to 0, empty table //printf("%d",L->length); return 1; } //Defines a function that finds the location of an element int Locate_SeqList(SeqList *L,int x) { int i=0; //From the 0 to the end, the end condition is ① the index has reached length ② the value found in the table is equal to the entered value for(i=0;i<L->length&&L->elem[i]!=x;i++) { ;; } /* while(i<L->length&&L->elem[i]!=x) i++;*/ //End cycle, first case if(i>=L->length) { printf("The element does not exist in the sequence table!\n"); return 0; } //In the second case, because this is an array, the ordinal number returned should be increased by 1 else return i+1; } //Function to define the insertion order table: insert x before the ith element //Note: for the user, the first is the 0 of the array int Insert_SeqList(SeqList *L,int i,int x) { int j; //printf("MaxSize\n"); //printf("%d",MaxSize); //printf("%d",L.length); if(L->length>=MaxSize) { printf("The sequence table is full, unable to insert!"); return 0; } else if(i<=0||i>L->length+1) { printf("Incorrect insertion position!"); return 0; } //Starting with the last element, move back one by one to make room for the elements to be inserted for(j=L->length-1;j>=i-1;j--) { L->elem[j+1]=L->elem[j]; } //Inserting before the ith element is to move the element starting from i back and assign it to the ith element, which is i-1 in the array L->elem[i-1]=x; L->length++; //Array length + 1 after insertion return 1; } //Define the function to delete the element of the sequence table and delete the ith element int Delete_SeqList(SeqList *L,int i) { int j; if((i<1)||(i>L->length))//And insertion are the same judgment conditions { printf("The deletion position is incorrect!"); return 0; } //To delete the ith element is to start from the ith element and overwrite it one by one from the back to the front for(j=i;j<L->length;j++) { L->elem[j-1]=L->elem[j]; } L->length--;//Array length - 1 return 1; } //Defines a function that displays a linear table int Display_SeqList(SeqList *L) { int i; //Print out one by one for(i=0;i<L->length;i++) { printf("%d",L->elem[i]); //return 1; Serious error. In fact, it has been inserted, but only one (a previous small bug) is displayed } return 1; } int main() { SeqList L; ElemType e,x; int i=1,k,j,n,ii; Init_SeqList(&L); //printf("%d",Init_SeqList(&L)); //printf("initialization \ n establishment sequence table is as follows:"); //printf("%d",L.length); //Insert element 5201314 Insert_SeqList(&L,1,5); Insert_SeqList(&L,2,2); Insert_SeqList(&L,3,0); Insert_SeqList(&L,4,1); Insert_SeqList(&L,5,3); Insert_SeqList(&L,6,1); Insert_SeqList(&L,7,4); //printf("\n"); while(i)//Keep it going { printf("The current sequence is as follows: "); Display_SeqList(&L); printf("\n------------------------------------\n"); printf(" Main Menu \n"); printf(" 1 Find the specified element \n"); printf(" 2 Inserts the element into the specified location \n"); printf(" 3 Deletes an element at a specified location \n"); printf(" 4 Clear screen \n"); printf(" 0 End program \n"); printf("------------------------------------\n"); printf("Please enter the menu number you selected<1, 2, 3, 0>:\n"); scanf("%d",&i); switch(i) { case 1: printf("Please enter the lookup element:"); scanf("%d",&x); j=Locate_SeqList(&L,x); if(j!=0)//Find the element and return i+1. You can see the previous function { printf("The specified element location is %d",j); } printf("\n\n"); break; case 2: printf("Please enter the insert element location:"); scanf("%d",&k); printf("Please enter the insert element value:"); scanf("%d",&x); j=Insert_SeqList(&L,k,x); if(j!=0) { printf("The sequence table after insertion is shown as follows:\n"); Display_SeqList(&L); printf("\n"); } printf("\n\n"); break; case 3: printf("Please enter the location of the deleted element:"); scanf("%d",&k); j=Delete_SeqList(&L,k); if(j!=0) { printf("The sequence table after insertion is shown as follows:\n"); Display_SeqList(&L); printf("\n"); } printf("\n\n"); break; case 4: system("cls"); break; case 0: exit(0); break; default: printf("Incorrect input~"); } } return 0; }

25 September 2021, 03:42 | Views: 3858

Add new comment

For adding a comment, please log in
or create account

0 comments