Transpose a single-chain table

Source: Internet
Author: User

Single-chain table conversion is very familiar, here is just to practice the chain table operation, there are also a few points to note, if it is recursive to solve this problem, if it is adjacent two transpose how to deal.

1. Transpose non-recursion of a single-chain table

 
Void reverse (struct node ** list) {struct node * currentp = * List; struct node * pleft = NULL; struct node * pright = NULL; while (currentp! = NULL) {pright = currentp-> next; currentp-> next = pleft; pleft = currentp; currentp = pright;} * List = pleft ;}

2. Transpose recursion of a single-chain table

 
Struct node * recursive_reverse (struct node * List) {struct node * head = List; struct node * P = r_reverse (list); head-> next = NULL; return P ;} struct node * r_reverse (struct node * List) {If (null = List | null = List-> next) return list; struct node * P = r_reverse (list-> next); List-> next = List; return P ;}

3. Transpose Adjacent Elements in a single-link table (non-recursive)

Struct node * recursive_reverse (struct node * List) {struct node * head = List; struct node * P = r_reverse (list); head-> next = NULL; return P ;} struct node * r_reverse (struct node * List) {If (null = List | null = List-> next) return list; struct node * P = r_reverse (list-> next); List-> next = List; return P ;}

4. Transpose (recursion) of Adjacent Elements in a single-chain table)

 
Struct node * recursive_partial_reverse (struct node * List) {If (null = List | null = List-> next) return list; struct node * P = List-> next; struct node * node = recursive_partial_reverse (list-> next); List-> next = List; List-> next = node; return P ;}

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.