Data structures and algorithms-Learning Note 4

Source: Internet
Author: User

Two types of storage structures for linear tables-sequential storage

Sequential storage Structure Code:

#define MAXSIZE 20

typedef int ELEMTYPE;

typedef struct

{

Elemtype Data[maxsize];

int length;

}sqlist;

The structure package requires three properties:

The starting position of the storage space, array data, where it is stored is the storage location of the linear table storage space

Maximum storage capacity for linear tables: array length maxsize

Current length of linear table: length

Address calculation method:

Assuming that Elemtype occupies a C storage unit, the relationship between the i+1 data element and the first data element in your linear table is

LOC (ai+1) =loc (AI) +c

Code snippet:

650) this.width=650; "title=" 11.jpg "src=" http://s3.51cto.com/wyfs02/M00/57/12/wKioL1SQ59XSLnLwAADqcU0PYyc212.jpg "alt=" Wkiol1sq59xslnlwaadqcu0pyyc212.jpg "/>

Insert operation:

Ideas:

① the insertion position is unreasonable and throws an exception

② linear table length is greater than or equal to the array length, then run out of exception or dynamically increase the array capacity

③ moves forward from the last element to the I position, moving each of them backwards one position

④ insert element into position I

⑤ Linear gauge Length +1

Implementation code:

Status Listinsert (sqlist *l,int i,elemtype e)

{

int k;

if (l->length = = MAXSIZE)//sequential linear table is full

{

return ERROR;

}

if (i<1 | | i>l->length+1)//When I is not in range

{

return ERROR;

}

if (i<=l->length)//If the inserted data is not at the end of the table

{

The data element moves backward one after the position is inserted

for (K = l->length-1;k>=i-1;k--)

{

L->DATA[K+1] = l->data[k];

}

}

L->DATA[I-1] = e;//Inserts a new element

l->length++;


return OK;

}


Delete operation:

Ideas:

① If the delete location is unreasonable, throw an exception

② remove Element

③ the position of the last element from the location of the deleted element, moving them all forward one position, respectively.

④ Table Length-1

Code implementation:

Status Listinsert (sqlist *l,int i,elemtype e)

{

int k;

if (L->length = = 0)//Determine if the table length is empty

{

return ERROR;

}

if (i<1 | | i>l->length)//Determine if the delete location is correct

{

return ERROR;

}

*e = l->data[i-1];

if (i<l->length)//Shift

{

for (K =i;k<l->length;k++)

{

L->DATA[K-1] = l->data[k];

}

}

l->length--;//linear table Length-1


return OK;

}

Time complexity: O (N)

Sequential storage structure of linear tables, where the time complexity is O (1), regardless of the location of the data being stored or read. When inserting or deleting, the time complexity is O (n), indicating that it is more suitable for the number of elements stable, not often inserting and deleting elements.

Advantages:

① no need to add additional storage space to represent the logical relationship between elements in a table

② can quickly access elements from anywhere in the table

Disadvantages:

① Insert and delete operations require a large number of elements to be moved

② the capacity of the storage space is difficult to determine when the linear table length varies greatly


Data structures and algorithms-Learning Note 4

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.