Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and beauty - Realization of merging two linear tables by sequential storage
Realization of merging two linear tables by sequential storage
Merges the elements in two linear tables, keeping only one of the same elements. The code is as follows:

# Miscellaneous note once

# Define list size 200

# include & ltiostream & gt

Use namespace std

Typedef int data type;

Typedef structure

{

Data type list [listsize];

Int length;

} SeqList

//Initialize the linear table

void InitList(SeqList *L)

{

l-& gt; Length = 0; //Set the length of the linear table to 0.

}

//Determines whether the linear table is empty, and returns 1 if the linear table is empty, otherwise returns 0.

int ListEmpty(SeqList L)

{

If (length == 0)

Returns1;

other

Returns 0;

}

//Search by serial number

int GetElem(SeqList L,int i,DataType *e)

/* Find the I-th element in the linear table. If it is successful, return E, and return 1 to indicate success; otherwise, return-1 to indicate failure */

{

If (I <1|| I >; Length)

return- 1;

other

* e = l . list[I- 1];

Returns1;

}

//Search by content

Int LocateElem (sequence list l, data type e)

{

int I;

for(I = 0; I < length; I++)/* Compare with E from the first element */

If (L.list[i] == e) /* If there is an element equal to e */

Returns I+1; ? /* Returns the serial number of the element in the linear table */

Returns 0; ? /* Otherwise, return 0 */

}

//Insert operation

Int insertList(SeqList *L, int I, data type e)

/* Insert the element E at the ith position in the sequence table, and return 1 if it is successfully inserted,-1 if it is illegally inserted, and return 0 if the sequence table is full. */

{

int j;

If (I <1|| I >; l-& gt; Length+ 1)/* Before inserting an element, judge whether the insertion position is legal */

{

Cout & lt& lt "insertion position"

return- 1; ?

}

else if(L-& gt; Length & gt=ListSize)/* Before inserting an element, judge whether the sequence table is full and the element cannot be inserted */

{

Cout & lt& lt "sequence table is full. Cannot insert element."<& ltendl

Returns 0;

}

other

{

for(j = L-& gt; Length; j & gt= I; j -)

/* Move the elements after the ith position backward in order */

{

l-& gt; list[j]= L-& gt; List [j-1];

}

l-& gt; list[I- 1]= e;

l-& gt; Length = L-& gt;; Length+1;

Returns1;

}

}

/* Delete operation, delete the i-th element */

int DeleteList(SeqList *L,int i,DataType *e)

{

int j;

If (l->; Length & lt=0)

{

Cout & lt& lt sequence table is empty, and<& ltendl cannot be deleted.

Returns 0;

}

else if(I & lt; 1 | | I & gt; l-& gt; Length)

{

Cout & lt& lt "Delete location is inappropriate!" & lt& ltendl

return- 1;

}

other

{

* e = L-& gt; list[I- 1];

for(j = I; j & lt= L-& gt; Length-1; j++)

{

l-& gt; list[j- 1]= L-& gt; list[j];

}

l-& gt; Length = L-& gt;; Length-1;

Returns1;

}

}

/* Find the length of the linear table */

int ListLength(SeqList L)

{

Returns L.length

}

/* Clear the sequence table */

Void clear list (SeqList *L)

{

l-& gt; Length = 0;

}

Extended data

The sequential storage structure of linear tables is to find a space in memory, occupy a certain memory space by occupying space, and then store data elements of the same data type in this space in turn.

Because every data element in the linear table is of the same type, C language (as well as other languages) uses one-dimensional array to realize the sequential storage structure, that is, the first data element is stored in the position marked as 0 in the array, and then the adjacent elements in the linear table are stored in the adjacent positions in the array.

Properties of sequential storage

Three attributes:

1. Starting location of storage space: array data, and its storage location is the storage location of storage space.

2. Maximum storage capacity of linear table: the length of array MaxSize.

3. Current length of linear table: length.