Code for sequential storage and chained storage of linear structures (I)

Source: Internet
Author: User

There are two main methods for physical storage of linear structures: sequential storage and chained storage. A data element in a linear structure has no significance. Only when all data elements constitute a whole can it make sense. In order to have a generous management of this whole, we should establish the headers about this whole, and the types of data elements.The code for writing data structures in the future will be implemented based on the C ++ language.

Code for sequential storage of linear structures (content of C ++ is omitted)

// Load the header file
# Include <iostream. h>
# Include <stdlib. h>

// Macro definition
# Define maxsize 10
# Define add 10

// Header data type
Typedef struct
{
Int * List; // The first address used to store the array.
Int curlen; // The number of data records in the current table (array)
Int maxsize; // The maximum number of data records that can be stored in this table
} Arrlist;

// Initialize a linear table
Void inilist (arrlist & L)
{
L. List = new int [maxsize]; // The list of this table stores the first address of an array for storing data.
If (! L. List)
{
Cout <"An error occurred while initializing the linear table! \ N ";
Return; // The if statement determines whether the previous statement has been correctly executed
}
L. curlen = 0; // No data is stored in this table.
L. maxsize = maxsize; // This table can store up to 10 data records.
}

// Input n elements consecutively
Void creatlist (arrlist & L, int N)
{
Int I;
Int E;
For (I = 0; I <n; I ++)
{
Cout <"Enter the" <I + 1 <"number :";
Cin> E;
L. list [I] = E;
}
L. curlen = N;
Cout <"your operation has been completed! \ N ";
}

// Display the elements in the table
Void display (arrlist L)
{
Int I;
For (I = 0; I <L. curlen; I ++)
{
If (I + 1) % 5 = 0)
Cout <"\ n ";
Cout <"L [" <I <"]" <"=" <L. list [I] <"";
}
Cout <"\ n your operation has been completed! \ N ";
}

// Insert an element
Void inlist (arrlist & L, int I, int e)
{
If (I <1 | I> L. curlen + 1)
{
Cout <"Your Insertion Location is incorrect! \ N ";
Return;
}
If (L. curlen> = L. maxsize) // If the table cannot insert data, the size of an array is increased.
{
Int * relist;
Int Q;
Relist = new int [L. maxsize + add];
For (q = 0; q <L. maxsize; q ++)
Relist [Q] = L. list [Q];
Free (L. List );
L. List = relist;
}
Int J;
For (j = L. curlen; j> = I; j --) // causes the original data to be inserted to a unit backward.
{
L. list [J] = L. list [J-1];
}
L. list [I-1] = E;
L. curlen ++;
Cout <"your operation has been completed successfully! ";
}

// Delete an element
Void deletelist (arrlist & L, int I)
{
If (I <1) | (I> L. curlen ))
{
Cout <"the location you entered is incorrect! \ N ";
Return;
}
For (++ I; I <= L. curlen; ++ I)
L. list [I-1] = L. list [I];
-- L. curlen;
Cout <"your operation has been completed! \ N ";
}

Void main ()
{
Arrlist;
Int m;
Int N;
Inilist ();
Int option;
Option =-1;
While (option! = 0)
{
Cout <"\ n ";
Cout <"Menu \ n ";
Cout <"0: Exit \ n ";
Cout <"1: initial continuous input element \ n ";
Cout <"2: insert element \ n ";
Cout <"3: delete element \ n ";
Cout <"4: show the element \ n" in the linear table ";
Cout <"select your operation :";
Cin> option;
Switch (option)
{
Case 0:
Option = 0;
Break;
Case 1:
Cout <"Enter the number of consecutive input data :";
Cin> N;
Creatlist (A, N );
Break;
Case 2:
Cout <"Enter the position of the inserted element :";
Cin> N;
Cout <"Enter the value of the inserted element :";
Cin> m;
Inlist (A, n, m );
Break;
Case 3:
Cout <"Enter the location of the deletion element :";
Cin> N;
Deletelist (A, N );
Break;
Case 4:
Display ();
Break;
Default:
Cout <"enter the correct options ";
Break;
}
}
}

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.