Lookup, insert, delete, merge operations for sequential tables and their pros and cons

Source: Internet
Author: User

The lookup, insert, delete, merge operation of sequential table, implement related code in C + +:

#include <iostream>

using namespace Std;


Defining a linear table sequential storage structure

#define MAXSIZE 100//Linear table maximum length

typedef struct

{

Linear table occupies array space

int elem[maxsize];

Records the position (subscript value) of the last element in the linear table in the array elem[], with an empty table of 1

int last;

}seqlist;



Find operations by content for sequential tables

Finds an element equal to E in the Order table L, if l.elem[i]=e, finds the element and returns the i+1, if not found, returns-1

int Locate (seqlist L, int e)

{

I is the scanning counter, the initial value is 0, that is, starting from the first element to compare

int i = 0;

Sequentially scans the table until the element with the value E is found or scanned to the end of the table and not found

while ((I<=l.last) && (l.elem[i]!=e))

{

i++;

}

if (i <= l.last)

{

return (i + 1);//If an element with the value E is found, its ordinal is returned

}

Else

{

return-1;//if not found, returns an empty ordinal

}

}


Insert operations on sequential tables

#define OK 1

#define ERROR 0

Insert an element before the first data element in the order table L e,n elements have n+1 insertion position, 1<=i<=l->last + 2

int Inslist (seqlist *l, int i, int e)

{

int k = 0;

if ((i<1) | | | (I>l->last + 2)) Determine if the insertion position is valid

{

cout << "Insertion position I value is not legal! "<< Endl;

return (ERROR);

}

if (l->last >= MAXSIZE-1)

{

cout << "The table is full and cannot be inserted!" "<< Endl;

return (ERROR);

}

for (k = l->last; k >= i-1; k--)//move position for insertion element

{

L->elem[k + 1] = l->elem[k];

}

L->elem[i-1] = e;

l->last++;

return (OK);

}


Delete operations for sequential tables

Delete the I data element in the order table L and return its value with the pointer parameter e, 1<=i<=l->last + 1

int Dellist (seqlist *l, int i, int *e)

{

int k = 0;

if ((i<1) | | | (I>l->last + 1)) Determine if the delete location is legitimate

{

cout << "Delete location illegal! "<< Endl;

return (ERROR);

}

*e = l->elem[i-1];//The deleted element is stored in the variable pointed to by E

for (k = i; k <= l->last; k++)//move the following elements forward in turn

{

L->ELEM[K-1] = l->elem[k];

}

l->last--;

return (OK);

}

int main ()

{

//

System ("pause");

return 0;

}

Example: There are two sequential table la,lb, whose elements are non-descending ordered arrangement, the writing algorithm merges them into a sequential table LC, which requires the LC is also non-descending ordered arrangement.

Solution: The algorithm is as follows:

Merging operations of linear tables

void Mergelist (Seqlist *la, Seqlist *lb, Seqlist *LC)

{

int i = 0, j = 0, k = 0;

while ((I <= La->last) && (J <= Lb->last))

{

if (La->elem[i] <= lb->elem[j])

{

Lc->elem[k] = la->elem[i];

i++;

k++;

}

Else

{

Lc->elem[k] = lb->elem[j];

j + +;

k++;

}

}

while (i <= la->last)//When table LA has the remaining elements, the remaining elements of table La are assigned to the table LC

{

Lc->elem[k] = la->elem[i];

i++;

k++;

}

while (J <= lb->last)//When table lb has the remaining elements, the remaining elements of the table lb are assigned to the table LC

{

Lc->elem[k] = lb->elem[j];

j + +;

k++;

}

Lc->last = la->last + lb->last + 1;

}


Advantages of the Sequential table: 1. There is no need to add additional storage space to represent the logical relationship between nodes;

2. It is convenient to randomly access any element in the table.

Cons: 1. Inserting or deleting operations is inconvenient;

2. Since sequential tables occupy contiguous storage space, storage allocations can only be statically allocated in advance, and it is difficult to determine the appropriate storage size when the length of the table changes significantly.


This article is from the "Rock Owl" blog, please be sure to keep this source http://yaoyaolx.blog.51cto.com/10732111/1768221

Lookup, insert, delete, merge operations for sequential tables and their pros and cons

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.