The C language code implements linear and sequential tables, and processes integer data and linear integer data.

Source: Internet
Author: User

The C language code implements linear and sequential tables, and processes integer data and linear integer data.

# Include

# Include // conio is short for Console Input/Output (Console Input/Output), which defines functions for data Input and Output through the Console,

// Operations generated by the keyboard, such as the getch () function.

# Define MAX 30 // defines the maximum length of a linear table

Enum BOOL {False, True}; // defines the BOOL type.

Typedef struct {

Int elem [MAX]; // linear table of the int array type

Int length; // length indicates the length of the current linear table.

} Sqlist; // sqlist is the alias of struct.

Void initial (sqlist &); // initialize linear table 1.

BOOL insert (sqlist & v, int loc, int intt); // insert an element in a linear table (reference a linear table, location, int type variable) 2.

BOOL del (sqlist &, int weizhi, int &); // delete an element from a linear table (reference a linear table, position, return the element value of the deleted element) 3.

// 1.2.3. All linear tables must be operated. Therefore, linear tables are referenced. &.

Int locate (sqlist, int elem); // locate an element in a linear table (linear table, the element to be searched)

Void print (sqlist); // display all elements in a linear table (print all elements in a linear table)

Void main () // (main function)

{

Sqlist S; // S is a linear table (create a sequence table structure object named S .)

Int loc, flag = 1; // defines the integer position variable and the flag variable (used to enter the while loop after the variable is marked)

Char j; // used to select a character for the condition after swich.

Int intt; // (int type variable element to be inserted)

BOOL temp; // (temp is used to record insertion and delete the return value of whether the function is successfully inserted)

Printf ("this program is used to implement linear tables with sequential structures. \ N ");

Printf ("allows you to search, insert, delete, and perform other operations. \ N ");

Initial (S); // initialize the linear table (the called function is initialized .)

While (flag)

{

Printf ("select \ n ");

Printf ("1. Show all elements \ n ");

Printf ("2. Insert an element \ n ");

Printf ("3. delete an element \ n ");

Printf ("4. Find an element \ n ");

Printf ("5. Exit program \ n ");

Scanf ("% c", & j );

Switch (j)

{

Case '1': print (S); break; // display all elements

Case '2 ':

{

Printf ("Enter the element to be inserted (one character) and insert position: \ n ");

Printf ("Format: character, location; for example: a, 2 \ n ");

Scanf ("% d, % d", & intt, & loc); // enter the element to be inserted and the position to insert.

Temp = insert (S, loc, intt); // insert

If (temp = False)

Printf ("insertion failed! \ N "); // insertion failed

Else

{

Printf ("inserted successfully! \ N ");

Print (S );

} // Insert successful

Break;

}

Case '3': {printf ("Enter the location of the element to be deleted :");

Scanf ("% d", & loc); // enter the location of the element to be deleted

Temp = del (S, loc, intt); // Delete (reference a linear table, location, return the element value of the deleted element)

If (temp = True)

Printf ("an element is deleted: % d \ n", intt); // deletion successful

Else

Printf ("This element does not exist! \ N "); // deletion failed

Print (S );

Break;

Case '4 ':{

Printf ("Enter the element to be searched :");

Scanf ("% d", & intt); // enter the element to be searched

Loc = locate (S, intt); // locate

If (loc! =-1)

Printf ("Location of the element: % d \ n", loc + 1); // display the location of the element

Else

Printf ("% d does not exist! \ N ", intt); // The current element does not exist

Break;

}

Case '5 ':{

Flag = 0;

Printf ("the program ends. Press any key to exit! \ N ");}

}

}

Getch ();

}

}

Void initial (sqlist & v)

{

// Initialize the linear table

Int I;

Printf ("Enter the length of the initial linear table: n ="); // enter the length of the linear table during initialization.

Scanf ("% d", & v. length );

Printf ("enter each element (integer) from 1 to % d, for example, 1 2 3 4 \ n", v. length );

Getchar ();

For (I = 0; I scanf ("% d", & v. elem [I]); // each element of the input linear table

}

BOOL insert (sqlist & v, int loc, int intt)

{

// Insert an element. If the element is successfully inserted, True is returned. If the element fails, False is returned.

Int I;

If (loc <1) | (loc> v. length + 1 ))

{

Printf ("the Insertion Location is unreasonable! \ N "); // The location is unreasonable

Return False;

}

Else if (v. length> = MAX) // The linear table is full.

{

Printf ("the linear table is full! \ N ");

Return False;

}

Else

{

For (I = v. length-1; I> = loc-1; I --)

V. elem [I + 1] = v. elem [I]; // The following elements are moved backward.

V. elem [loc-1] = intt; // insert element

V. length ++; // linear table length plus one

Return True;

}

}

BOOL del (sqlist & v, int loc, int & intt)

{

// If an element is deleted successfully, True is returned, and the value of this element is returned with ch. If the element fails, False is returned.

Int j;

If (loc <1 | loc> v. length) // The deletion location is invalid.

Return False;

Else {

Intt = v. elem [loc-1]; // ch gets the element value

For (j = loc-1; j v. elem [j] = v. elem [j + 1]; // The elements after them move forward sequentially

V. length --; // the length of a linear table minus one.

Return True;

}

}

Int locate (sqlist v, int intt)

{

// Locate the int element in the linear table, and return its position.-1 is returned if no value is returned.

Int I = 0;

While (I ++ I; // The current position moves backward until it is found.

If (v. elem [I] = intt) // locate the current element

Return I;

Else return (-1 );

}

Void print (sqlist v) // display all elements of the current linear table

{

Int I;

For (I = 0; I printf ("% d", v. elem [I]);

Printf ("\ n ");

}

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.