交换C ++中的第一个和最后一个节点

时间:2016-12-24 15:41:50

标签: c++ linked-list

我正在尝试使用dev c ++交换第一个节点和最后一个节点的位置,这是我的成绩的分配。

以下是我到目前为止所做的事情。我还没有开始交换功能,因为我不知道如何。

#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

2 个答案:

答案 0 :(得分:0)

找到第一个,第二个,最后一个和第二个最后一个节点。

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

如果是2个节点

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

您已创建节点列表。从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.
    }
}

代码

#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

答案 1 :(得分: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