📌  相关文章
📜  数据结构示例-交换单向链表第一个和最后一个节点

📅  最后修改于: 2020-10-15 04:49:29             🧑  作者: Mango

程序交换第一个链表中的最后一个元素

说明

在此程序中,我们需要将单链列表的最后一个节点与第一个节点交换,以便第一个节点将成为最后一个节点,而最后一个节点将成为第一个节点。

考虑上面的例子;节点1代表列表的开头,节点4代表最后一个节点。为了将第一个节点与最后一个节点交换,我们将遍历列表,以使索引指向第二个最后一个节点,而current指向最后一个节点。节点温度将指向头。然后,将当前(最后一个节点)作为列表的新标题。现在,将整个列表移到旧的头之后,并将其附加到新的头之后。最后,在索引节点(倒数第二个节点)之后添加temp(旧头节点)。

算法

  • 创建一个具有两个属性的类Node:data和next。下一个是指向列表中下一个节点的指针。
  • 创建另一个具有两个属性的Swap类:head和tail。
  • addNode()将向列表添加一个新节点:
    1. 创建一个新节点。
    2. 它首先检查head是否等于null,这意味着列表为空。
    3. 如果列表为空,则头和尾都将指向新添加的节点。
    4. 如果列表不为空,则新节点将被添加到列表的末尾,使得尾部的下一个将指向新添加的节点。这个新节点将成为列表的新尾部。
  • SwapFirstWithLast()将第一个节点与最后一个节点交换:
    1. 如果列表为空,则从函数返回。
    2. 如果列表不为空,请遍历列表,以使当前节点将指向列表的最后一个节点,而索引将指向倒数第二个节点。
    3. 检查列表是否仅包含一个节点,则无法交换。
    4. 如果列表仅包含两个节点,则使用节点temp与当前节点交换头节点。
    5. 否则,温度将指向头部,而指向最后一个节点的电流将成为列表的新头部。
    6. 移动除旧头节点之外的列表,并将其附加到新头之后,即head.next = temp.next
    7. 现在将temp(第一个)节点添加到索引节点之后,使其成为列表的最后一个节点,其下一个节点将为null。
  • display()将显示列表中存在的节点:
    1. 定义一个节点电流,该电流将初始指向列表的开头。
    2. 遍历列表,直到当前指向null。
    3. 通过在每次迭代中使电流指向其旁边的节点来显示每个节点。

示例:

Python

#Represent a node of the singly linked list
class Node:
    def __init__(self,data):
        self.data = data;
        self.next = None;
        
class Swap:
    #Represent the head of the singly linked list
    def __init__(self):
        self.head = None;
        
    #addNode() will add a new node to the list
    def addNode(self, data):
        #Create a new node
        newNode = Node(data);
        
        #Checks if the list is empty
        if(self.head == None):
            #If list is empty, head will point to new node
            self.head = newNode;
        else:
            current = self.head;
            while(current.next != None):
                current = current.next;
            #newNode will be added after last node of the list
            current.next = newNode;
            
    #swapLastWithFirst() will swap head node with the last node of the list
    def swapLastWithFirst(self):
        current = self.head;
        temp = None;
        index = None;
        
        #If list is empty, then display the list as it is
        if(self.head == None):
            return;
        else:
            #After the loop, current will point to last element and index will point to second last element
            while(current.next != None):
                index = current;
                current = current.next;
                
            #If list contains only one node, then display list as it is
            if(self.head == current):
                return;
                
            #If list contains only two nodes, then swap the head node with current node
            elif(self.head.next == current):
                temp = self.head;
                #head will point to last node i.e. current
                self.head = current;
                #node next to new head will be the last node
                self.head.next = temp;
                #Node next to last element will be None
                temp.next = None;
            else:
                temp = self.head;
                #head will point to last node i.e. current
                self.head = current;
                #Detach the list from previous head and add it after new head
                self.head.next = temp.next;
                #Previous head will become last node of the list
                index.next = temp;
                #Node next to last element will be None
                temp.next = None;
            
    #display() will display all the nodes present in the list
    def display(self):
        #Node current will point to head
        current = self.head;
        
        if(self.head == None):
            print("List is empty");
            return;
        while(current != None):
            #Prints each node by incrementing pointer
            print(current.data ),
            current = current.next;
        print "";
 
sList = Swap();
        
#Add nodes to the list
sList.addNode(1);
sList.addNode(2);
sList.addNode(3);
sList.addNode(4);
 
print("Originals list: ");
sList.display();
 
#Swaps Last node with first node
sList.swapLastWithFirst();
 
print("List after swapping the first node with last: ");
sList.display();

输出:

 Originals list: 
1 2 3 4 
List after swapping the first node with last:
4 2 3 1 

C

#include 
 
//Represent a node of the singly linked list
struct node{
    int data;
    struct node *next;
};    
 
//Represent the head of the singly linked list
struct node *head = NULL;
 
