C language A code example for solving the problem of reverse order of strings and one-way list _c language

Source: Internet
Author: User

String reverse
The last interview encountered a one-way list of the reverse sequence of the topic, fortunately the string reverse order more familiar with the analogy made. String reverse order is simpler, directly on the code:

void Stringreverse (char* p1,char* p2) 
{ 
 if (P1==P2) return; 
 Swap the value of P1, p2 
 *p1= (*P1) + (*P2); 
 *p2= (*P1)-(*P2); 
 *p1= (*P1)-(*P2); 
  if (p1==p2-1) return; 
  else Stringreverse (++p1,--p2); 
} 

Call Example:

Stringreverse (Str,str+strlen (str)-1); 

Reverse order of one-way linked list
first three pictures: The original state is figure one, all the reverse order is the appearance of Tu San, figure II is a reverse step after the appearance. Follow the steps in Figure two to move one step at a time, and reverse the p1->next until all is reversed. Detailed principle see code.
Figure I:

Figure II:

Figure III:

typedef struct node node; 
struct node{ 
 int data; 
 Node* Next; 
 
node* reverselinklist (node* head) { 
 if (head==null| | Head->next==null) {return head 
  ; 
 } 
 node* P1 = head; 
 node* P2 = p1->next; 
 node* P3 = p2->next; 
 P1->next = Null;//p1 to end pointer, so point to NULL while 
 (p3!=null) { 
  p2->next = p1;//p2->next reverse 
  p1=p2;// P1 pointer moves backward p2=p3;//p2 pointer moves back one step 
  p3=p3->next;//p3 pointer moves backward 
 p2->next=p1;//p3 points to null, P2 
 the reverse head=p2;//header pointer to the previous tail pointer return head 
 ; 
} 

It's over.

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.