Static tables using Arrays

Source: Internet
Author: User

1. Create, insert, delete, and search an ordered linear table.

2. Specific requirements:

(1) Create an ordered table containing n data elements;

(2) you can insert a data element at any legal position of a linear table;

(3) You can delete a data element of a linear table at any legal position I;

(4) You can delete all the data elements with E values in a linear table and delete several such elements;

(5) You can find the elements at position I;

(6) You can find the first position of an element with the value of E in a linear table;

(7) The values of each element in the table can be output;

(8) The length of the sequence table can be output (that is, the number of data elements );

Create a project

Header files: <sqlist. h>

# Ifndef sqlist_h # define maxsize 30 // defines the maximum length of the linear table typedef int elemtype; // The linear table contains the integer element typedef struct {elemtype ELEM [maxsize]; // linear table int length; // Length indicates the length of the current linear table} sqlist; void initial (sqlist &); // initialize the linear table void print (sqlist &); // output function bool insert (sqlist &, Int, INT); // Insert the bool del element (sqlist &, Int, Int &); // Delete the int locate (sqlist, INT); // search # endif

Sourse flies:

Main function file: <main. cpp>

# Include <stdio. h> # include <sqlist. h> void main () {sqlist s; // a linear table int Loc, E, flag = 1; char J; bool temp; initial (s ); // initialize the linear table while (FLAG) {printf ("select \ n"); printf ("1. show all elements 2. insert an element 3. delete an element 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 location: \ n"); printf ("Format: location, integer; for example: 1,-2 \ n "); scanf (" % d, % d ", & Loc, & E); // enter The inserted element and the inserted position temp = insert (S, Loc, e); // insert if (temp = false) printf ("insertion failed! \ N "); // insertion failed else {printf (" insertion successful! \ N "); print (s) ;}// the inserted break is successful;} 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, e ); // Delete if (temp = true) printf ("% d \ n", e); // else printf ("This element does not exist! \ N "); // print (s); break;} case '4': {printf (" Enter the element to search :"); scanf ("% d", & E); // enter the element loc = locate (S, e) to be searched; // 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 ", e); // The current element does not have a break;} default: Flag = 0; printf (" the program ends. Press any key to exit! \ N ") ;}getchar ();}}

Function implementation: <sqlist. cpp>

# Include "stdio. H "# include" sqlist. H "Void initial (sqlist & V) // initialize the linear table {int I; printf (" Enter the initial linear table length: N = "); // scanf ("% d", & v. length); printf ("Please input each element (integer) \ n from 1 to % d", V. length); getchar (); for (I = 0; I <v. length; I ++) scanf ("% d", & v. ELEM [I]); // each element in the input linear table} void print (sqlist & V) {int I; for (I = 0; I <v. length; I ++) printf ("% d \ n", V. ELEM [I]);} bool insert (sqlist & V, int Loc, int e) // 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 "); // return false if (V. length> = maxsize) // 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 after v. ELEM [loc-1] = E; // insert element V. length ++; // the length of a linear table plus return true ;}} bool del (sqlist & V, int Loc, Int & E) {// returns true if an element is deleted successfully, return the value of this element with CH. If (loc <1 | loc> v. length) return false; // The deletion location is unreasonable. else {e = v. ELEM [loc-1]; // e obtain the element value for (j = loc-1; j <= v. length-1; j ++) v. ELEM [J] = v. ELEM [J + 1]; // The subsequent elements move forward to V in sequence. length --; // linear Return true if the table length is reduced by one;} int locate (sqlist V, int e) {// query the location of CH in the linear table, and return its location, -1 int I = 0; while (I <v. length & v. ELEM [I]! = E) I ++; // The current position is moved back until it is found. If (v. ELEM [I] = e) return I; // locate the current element else return (-1 );}


Generally, dynamic data storage is used. Array, you can find the corresponding data by directly providing the subscript. The subscript is zero and no data is stored.


Static tables using Arrays

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: 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.