//addNode() will add a new node to the list
void addNode(int data) {
    //Create a new node
    struct node *newNode = (struct node*)malloc(sizeof(struct node));
    newNode->data = data;
    newNode->next = NULL;
    
    //Checks if the list is empty
    if(head == NULL) {
        //If list is empty, head will point to new node
        head = newNode;
    }
    else {
        struct node *current = head;
        while(current->next != NULL) {
            current = current->next;
        }
        //newNode will be added after last node of the list
        current->next = newNode;
    }
}
 
//swapLastWithFirst() will swap head node with the last node of the list
void swapLastWithFirst() {
    struct node *current = head, *temp = NULL, *index = NULL;
    
    //If list is empty, then display the list as it is
    if(head == NULL) {
        return;
    }
    else {
        //After the loop, current will point to last element and index will point to second last element
        while(current->next != NULL) {
            index = current;
            current = current->next;
        }
        
        //If list contains only one node, then display list as it is
        if(head == current) {
            return;
        }
        //If list contains only two nodes, then swap the head node with current node
        else if(head->next == current) {
            temp = head;
            //head will point to last node i.e. current
            head = current;
            //node next to new head will be the last node
            head->next = temp;
            //Node next to last element will be null
            temp->next = NULL;
        }
        else {
            temp = head;
            //head will point to last node i.e. current
            head = current;
            //Detach the list from previous head and add it after new head
            head->next = temp->next;
            //Previous head will become last node of the list
            index->next = temp;
            //Node next to last element will be null
            temp->next = NULL;
        }
    }
}
    
//display() will display all the nodes present in the list
void display() {
    //Node current will point to head
    struct node *current = head;
    
    if(head == NULL) {
        printf("List is empty\n");
        return;
    }
    while(current != NULL) {
        //Prints each node by incrementing pointer
        printf("%d ", current->data);
        current = current->next;
    }
    printf("\n");
}
    
int main()
{
    //Add nodes to the list
    addNode(1);
    addNode(2);
    addNode(3);
    addNode(4);
 
    printf("Originals list: \n");
    display();
    
    //Swaps Last node with first node
    swapLastWithFirst();
    
    printf("List after swapping the first node with last: \n");
    display();
 
    return 0;
}

输出:

Originals list: 
1 2 3 4 
List after swapping the first node with last:
4 2 3 1 

JAVA

public class Swap {
    
    //Represent a node of the singly linked list
    class Node{
        int data;
        Node next;
        
        public Node(int data) {
            this.data = data;
            this.next = null;
        }
    }
 
    //Represent the head of the singly linked list
    public Node head = null;
    
    //addNode() will add a new node to the list
    public void addNode(int data) {
        //Create a new node
        Node newNode = new Node(data);
        
        //Checks if the list is empty
        if(head == null) {
            //If list is empty, head will point to new node
            head = newNode;
        }
        else {
            Node current = head;
            while(current.next != null) {
                current = current.next;
            }
            //newNode will be added after last node of the list
            current.next = newNode;
        }
    }
    
    //swapLastWithFirst() will swap head node with the last node of the list
    public void swapLastWithFirst() {
        Node current = head, temp = null, index = null;
        
        //If list is empty, then display the list as it is
        if(head == null) {
            return;
        }
        else {
            //After the loop, current will point to last element and index will point to second last element
            while(current.next != null) {
                index = current;
                current = current.next;
            }
            
            //If list contains only one node, then display list as it is
            if(head == current) {
                return;
            }
            //If list contains only two nodes, then swap the head node with current node
            else if(head.next == current) {
                temp = head;
                //head will point to last node i.e. current
                head = current;
                //node next to new head will be the last node
                head.next = temp;
                //Node next to last element will be null
                temp.next = null;
            }
            else {
                temp = head;
                //head will point to last node i.e. current
                head = current;
                //Detach the list from previous head and add it after new head
                head.next = temp.next;
                //Previous head will become last node of the list
                index.next = temp;
                //Node next to last element will be null
                temp.next = null;
            }
        }
    }
    
    //display() will display all the nodes present in the list
    public void display() {
        //Node current will point to head
        Node current = head;
        if(head == null) {
            System.out.println("List is empty");
            return;
        }
        while(current != null) {
            //Prints each node by incrementing pointer
            System.out.print(current.data + " ");
            current = current.next;
        }
        System.out.println();
    }
    
    public static void main(String[] args) {
        
        Swap sList = new Swap();
        
        //Add nodes to the list
        sList.addNode(1);
        sList.addNode(2);
        sList.addNode(3);
        sList.addNode(4);
 
        System.out.println("Originals list: ");
        sList.display();
        
        //Swaps Last node with first node
        sList.swapLastWithFirst();
        
        System.out.println("List after swapping the first node with last: ");
        sList.display();
    }
}

输出:

Originals list: 
1 2 3 4 
List after swapping the first node with last:
4 2 3 1 

C#

using System;
                    
public class CreateList
{
    //Represent a node of the singly linked list
    public class Node{
        public T data;
        public Node next;
        
        public Node(T value) {
            data = value;
            next = null;
        }
    }
        
