(1) Purpose of the experiment
① Master the structure characteristics and basic operation of linear table.
② Consolidate C related programming methods and techniques.
③ Learn to use sequence table to solve practical problems.
(2) Experimental process
① Establish a sequence table, input n elements and output, define the function name as creatlist (SqList & L, int n);
② Find the largest element in the linear table and output, define the function name as (SqList L, int & maxelem);
③ Insert a positive integer x before the i-th element of the linear table, and define the function name as listinsert (SqList & L, int i, int x);
④ Delete the jth element in the linear table, and define the function named ListDelete (SqList & L, int j);
⑤ The elements in the linear table are arranged in ascending order, and the function name is SortList(SqList L);
⑥ Reverse the elements in the linear table in place (only one temporary storage unit is allowed), and define the function name as ReverseList(SqList L);
PS: the results of operation ⑤ will not affect operation ⑥
(3) Experimental results
① Program code
#include <iostream> #include <cstdio> #include <algorithm> #include <cstring> #define MAXSIZE 100 #define TRUE 1 #define FALSE 0 #define OK 1 #define ERROR 0 #define INFEASIBLE -1 #define OVERFLOW -2 #define LIST_INIT_SIZE 5 #define LISTINCREMENT 1 using namespace std; typedef int Status; typedef int ElemType[MAXSIZE]; typedef struct { ElemType elem; int length; }SqList; int n, maxelem, i, j, x; Status InitList(SqList &L) //establish { for(int i = 0; i < MAXSIZE; ++i) L.elem[i] = 0; L.length = 0; return OK; } Status CreatList(SqList &L, int n) //Set up a sequence table, input n elements and output { printf("Please enter the number of elements:\n"); scanf("%d", &n); if(n <= 0 || n > MAXSIZE) { printf("Transboundary\n"); return ERROR; } printf("Please enter the elements:\n"); for(int i = 0; i < n; ++i) { scanf("%d", &L.elem[i]); //It can also be established by inserting ++L.length; } for(int i = 0; i < L.length; ++i) printf("%d ", L.elem[i]); printf("\n"); return OK; } Status Find_MaxElem(SqList L, int &maxelem) //Find the largest element in the linear table and output { if(!L.length) { printf("Non-existent\n"); return ERROR; } maxelem = L.elem[0]; for(int i = 0; i < L.length; ++i) if(L.elem[i] > maxelem) maxelem = L.elem[i]; printf("The largest element in the linear table is:\n"); printf("%d\n", maxelem); return OK; } Status ListInsert(SqList &L, int i, int x) //Insert a positive integer x before the i-th element of the linear table { printf("Please enter the insertion position and value:\n"); scanf("%d%d",&i, &x); if((i < 1) || (i > L.length+1)) { printf("Transboundary\n"); return ERROR; } if(L.length == MAXSIZE) return ERROR; for(int j = L.length-1; j >= i-1; --j) L.elem[j+1] = L.elem[j]; L.elem[i-1] = x; ++L.length; printf("The linear table after inserting is as follow:\n"); for(int i = 0; i < L.length; ++i) printf("%d ", L.elem[i]); printf("\n"); return OK; } Status ListDelete(SqList &L, int j) //Delete the j th element in the linear table { printf("Enter the position of the element to delete:\n"); scanf("%d", &j); if((j < 1) || (j > L.length)) { printf("Transboundary\n"); return ERROR; } for(int i = j; i <= L.length; ++i) L.elem[i-1] = L.elem[i]; --L.length; printf("The linear table after deleting is as follow:\n"); for(int i = 0; i < L.length; ++i) printf("%d ", L.elem[i]); printf("\n"); return OK; } Status SortList(SqList L) //Arrange elements in a linear table in ascending order { if(!L.length) { printf("Transboundary\n"); return ERROR; } int i, j, k, t; for(i = 0; i < L.length-1; ++i) { k = i; for(j = i+1; j < L.length; ++j) if(L.elem[j] < L.elem[k]) k = j; if(k != i) { t = L.elem[i]; L.elem[i] = L.elem[k]; L.elem[k] = t; } } printf("The linear table after ascending is as follow:\n"); for(i = 0; i < L.length; ++i) printf("%d ", L.elem[i]); printf("\n"); return OK; } Status ReverseList(SqList L) //Reverse elements in a linear table in place (only one staging unit is allowed) { if(!L.length) { printf("Transboundary\n"); return ERROR; } int l, r, t; l = 0; r = L.length-1; while(l < r) { t = L.elem[l]; L.elem[l] = L.elem[r]; L.elem[r] = t; ++l; --r; } printf("The linear table after local reverse order is as follow:\n"); for(int i = 0; i < L.length; ++i) printf("%d ", L.elem[i]); printf("\n"); return OK; } int main() { SqList L; InitList(L); CreatList(L, n); Find_MaxElem(L, maxelem); ListInsert(L, i, x); ListDelete(L, j); SortList(L); ReverseList(L); return 0; }
② Test results
(4) Experimental thoughts
Through this experiment, I have consolidated the structural characteristics and basic operation of linear table, as well as the programming methods and technologies related to C language. At the same time, I realize that mastering textbook knowledge is the basis of the experiment. If I don't thoroughly study the relevant knowledge in textbook, the experiment will be very difficult and waste a lot of time. In this experiment, it is not only important to realize various operations of a sequence table, but also important to learn the methods of thinking and solving problems by hands through the process of doing experiments.