Create a sequence table in the data structure and a sequence of Data Structure Construction

Source: Internet
Author: User

Create a sequence table in the data structure and a sequence of Data Structure Construction

An ordered table refers to an array in which data elements continuously allocate addresses in the memory. Since the pointer cannot specify the length of the array, no error is reported during compilation. All the arrays represent an ordered table:

The sequence table is represented in the C language as follows:

<span style="font-family: Arial, Helvetica, sans-serif;">  #define OK 1</span>
  #define ERROR -1  typedef int elem_type;
Typedef int Statue; // int Arrylength; typedef struct sqlist {elem_type * Arry; int Arrylength;} Sqlist; // create an empty table Statue Create_Sqlist (Sqlist * & S) {// S-> Arry = (elem_type *) malloc (MaxSize * sizeof (elem_type); // L = (sqlist *) malloc (sizeof (sqlist )); // L-> data = (char *) malloc (maxsize); S = (Sqlist *) malloc (sizeof (Sqlist); S-> Arry = (elem_type *) malloc (MaxSize); if (! S-> Arry) {return ERROR;} else {S-> Arrylength = 0; return OK ;}// the sequence table assigns the initial value void Init_Sqlist (Sqlist * & S) {int I; for (I = 0; I <20; I ++) {S-> Arry [I] = rand () % 100; s-> Arrylength ++ ;}} // Add an element m Statue Insert_Sqlist (Sqlist * & S, int I, int InsertNum) to position I) {if (I <= 0 & I> S-> Arrylength) return ERROR; else {int j; for (j = 0; j <= S-> Arrylength-I; j ++) S-> Arry [S-> Arrylength-j] = S-> Arry [S-> Arrylength-1-j]; // I move the element back One S-> Arry [I-1] = InsertNum; S-> Arrylength ++; return OK ;}// Delete Statue Delete_Sqlist (Sqlist * & S, int I) {if (I <= 0 & I> S-> Arrylength) return ERROR; else {int j; for (j = 0; j <= S-> Arrylength-I; j ++) S-> Arry [I-1 + j] = S-> Arry [I + j]; S-> Arrylength --; return OK ;}} // Delete the element void DeleteX_Sqlist (Sqlist * & S, int x) {int I; for (I = 0; I <S-> Arrylength; I ++) {if (x = S-> Arry [I]) {// Delete_Sqlist (* S, I); int j; fo R (j = 0; j <= S-> Arrylength-I; j ++) s-> Arry [I + j] = S-> Arry [I + j + 1]; S-> Arrylength --;}}} // print function void print (Sqlist * & S) {int m; for (m = 0; m <S-> Arrylength; m ++) {cout <setw (4) <S-> Arry [m]; if (m + 1) % 4 = 0) cout <endl ;}} int main () {Sqlist * p1; Create_Sqlist (p1); cout <"create sequence table is OK" <endl; Init_Sqlist (p1 ); cout <"the initialization sequence table is OK, and the data is as follows:" <endl; print (p1); int I, InsertNum; cout <"two inputs are as follows :"; cin> I; cin> InsertNum; cout <"the number of inserts is as follows: Insert a number after <I <"row" <InsertNum <. The structure is shown as follows: "<endl; Insert_Sqlist (p1, I, InsertNum); print (p1); int k; cout <endl; cout <: "; cin> k; cout <" the number of delete operations are as follows: "<endl; cout <" want to delete the number "<k <" the number is displayed as follows: "<endl; Delete_Sqlist (p1, k); print (p1); int j; cout <" enter a number "<endl; cin> j; cout <"the operation to delete a specified number is as follows:" <j <"The result is as follows:" <endl; DeleteX_Sqlist (p1, j ); print (p1); while (1); return 0 ;}
The result is as follows:


Analyze and compare the differences between the following code segments:

Section A -- code for creating an empty table without Bugs:

Typedef struct sqlist {elem_type * Arry; int Arrylength;} Sqlist; // create an empty table Statue Create_Sqlist (Sqlist * & S) {S = (Sqlist *) malloc (sizeof (Sqlist); S-> Arry = (elem_type *) malloc (MaxSize );}
Section B -- code for creating an empty table with a bug:

Typedef struct sqlist {elem_type Arry [MaxSize]; int Arrylength;} Sqlist; // create an empty table Statue Create_Sqlist (Sqlist * S) {S-> Arry = (elem_type *) malloc (MaxSize * sizeof (elem_type ));}






How to write the sequence table in the data structure ???

How to Write a sequence table?

You can create a Fixed Array or apply for a continuous memory space.

Enter the data you want to create in sequence to the corresponding position of the array.

In this way, you can sort the ordered table and operate the output.

Create, search, and semi-query sequence tables in the Data Structure

// If you have used vc for debugging, you can raise any questions.
# Include <stdio. h>
# Define list size 100
Typedef struct
{
Int data [listsize];
Int length;
} Sqlist; // sequence table Type
Void createtsqlist (sqlist & L, int a [], int n) // create an ordered table with an Array
{
L. length = 0;
For (int I = 0; I <n; I ++)
{
L. data [L. length ++] = a [I];
}
}

Void findvalue (sqlist L, int x) // you can check whether x is in the sequence table.
{
For (int I = 0; I <L. length; I ++)
{
If (L. data [I] = x)
{
Printf ("% d is the % d element \ n", x, I + 1); return;
}
}
Printf ("% d not in the sequence table \ n", x );
}
Void search_bin (sqlist L, int x) // semi-query an ordered table
{
Int low = 1; int high = L. length; int mid;
While (low <= high)
{
Mid = (low + high)/2;
If (x = L. data [mid])
{
Printf ("% d is the % d element \ n", x, mid + 1); return;
}
Else if (x <L. data [mid]) high = mid-1;
Else low = mid + 1;
}
Printf ("% d not in the sequence table \ n", x );
}
Void main ()
{
Int a [10] = {, 34 };
Int B [10] = {1, 2, 4, 5, 6, 9, 14, 19,23}; // ensure the order of elements in B
Sqlist L1, L2; // L2 is created as an ordered table.
Createtsqlist (L1, a, 10 );
Findvalue (L1, 45); // you can check whether 45 can be replaced by another number in the table.
Createtsqlist (L2, B, 10 );
Search_bin (L2, 14); // query whether 14 can be replaced by another number in the table
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.