    public class Swap{
        //Represent the head of the singly linked list
        public Node head = null;             
    
        //addNode() will add a new node to the list
        public void addNode(T data) {
            //Create a new node
            Node newNode = new Node(data);
 
            //Checks if the list is empty
            if(head == null) {
                //If list is empty, head will point to new node
                head = newNode;
            }
            else {
                Node current = head;
                while(current.next != null) {
                    current = current.next;
                }
                //newNode will be added after last node of the list
                current.next = newNode;
            }
        }
        
        //swapLastWithFirst() will swap head node with the last node of the list
        public void swapLastWithFirst() {
            Node current = head, temp = null, index = null;
 
            //If list is empty, then display the list as it is
            if(head == null) {
                return;
            }
            else {
                //After the loop, current will point to last element and index will point to second last element
                while(current.next != null) {
                    index = current;
                    current = current.next;
                }
 
                //If list contains only one node, then display list as it is
                if(head == current) {
                    return;
                }
                //If list contains only two nodes, then swap the head node with current node
                else if(head.next == current) {
                    temp = head;
                    //head will point to last node i.e. current
                    head = current;
                    //node next to new head will be the last node
                    head.next = temp;
                    //Node next to last element will be null
                    temp.next = null;
                }
                else {
                    temp = head;
                    //head will point to last node i.e. current
                    head = current;
                    //Detach the list from previous head and add it after new head
                    head.next = temp.next;
                    //Previous head will become last node of the list
                    index.next = temp;
                    //Node next to last element will be null
                    temp.next = null;
                }
            }
        }
        
        //display() will display all the nodes present in the list
        public void display() {
            //Node current will point to head
            Node current = head;
            
            if(head == null) {
                Console.WriteLine("List is empty");
                return;
            }
            while(current != null) {
                //Prints each node by incrementing pointer
                Console.Write(current.data + " ");
                current = current.next;
            }
            Console.WriteLine();
        }
    }
    
    public static void Main()
    {
        Swap sList = new Swap();
        
        //Add nodes to the list
        sList.addNode(1);
        sList.addNode(2);
        sList.addNode(3);
        sList.addNode(4);
 
        Console.WriteLine("Originals list: ");
        sList.display();
        
        //Swaps Last node with first node
        sList.swapLastWithFirst();
        
        Console.WriteLine("List after swapping first node with last: ");
        sList.display();    
    }
}

输出:

Originals list: 
1 2 3 4 
List after swapping first node with last: 
4 2 3 1 

PHP:




data = $data;
        $this->next = NULL;
    }
}
class Swap{
    //Represent the head of the singly linked list
    public $head;
    function __construct(){
        $this->head = NULL;
    }
    
    //addNode() will add a new node to the list
    function addNode($data) {
        //Create a new node
        $newNode = new Node($data);
        
        //Checks if the list is empty
        if($this->head == NULL) {
            //If list is empty, head will point to new node
            $this->head = $newNode;
        }
        else {
            $current = $this->head;
            while($current->next != null) {
                $current = $current->next;
            }
            //newNode will be added after last node of the list
            $current->next = $newNode;
        }
    }
    
    //swapLastWithFirst() will swap head node with the last node of the list
    function swapLastWithFirst() {
        $current = $this->head;
        $temp = null;
        $index = null;
        
        //If list is empty, then display the list as it is
        if($this->head == null) {
            return;
        }
        else {
            //After the loop, current will point to last element and index will point to second last element
            while($current->next != null) {
                $index = $current;
                $current = $current->next;
            }
            
            //If list contains only one node, then display list as it is
            if($this->head == $current) {
                return;
            }
            //If list contains only two nodes, then swap the head node with current node
            else if($this->head->next == $current) {
                $temp = $this->head;
                //head will point to last node i.e. current
                $this->head = $current;
                //node next to new head will be the last node
                $this->head->next = $temp;
                //Node next to last element will be null
                $temp->next = null;
            }
            else {
                $temp = $this->head;
                //head will point to last node i.e. current
                $this->head = $current;
                //Detach the list from previous head and add it after new head
                $this->head->next = $temp->next;
                //Previous head will become last node of the list
                $index->next = $temp;
                //Node next to last element will be null
                $temp->next = null;
            }
        }
    }
    
    //display() will display all the nodes present in the list
    function display() {
        //Node current will point to head
        $current = $this->head;
        
        if($this->head == NULL) {
            print("List is empty 
"); return; } while($current != NULL) { //Prints each node by incrementing pointer print($current->data . " "); $current = $current->next; } print("
"); } } $sList = new Swap(); //Add nodes to the list $sList->addNode(1); $sList->addNode(2); $sList->addNode(3); $sList->addNode(4); print("Originals list:
"); $sList->display(); //Swaps Last node with first node $sList->swapLastWithFirst(); print("List after swapping first node with last:
"); $sList->display(); ?>

输出:

Originals list: 
1 2 3 4 
List after swapping first node with last: 
4 2 3 1