Data structure and algorithm learning (i.)

Source: Internet
Author: User

Operation of linear tables:

Initlist (*L): Initializes the operation, creating an empty linear table for L;

Listeempty (L): Determine if L is an empty table, if the linear table is empty, then return ture, otherwise return false;

Clearlist (*L): Clears the linear table;

Getelem (l,i,*e): Returns the value of the first position element in the linear table L to E;

Locateelem (L,e): Finds an element in linear table L equal to the given value E, if the lookup succeeds, the ordinal of the returned element in L indicates success, otherwise, 0 indicates failure;

Listinsert (*l,i,e): Inserts the element value in the linear table L at the first position E;

Listdetele (*l,i,*e): Removes the I position element in linear table L and returns its value with E;

Listlength (L): Returns the number of elements in the linear table L;

union.c//will A=aub, merge elements in B into the code snippet in a:

void unionl (list *la, list Lb)

{int La_len, lb_len, I;

Elemtype e;//same Element E

La_len=listlength (*la);

Lb_len=listlength (LB);

for (i=1;i<=lb_len;i++);

{Getelem (lb,i,&e);

if (! Locateelem (La,e))

{Liseinsert (la,la_len++,e);

}

}

Sequential storage structure of linear tables: initial addresses are opened, and element values are stored sequentially in successive addresses.

#define MAXSIZE 20

typedef int ELEMTYPE

typedef struct

{Elemtype data[maxsize];

int length//linear table current lengths

}sqlist;

Sequential storage Structure Three properties: (1) The starting position of the storage space, the array data, (2) The length of the array MaxSize; (3) The current length of the linear table.

getelem.c//returns the value of the I position element in the linear table L.

#define OK 1

#define ERROR 0

#define TURE 1

#define FALSE 0

typedef int STATUS

Status is the type of function whose value is the status code of the function result, such as OK, etc.

Initial conditions, sequential linear table L already exist, 1<=i<=listlength (L)

The result of the operation, using E to return the value of the first element in the linear table L

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

{if (l.length==0| | i<1| | I>l.length)

Return ERROR

*E=DATA[I-1];

return 0K;

}

Linear table Insertion algorithm:

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

{int k;

if (l->length>maxsize)

{return ERROR;}

if (i<1| | I>L->LENGTH+1)

{return ERROR;}

if (i<l->length)

{//To move the data behind the inserted element backwards

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

{l->data[k+1]=l->data[k];}

}

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

l->length++;

return OK;

}

Linear table Deletion algorithm:

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

{int k;

if (l->length==0)

{return ERROR;}

if (i<1| | I>l->length)

{return ERROR;}

if (1<=i<l->length)//should be used for loop

{for (k=i;k<l->length;k++) {l->data[i-1]=l->data[i];}}

l->length--;

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

return OK;}

Time complexity analysis for linear table insert and delete operations:

In the worst case, the first element is inserted and deleted, each element is moved, and the time complexity is O (n);

In the best case, the last element is inserted and deleted, no other elements need to be moved, and the time Complexity is O (1);

Average condition, O (n-1/2) =o (0);

In summary, the access operation time complexity of sequential linear table is O (1), and both insert and delete operations are O (n);

Linear table Sequential storage structure:

Advantage: There is no need to add additional storage space to represent the logical relationships of elements in the table, and you can quickly access any element in the table

Disadvantage: Insert or delete operations need to move a large number of elements, when the linear table length varies greatly, it is difficult to determine the capacity of storage space, easy to create fragmentation of storage space

Data structure and algorithm learning (i.)

Related Article

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.