2016-12-24 4 views
-3

Ich versuche, die Position des ersten und letzten Knoten mit Dev C++ zu tauschen, es ist eine Aufgabe für meine Noten.Vertausche den ersten und letzten Knoten in C++

Und das Folgende habe ich bisher gemacht. Ich habe nicht mit der Swapping-Funktion begonnen, weil ich keine Ahnung habe, wie.

#include <iostream> 
#include <cstdlib> 
using namespace std; 

// Node class 
class Node { 
    int data; 

    public: 
    Node* next; 

    Node(int d=0){ 
     data = d; 
     next=NULL; 
    } 

    int getData() const{ 
     return data; 
    } 

}; //class Node 

    void display(Node *start, Node *end) 
    { 

    // Temp pointer 
    Node *tmp = start; 
    // One node in the list 
    while (tmp) 
    { 
    cout << tmp->getData() << "\t"; 
    tmp=tmp->next; 
    } 

    } //display 

Node* node; 

int main() 
    { 
     Node *start, *end, *head, *last, *newNode; 
     start = end = new Node(5); 

     for (int n=10; n<=35; n=n+5) { 
      end->next=new Node(n); 
      end=end->next; 

     } 

     display(start,end); 
     cin.get(); 

     //delete the first node 
     head=start; 
     start = start->next; 
     delete(head); 

     display(start,end); 
     cin.get(); 

     //delete the last node 
     last = start; 
     end = start->next; 
     while(end->next != NULL){ 
      last = end; 
      end = end->next; 
     } 
     last->next = NULL; 
     delete(end); 

     display(start,end); 
     cin.get(); 

     //insert value 3 in front of the node 
     newNode = new Node(3); 
     newNode->next = NULL; 
     newNode->next = start; 
     start = newNode; 

     display(start,end); 
     cin.get(); 

     //insert value 23 in between 20 and 25 
     int pos = 5; 
     newNode = new Node(23); 
     Node *temp; 

     end = start; 
     for(int i=1; i<pos-1; i++){ 
      end = end->next; 
     } 

     temp = end->next; 
     end->next = newNode; 
     newNode->next = temp; 

     display(start,end); 
     cin.get(); 

     //here is where the swap function must be performed. 

     display(start,end); //to display the result 
     cin.get(); 


    } //main 
+1

Armer Kerl, ist es nicht Weihnachten? lol –

+1

@FrederickZhang Aufschub. – LogicStuff

+1

* Mädchen. Kein Weihnachten für mich. Das Finale ist in 2 Tagen. bitte hilfe. – beginner96

Antwort

0

Finden Sie den ersten, zweiten, letzten und vorletzten Knoten.

first->link = null; 
last->link=second; 
second_last->link=first; 
return last 

Bei 2 Knoten

first->link=null; 
second->link=first; 
return second; 

Sie haben eine Liste von Knoten erstellt. Ausgehend von first.

if(first == NULL) 
    return ; 
else if(first ->next ==NULL) 
    return first; 
else 
{ 
    second = first -> next; 
    if(second ->next == NULL) 
    { 
     first->next = NULL; 
     second->next = first; 
     return second; 
    } 
    else 
    { 
     //declare firstcopy,last,second_last 
     firstcopy = first; 
     while(firstcopy->next != NULL) 
     { 
      second_last = firstcopy; 
      firstcopy = firstcopy ->next; 
      last = firstcopy; 
     } 

     first->next = null; 
     last->next=second; 
     second_last->next=first; 
     return last;//or you may print the linked list starting from last. 
    } 
} 

-Code

#include <iostream> 
#include <cstdlib> 
using namespace std; 

// Node class 
class Node { 
private: 
int data; 

public: 
Node* next; 

Node(int d=0):data(d),next(NULL) 
{ 

} 

int getData() const{ 
    return data; 
} 
Node operator=(const Node& b) { 
     Node box(b.getData()); 
     box.next=b.next; 
     return box; 
     } 
}; //class Node 

void display(Node *start) 
{ 
while(start!=NULL) 
{ 
    cout<<start->getData()<<" "; 
    start=start->next; 
} 
cout<<endl; 
} //display 

Node* swap(Node *first){ 
    if(first == NULL) 
     return first; 
    else if(first ->next ==NULL) 
     return first; 
    else 
    { 
     Node *second = first -> next; 
     if(second ->next == NULL) 
     { 
      first->next = NULL; 
      second->next = first; 
      return second; 
     } 
     else 
     { 
      Node* firstcopy,*last,*second_last; 
      firstcopy=first; 
      while(firstcopy->next != NULL) 
      { 
       second_last = firstcopy; 
       firstcopy = firstcopy ->next; 
       last = firstcopy; 
      } 

      first->next = NULL; 
      last->next=second; 
      second_last->next=first; 
      return last;//or you may print the linked list starting from last. 
     } 
    } 

} 

