Problem description and code:
*/ * The School of Computer and Control engineering, Yantai University : Zhangxiaotong * Completion date: September 12, 2016 * Problem Description: In the study of data structure, mastering basic arithmetic is a basic work. This "abstract" level of the results, applicable to a variety of applications, but also the basis for the training of computational thinking. (1) Initialize linear table initlist (&L): Constructs an empty linear table L (2) destroys the linear table destroylist (&L): Frees the linear table L occupies memory space (3) The linear table is empty table Listempty (L) : If L is a blank table, return True, otherwise return False (4) to find the length of the linear table Listlength (L): Returns the number of elements in L (5) Output linear table Displist (L): When linear table L is not empty, the range of the nodes in L is displayed sequentially (6) To find a data element at the specified position in the linear table L Getelem (l,i,&e): Returns the value of the I element in L (7) finds the element Locateelem (l,e): Returns the 1th ordinal in the linear table L equal to E, and cannot find the return 0 (8) Insert element Listinsert (&l, I, &e): Insert element E in the linear Table L, (9) Delete element Listdelete (&l, I, &e): delete element I in linear table L, E return deleted value ; */
(1) The purpose is to test the algorithm of "establishing linear table" createlist, in order to view the results of the table, it is necessary to implement the algorithm displist of "output linear table". In the study displist, it is found that to output linear table and to determine whether the table is empty, it is necessary to realize the algorithm listempty to determine whether the linear table is empty. This, in addition to the main function, is composed of 4 functions. The main function is used to write test-related code.
#include <stdio.h> #include <malloc.h> #define MAXSIZE typedef int ELEMTYPE; typedef struct { elemtype data[maxsize]; int length; } SqList; Custom Function Declaration section void Creatlist (sqlist *&l,elemtype a[],int N);//create linear table with array void Displist (SqList *l);// Output linear table Dislist (l) bool Listempty (sqlist *l);//Determine if NULL table Listempty (L) //Implement test function int main () { SqList *sq; Elemtype x[6]={5,8,7,2,4,9}; Creatlist (sq,x,6); Displist (sq); return 0; } Implement custom Function void Creatlist (sqlist *&l,elemtype a[],int N)//create linear table with array { int i; L= (SqList *) malloc (sizeof (sqlist)); for (i=0;i<n;i++) l->data[i]=a[i]; l->length=n; } void Displist (SqList *l)//Output Linear table dislist (L) { int i; for (i=0;i<l->length;i++) printf ("%d", L->data[i]); printf ("\ n"); } BOOL Listempty (sqlist *l)//Determines whether the empty table Listempty (L) { return (l->length==0); }
Operation Result:
(2) on the basis of a linear table, it is possible to find the listlength of the linear table length, the getelem of a data element in the specified position in the linear table L, and the algorithm for finding the Locateelem of the element. Added on the basis of the original procedure:
increase the length of the linear table listlength function and test;
Increase the function of the getelem of a data element in the specified position in the linear table l and test;
Add function to find element Locateelem and test.
1.head.h's Code
#include <stdio.h> #include <malloc.h> #define MAXSIZE typedef int ELEMTYPE; typedef struct { elemtype data[maxsize]; int length; } SqList; Custom Function Declaration section void Creatlist (sqlist *&l,elemtype a[],int N);//create linear table with array void Displist (SqList *l);// Output linear table Dislist (l) bool Listempty (sqlist *l);//Determine if NULL table Listempty (l) int listlength (sqlist *l);// Find the length of the linear table Listlength (L) bool Getelem (sqlist *l,int i,elemtype &e);//Request a Data element value Getelem (l,i,e) int Locateelem (SqList *l, elemtype e); Find Locateelem by Element value (l,e)
code in the 2.main.cpp
#include "head.h" //Implement test function int main () { sqlist *sq; Elemtype x[6]={5,8,7,2,4,9}; Creatlist (sq,x,6); Displist (sq); Elemtype A; int loc; printf ("Table length:%d\n", listlength (Sq)); Test the length if (Getelem (Sq, 3, a)) //test the case in the range printf ("found a 3rd element value of:%d\n", a); else printf ("3rd element out of range!") \ n "); if (Getelem (Sq, n, a)) //test is not in the range of the case printf ("found the 15th element value:%d\n", a); else printf ("15th element out of range!") \ n "); if ((Loc=locateelem (Sq, 8)) >0) //test can be found in the case of printf ("found, the element with a value of 8 is%d \ n", loc); else printf ("A value of 8 element wood is found! \ n "); if (Loc=locateelem (sq), >0) //test cannot be found in the case of printf ("found, the element with a value of 17 is%d \ n", loc); else printf ("A value of 17 element wood is found! \ n "); return 0; }
3. Creating the base code for a linear table
Custom Function Declaration section void Creatlist (sqlist *&l,elemtype a[],int N);//create linear table with array void Displist (SqList *l);// Output linear table Dislist (l) bool Listempty (sqlist *l);//Determine if empty table Listempty (L) //Implement custom Function void Creatlist (SqList *& L,elemtype a[],int N)//create linear table with array { int i; L= (SqList *) malloc (sizeof (sqlist)); for (i=0;i<n;i++) l->data[i]=a[i]; l->length=n; } void Displist (SqList *l)//Output Linear table dislist (L) { int i; for (i=0;i<l->length;i++) printf ("%d", L->data[i]); printf ("\ n"); } BOOL Listempty (sqlist *l)//Determines whether the empty table Listempty (L) { return (l->length==0); }
4. Find, length, etc. code
#include "head.h" //Implement test function int listlength (sqlist *l)//To find the length of the linear table Listlength (L) { return (l->length) ; } BOOL Getelem (sqlist *l,int i,elemtype &e)//Request a Data element value Getelem (l,i,e) { if (i<1| | I>l->length) return false; e=l->data[i-1]; return true; } int Locateelem (sqlist *l, elemtype e)//Find Locateelem by element value (l,e) { int i=0; while (I<l->length &&l->data[i]!=e) i++; if (i>=l->length) return 0; else return i+1; }
Operation Result:
(3) The remaining 4 basic operations: inserting the data element Listinsert, deleting the data element Listdelete, initializing the linear table initlist, destroying the linear table destroylist all can be done with the law.
1. Initialization and insertion of code
#include <stdio.h> #include <malloc.h> #define Maxsize typedef int ELEMTYPE; typedef struct {elemtype data[maxsize]; int length; }sqlist; Custom Function declaration section void creatlist (SqList *&l,elemtype a[],int N);//create linear table void Displist (SqList *l) with array;//output Linear table dislist (L) Boo L Listempty (sqlist *l);//Determine if NULL table Listempty (L) void Initlist (SqList *&l);//Initialize Linear table bool Listinsert (SqList *&l,int I,elemtype e);//Insert data element void Creatlist (SqList *&l,elemtype a[],int N)//create linear table with array {int i; L= (SqList *) malloc (sizeof (sqlist)); for (i=0;i<n;i++) l->data[i]=a[i]; l->length=n; } void Displist (SqList *l)//Output Linear table Dislist (L) {int i; for (i=0;i<l->length;i++) printf ("%d", l->data[i]); printf ("\ n"); } bool Listempty (SqList *l)//Determines whether the empty table Listempty (L) {return (l->length==0); } void Initlist (SqList *&l)//Initialize linear table {l= (sqlist *) malloc (sizeof (sqlist)); l->length=0; } bool Listinsert (SqLiSt *&l,int I,elemtype E)//Insert data element {int J; if (i<1| | I>L->LENGTH+1) return false; i--; for (j=l->length;j>i;j--) l->data[j]=l->data[j-1]; l->data[i]=e; l->length++; return true; } int main () {sqlist *sq; Initlist (SQ); Listinsert (Sq, 1, 5); Listinsert (Sq, 2, 3); Listinsert (Sq, 1, 4); Displist (SQ); return 0; }Operation Result:
2. Deletion and destruction of code
#include <stdio.h> #include <malloc.h> #define Maxsize typedef int ELEMTYPE; typedef struct {elemtype data[maxsize]; int length; }sqlist; Custom Function declaration section void creatlist (SqList *&l,elemtype a[],int N);//create linear table void Displist (SqList *l) with array;//output Linear table dislist (L) Boo L Listempty (sqlist *l);//Determine if NULL table Listempty (L) void Initlist (SqList *&l);//Initialize Linear table bool Listinsert (SqList *&l,int I,elemtype e);//Insert Data element bool Listdelete (sqlist *&l,int i,elemtype &e);//delete data element void Destroylist (SqList *&l); Destroy linear table void Creatlist (SqList *&l,elemtype a[],int N)//create linear table with array {int i; L= (SqList *) malloc (sizeof (sqlist)); for (i=0;i<n;i++) l->data[i]=a[i]; l->length=n; } void Displist (SqList *l)//Output Linear table Dislist (L) {int i; for (i=0;i<l->length;i++) printf ("%d", l->data[i]); printf ("\ n"); } bool Listempty (SqList *l)//Determines whether the empty table Listempty (L) {return (l->length==0); } void Initlist (sqlist*&L)//Initialize the linear table {l= (sqlist *) malloc (sizeof (sqlist)); l->length=0; } bool Listinsert (sqlist *&l,int i,elemtype e)//Insert data element {int J; if (i<1| | I>L->LENGTH+1) return false; i--; for (j=l->length;j>i;j--) l->data[j]=l->data[j-1]; l->data[i]=e; l->length++; return true; } bool Listdelete (sqlist *&l,int i,elemtype e)//delete data element {int J; if (i<1| | I>L->LENGTH-1) return false; i--; e=l->data[i]; for (j=i;j<l->length-1;j++) l->data[j]=l->data[j+1]; l->length--; return true; } void Destroylist (SqList *&l) {free (L); } int main () {sqlist *sq; Initlist (SQ); Listinsert (Sq, 1, 5); Listinsert (Sq, 2, 3); Listinsert (Sq, 1, 4); printf ("post-insert: \ n"); Displist (SQ); Listdelete (Sq, 1, 5); Listdelete (Sq, 2, 3); printf ("Deleted: \ n"); Displist (SQ); Destroylist (Sq); printf ("Destroyed: \ n"); Displist (SQ); return 0; }Operation Result:
Summary of Knowledge points:
to implement the basic operation of a linear table: (1) initializes the linear table Initlist (&L): Constructs an empty linear table L (2) destroys the linear table destroylist (&L): Releases the memory space occupied by the linear table L (3) The linear table is empty table Listempty (L): If L is empty table, return True, otherwise false (4) The length of the linear table Listlength (L): Returns the number of elements in L (5Output Linear table displist (L): When the linear table L is not empty, the sequence shows the range of the nodes in L (6) to find a data element in the specified position in the linear table L Getelem (L,I&e): Use E to return LIthe value of an element (7) Find element Locateelem (l,e): Returns the linear table L1an ordinal number equal to E, cannot find the return0. (8) Insert data element Listinsert (sqlist *&l,int i,elemtype e): Insert a new element before the first element of L the e,l length is increased by 1 and (9) Delete the data element Listdele (&L,i,& E): Delete the I data element of L, and return its value with E, the length of L minus 1, (10) Destroy linear table destroylist (SqList *&l): Release the memory space occupied by the linear table L.
Learning experience:
My goodness, the code is much, there are many files of the combination, this I do not very much, is the help of the school elder sister's strength, I will study hard.
Third week item 1-Basic operations for sequential tables