Reverse Sequence of a single-chain table

Source: Internet
Author: User

It's very simple. Just practice it.

1 // the reverse order of a single-chain table is actually very easy, that is, to point the pointer to a change, note several issues 2 // (1) if it is an element, it is not a header node, directly return 3 // (2) Note that the header node should handle the problem independently. 4 # include <iostream> 5 using namespace STD; 6 7 typedef struct linknode {8 int val; 9 linknode * Next; 10} node, * List; // Adding typedef indicates that node and list are of the type. Otherwise, it is only a struct variable 11 12 Void reverse (list head) 13 {14 if (Head = NULL | head-> next = NULL) 15 return; 16 node * P = head-> next; 17 node * q = p-> next; 18 node * T; 19 P-> next = NULL; 20 while (Q! = NULL) 21 {22 t = Q-> next; 23 Q-> next = P; 24 p = Q; 25 q = T; 26} 27 head-> next = P; 28} 29 List createlist () 30 {31 int data; 32 list head = new node; 33 head-> val = 0; 34 head-> next = NULL; 35 node * P = head; 36 while (CIN> data) 37 {38 node * TMP = new node; 39 TMP-> val = data; 40 TMP-> next = NULL; 41 P-> next = TMP; 42 p = TMP; 43} 44 return head; 45} 46 47 int main () 48 {49 node * head = createlist (); 50 reverse (head); 51 node * P = head-> next; 52 while (P) 53 {54 node * q = p-> next; 55 cout <p-> Val <""; 56 Delete P; 57 P = Q; 58} 59 cout <Endl; 60 system ("pause"); 61}

 

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.