Node* node; 

int main() 
{ 
    Node *start, *end; 
    start = end = new Node(5); 

    for (int n=10; n<=35; n=n+5) { 
     end->next=new Node(n); 
     end=end->next; 

    } 

    display(start); 
    cin.get(); 

    start = swap(start); 


    display(start); 
    cin.get(); 



} //main 
+0

Kommentare sind nicht für längere Diskussion; Diese Konversation wurde [in den Chat verschoben] (http://chat.stackoverflow.com/rooms/131439/discussion-on-answer-by-coderredoc-swap-the-first-and-last-nodes-in-c) . –

0
void swap(Node **start, Node **end) 
{ 
    if(*start == *end) 
    { 
     cout << "start and end are at same position" << endl; 
     return; 
    } 

    Node *temp1 = *start; 
    Node *temp2; 
    Node *temp3 = *start; 
    while(temp1) 
    { 
     temp2 = temp1; 
     temp1 = temp1->next; 
     if(temp1 == *end) 
     { 
      *start = *end; 
      (*start)->next = temp3->next; 
      *end = temp3; 
      (*end)->next = NULL; 
      return;    
     } 
    } 
} 

Wenn Sie den Funktionsaufruf als Makeln (& Start, & Ende);

+0

funktioniert nicht .... – beginner96

+0

es funktioniert nicht ... – beginner96

+0

Ok. Anstatt nur die Knoten zu tauschen, müssen wir auch die nächsten Zeiger ändern. Überprüfen Sie dies. Wenn es nicht funktioniert, poste den Code und gib ihn auch aus. – kadina

0
#include <iostream> 
#include <cstdlib> 
using namespace std; 

// Node class 
class Node { 
int data; 

public: 
Node* next; 

Node(int d=0){ 
    data = d; 
    next=NULL; 
} 

int getData() const{ 
    return data; 
} 

}; //class Node 

void display(Node *start, Node *end) 
{ 

// Temp pointer 
Node *tmp = start; 
// One node in the list 
while (tmp) 
{ 
cout << tmp->getData() << "\t"; 
tmp=tmp->next; 
} 

} //display 

void swap(Node *first, *second, *second_last, *firstcopy){ 

    first->next = NULL; 
    last->next=second; 
    second_last->next=first; 
    return last; 

    if(first == NULL) 
    return ; 
    else if(first ->next ==NULL) 
    return first; 
    else{ 
     second = first -> next; 
     if(second ->next == NULL) 
     { 
      first->next = NULL; 
      second->next = first; 
      return second; 
     } 
    else 
    { 
    //declare firstcopy,last,second_last 
    firstcopy = first; 
    while(firstcopy->next != NULL) 
    { 
     second_last = firstcopy; 
     firstcopy = firstcopy ->next; 
     last = firstcopy; 
    } 

    first->next = NULL; 
    last->next=second; 
    second_last->next=first; 
    return last;//or you may print the linked list starting from last. 
    } 
} 
} 

Node* node; 

int main() 
{ 
    Node *start, *end, *head, *last, *newNode; 
    start = end = new Node(5); 

    for (int n=10; n<=35; n=n+5) { 
     end->next=new Node(n); 
     end=end->next; 

    } 

    display(start,end); 
    cin.get(); 

    //delete the first node 
    head=start; 
    start = start->next; 
    delete(head); 

    display(start,end); 
    cin.get(); 

    //delete the last node 
    last = start; 
    end = start->next; 
    while(end->next != NULL){ 
     last = end; 
     end = end->next; 
    } 
    last->next = NULL; 
    delete(end); 

    display(start,end); 
    cin.get(); 

    //insert value 3 in front of the node 
    newNode = new Node(3); 
    newNode->next = NULL; 
    newNode->next = start; 
    start = newNode; 

    display(start,end); 
    cin.get(); 

    //insert value 23 in between 20 and 25 
    int pos = 5; 
    newNode = new Node(23); 
    Node *temp; 

    end = start; 
    for(int i=1; i<pos-1; i++){ 
     end = end->next; 
    } 

    temp = end->next; 
    end->next = newNode; 
    newNode->next = temp; 

    display(start,end); 
    cin.get(); 

    //swap the position of the first and the last node 
    swap(&first, &second, &second_last, &firstcopy); 


    display(start,end); 
    cin.get(); 



} //main 
Verwandte Themen