Interview Questions-details of a single-chain table of Data Structure (basic article 3)

Source: Internet
Author: User

The forward sorting of a single-chain table means that data is inserted in ascending order.

It is easy to understand the code with annotations:

// Node * insertsort (void) {int DATA = 0; struct node * head = NULL; struct node * New, * cur, * pre; while (1) {printf ("Please input the data:"); scanf ("% d", & data); If (0 = data) // input 0 to end {break;} new = (struct node *) malloc (sizeof (struct node); New-> DATA = data; // assign a new node New-> next = NULL; If (null = head) // assign the value of {head = new; continue;} to the first node in the loop ;} if (New-> data <= head-> data) // Insert the node {New-> next = head; head = before the head New; continue;} cur = head; while (New-> DATA> cur-> Data & null! = Cur-> next) {// locate the location to be inserted pre = cur; cur = cur-> next;} If (cur-> DATA> = new-> data) // The position is in the middle {// Insert the new node to pre-> next = new; New-> next = cur ;} else // end position {// Insert the new node to cur and then cur-> next = new ;}} return head ;}

To determine whether a single-chain table has a linked list, here is a simple solution. Assume that the two pointers are P1 and P2, respectively. Each cycle is one step forward for P1 and two steps forward for P2, the loop ends when P2 encounters a null pointer or two pointers are equal. If the two pointers are equal, a ring exists.

The program code is as follows:

// Determine whether a single-chain table has a loop. // If yes, start stores the bool isloop (node * head, node ** start) {node * P1 = head; node * P2 = head; If (null = head | null = head-> next) {// if the head is null or the linked list is empty, falsereturn false is returned ;} do {p1 = p1-> next; // P1 step P2 = P2-> next; // P2 two steps} while (P2 & p2-> next & P1! = P2); If (p1 = P2) {* Start = p1; // P1 is the return true;} else {return false ;}}

Below are the test functions and main functions:

Void print (node * head) {int Pos = 0; node * P = head; while (null! = P) {printf ("the % DTH node % d \ n", ++ POs, p-> data); P = p-> next ;}} int main () {bool bloop = false; node * head = insertsort (); // create and sort the linked list printf ("link sort... \ n "); print (head); printf (" isloop test ......... \ n "); node * Start = head-> next; // set the fourth node to start-> next = head-> next; // connect the loop to the second node * loopstart = NULL; bloop = isloop (Head, & loopstart); printf ("Bloop = % d \ n", bloop ); printf ("Bloop = loopstar T? % D \ n ", (loopstart = Start); Return 0 ;}

The following is the program execution result: the input is 1 6 4 5 3 2

please input the data: 1please input the data: 6please input the data: 4please input the data: 5please input the data: 3please input the data: 2please input the data: 0Link Sort...the 1th node 1the 2th node 2the 3th node 3the 4th node 4the 5th node 5the 6th node 6IsLoop test.........bLoop = 1bLoop == loopStart ? 1Press any key to continue

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.