Two methods of realizing chain-list Flip in C + + __c++

Source: Internet
Author: User

There are two ways to implement a linked list flip: Create a new list, and then copy the node from the old list and use it as the head. The space complexity of O (N) in situ flip. Space Complexity of O (1)

Header file MyList.h

#ifndef my_list_h_
#define MY_LIST_H_

typedef int ELETYPE;
typedef struct node{
    eletype ele;
    struct node* next;
Node, *pnode;
Pnode Create ();
Pnode Reverse (Pnode a);
Pnode Reverse2 (Pnode a);
void Display (Pnode a);

#endif

MyList.cpp is as follows:

#include "myList.h" Pnode Create () {//lead node Pnode head = (pnode) malloc (sizeof (Pnode));
    A temp variable used as a marker pnode temp = head;
    Head->ele =-1;
    Head->next = NULL;
        The loop establishes a single linked list for (int i = 1; I <= i++) {//establishes the node and initializes pnode node = (malloc) sizeof (Pnode));
        Node->ele = i;
        Node->next = NULL;
        Temp->next = node;
        Temp moves backwards, always pointing to the tail node temp = node;
    cout << "Success" << "" << I << Endl;
return head;
    The position of the node after the next node of the header and the marked node is exchanged continuously, until the end of the list, the exchange completes Pnode Reverse (Pnode a) {Pnode stable, flag, temp;
    Stable = Flag = a->next; Flag is used to mark the most sequential node in the original list, moving backwards while (Flag->next) {//temp Saves the second node after flag,//Because the next node of the Flag->next points to a
        ->next, i.e. stable temp = flag->next->next;
        A->next = flag->next;
        A->next->next = stable;
       Variable stable always point to a->next; stable = a->next; Flag->next = temp;
return A;
    //The second is to create a new linked list, take the tail interpolation method, from the original list to pick one on the new list Pnode Reverse2 (Pnode a) {Pnode f1, F2;
    Pnode h = (pnode) malloc (sizeof (Pnode));
    H->ele =-1;
    H->next = NULL;
    F1 = a->next;
    F2 = h;
        while (F1) {//define TEMP variable and initialize pnode temp = (pnode) malloc (sizeof (Pnode));
        Temp->ele = f1->ele;
        Start tail Plug Temp->next = f2->next;
        F2->next = temp;
    F1 = f1->next;
return h;
    }//Simple display function void display (Pnode a) {Pnode q = a->next;
        while (q) {cout << q->ele << "";
    Q = q->next;
} cout << Endl; }

Test file Main.cpp

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include "Header.h"
using namespace std;

/* Run this program using the console Pauser or add your own getch, System ("pause") or input loop */

int main (int argc , char** argv) {
    pnode p;
    p = Create ();
    cout << "before reverse:" << Endl;    
    Display (p);
    p = Reverse (p);
    cout << "After reverse:" << Endl;
    Display (p);
    p = Reverse2 (p);
    cout << "after Reverse2:" << Endl;
    Display (p);
    System ("pause");
    return 0;
    }

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.