Single-chain table operation

Source: Internet
Author: User

1. Create a single-chain table
Assume that the data type of a node in a linear table is a character. We enter these two nodes one by one and use the linefeed '\ n' as the terminator of the input condition. There are two common methods to dynamically create a single-chain table:

(1) Table creation using the header plug-in method
① Algorithm ideas
From an empty table, read data repeatedly, generate a new node, store the read data in the data field of the new node, and then insert the new node into the table header of the current linked list until the end mark is read.

For more information, see animation demonstration]
Note:
The node order of the linked list generated by this method is opposite to the input order.

② Specific Algorithm Implementation

1 linklist creatlistf (void)
2 {// return the header pointer of a single-chain table
3 char ch;
4 linklist head; // header pointer
5 listnode * s; // working pointer
6 head = NULL; // The start of the linked list is null.
7 CH = getchar (); // read 1st characters
8 while (Ch! = '\ N '){
9 S = (listnode *) malloc (sizeof (listnode); // generate a new node
10 s-> DATA = CH; // put the read data into the data domain of the new node
11 S-> next = head;
12 head = s;
13 CH = getchar (); // read the next character
14}
15 return head;
16}

 

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.