Data structures and algorithms-Learning note 6

Source: Internet
Author: User
Tags random seed

Whole table creation, deletion of single linked list

Full table creation for single-linked lists

Ideas:

-Declares a node p and counter variable i

-Initialize an empty list L

-Let the pointer of the head node of L point to NULL, that is, create a single linked list with a leading node;

-Loop implementation Assignment and insertion

head interpolation method to build a table

Starting with an empty table, generating a new node, reading the data into the data field of the new node, and inserting the new nodes into the table header of the current list until the end.

For example, we insert the hello into the linked list

650) this.width=650; "title=" 11.jpg "src=" http://s3.51cto.com/wyfs02/M02/57/16/wKiom1SQ97eiNNkOAABxHky5m4Y505.jpg "alt=" Wkiom1sq97einnkoaabxhky5m4y505.jpg "/>

Code:

void Createlisthead (linklist *l,int e)

{

Linklist p;

int i;

Srand (Time (0));//random seed, combined with time to simulate the effect of random numbers

*l = (linklist) malloc (sizeof (Node));//malloc generates a node.

(*l)->next = NULL;

for (i = 0;i<n;i++)

{

p = (linklist) malloc (sizeof (Node));

P->data = rand ()%100+1;//rand generates a random number,%100 the number of 0~99 generated

P->next = (*l)->next;

(*l)->next = p;

}

}

Tail interpolation method

Contrary to the head interpolation method

650) this.width=650; "title=" 11.jpg "src=" http://s3.51cto.com/wyfs02/M02/57/13/wKioL1SQ-MrgnONNAABwWfAc3sE276.jpg "alt=" Wkiol1sq-mrgnonnaabwwfac3se276.jpg "/>


void Createlisthead (linklist *l,int e)

{

Linklist p,r;

int i;

Srand (Time (0));//random seed, combined with time to simulate the effect of random numbers

*l = (linklist) malloc (sizeof (Node));//malloc generates a node.

R = *l;

for (i = 0;i<n;i++)

{

p = (node *) malloc (sizeof (node));

P->data = rand ()%100+1;//rand generates a random number,%100 the number of 0~99 generated

P->next = p;

r = P; Procedure for changing the name of a node

}

P->next = NULL;

}

Whole table deletion of single linked list

Ideas:

-declaration node P and Q

-Assigns the first node to P, and the next node assigns a value to Q.

-Loop execution releases p and Q assignment to P's operation

Code:

Status Clearlist (linklist *l)

{

Linklist p,q;

p= (*l)->next;

while (p)

{

Q = p->next;

Free (p);

P =q;

}

(*l)->next = NULL;

return OK;

}

Advantages and disadvantages of single linked list and sequential table storage structure


Storage mode
Time performance
Space performance
Sequential storage

Successive storage units are sequentially stored

Data Elements for linear tables

Insertion: O (1)

Insert and delete: Average move

The length of the table is half the element, the time

is O (n)

Easy Overflow
Single linked list

Use a set of arbitrary storage units

Storing elements

Insert: O (N)

Insert and delete: Calculate location

Pointer, time only O (1)

No need to allocate storage space

Sequential storage structure should be used if linear tables are frequently searched, rarely inserted and deleted.

If linear tables are frequently inserted and deleted, it is advisable to use a single linked list structure

Data structures and algorithms-Learning note 6

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.