数据结构:使用栈对链表元素位序进行反转

给定一个链表,业务需求:使用栈将链表中元素的次序进行反转。

输入 : List = 3 -> 2 -> 1
输出 : 1 -> 2 -> 3

输入 : 9 -> 7 -> 4 -> 2
输出 : 2 -> 4 -> 7 -> 9 

算法过程:算法复杂度:O(n)1、遍历列表,将所有节点推到栈上。2、遍历栈,并依次从栈顶弹出元素用相反顺序存储。java代码:
// Java program to reverse linked list
// using stack
import java.util.*;
class GfG
{ 

/* Link list node */
static class Node
{
    int data;
    Node next;
}
static Node head = null; 

/* Given a reference (pointer to pointer) to
the head of a list and an int, push a new
node on the front of the list. */
static void push( int new_data)
{
    Node new_node = new Node(); 

    new_node.data = new_data;
    new_node.next = (head);
    (head) = new_node;
} 

// Function to reverse linked list
static Node reverseList(Node head)
{
    // Stack to store elements of list
    Stack<Node > stk = new Stack<Node> (); 

    // Push the elements of list to stack
    Node ptr = head;
    while (ptr.next != null)
    {
        stk.push(ptr);
        ptr = ptr.next;
    } 

    // Pop from stack and replace
    // current nodes value‘
    head = ptr;
    while (!stk.isEmpty())
    {
        ptr.next = stk.peek();
        ptr = ptr.next;
        stk.pop();
    }
    ptr.next = null; 

    return head;
} 

// Function to print the Linked list
static void printList(Node head)
{
    while (head != null)
    {
        System.out.print(head.data + " ");
        head = head.next;
    }
} 

// Driver Code
public static void main(String[] args)
{
    /* Start with the empty list */
    //Node head = null; 

    /* Use push() to construct below list
    1->2->3->4->5 */
    push( 5);
    push( 4);
    push( 3);
    push( 2);
    push( 1); 

    head = reverseList(head); 

    printList(head);
}
} 

// This code is contributed by Prerna Saini. 

c#代码

// C# program to reverse linked list
// using stack
using System;
using System.Collections.Generic; 

class GfG
{ 

/* Link list node */
public class Node
{
    public int data;
    public Node next;
}
static Node head = null; 

/* Given a reference (pointer to pointer) to
the head of a list and an int, push a new
node on the front of the list. */
static void push( int new_data)
{
    Node new_node = new Node(); 

    new_node.data = new_data;
    new_node.next = (head);
    (head) = new_node;
} 

// Function to reverse linked list
static Node reverseList(Node head)
{
    // Stack to store elements of list
    Stack<Node > stk = new Stack<Node> (); 

    // Push the elements of list to stack
    Node ptr = head;
    while (ptr.next != null)
    {
        stk.Push(ptr);
        ptr = ptr.next;
    } 

    // Pop from stack and replace
    // current nodes value‘
    head = ptr;
    while (stk.Count != 0)
    {
        ptr.next = stk.Peek();
        ptr = ptr.next;
        stk.Pop();
    }
    ptr.next = null; 

    return head;
} 

// Function to print the Linked list
static void printList(Node head)
{
    while (head != null)
    {
        Console.Write(head.data + " ");
        head = head.next;
    }
} 

// Driver Code
public static void Main(String[] args)
{
    /* Start with the empty list */
    //Node head = null; 

    /* Use push() to construct below list
    1->2->3->4->5 */
    push( 5);
    push( 4);
    push( 3);
    push( 2);
    push( 1); 

    head = reverseList(head); 

    printList(head);
}
} 

// This code contributed by Rajput-Ji 

算法2(迭代):

时间复杂度:O(1)

// Iterative C program to reverse a linked list
#include <stdio.h>
#include <stdlib.h>

/* Linked list node */
struct Node
{
    int data;
    struct Node* next;
};

//function to reverse the linked list
static void reverse(struct Node** head_ref)
{
    struct Node* prev = NULL;
    struct Node* current = *head_ref;
    struct Node* next = NULL;

    while(current != NULL)
    {
        //Store next
        next = current->next;

        //Move pointers one position ahead.
        prev = current;

        current = next;
    }
    *head_ref = prev;
}

/* function to push a node */
void push(struct Node** head_ref, int new_data)
{
    struct Node* new_node = (struct Node*)malloc(sizeof(struct Node));
    new_node->data = new_data;
    new_node->next =(*head_ref);
    (*head_ref) = new_node;
}

/* function to print linked list */
void printList(struct Node* head)
{
    struct Node* temp = head;
    while( temp != NULL)
    {
        printf("%d ", temp->data);
        temp = temp->next;
    }
    printf("\n");
}

/* Driver program to test above function */
int main()
{
    /* Start with the empty list */
    struct Node* head = NULL;

    push(&head, 20);
    push(&head, 4);
    push(&head, 15);
    push(&head, 85);

    printf("Given linked list\n");
    printList(head);

    reverse(&head);

    printf("Reversed Linked List\n");
    printList(head);
}

文章来源:https://www.geeksforgeeks.org/program-to-reverse-a-linked-list-using-stack/

原文地址:https://www.cnblogs.com/passedbylove/p/11442606.html

时间: 2024-08-30 07:05:44

数据结构:使用栈对链表元素位序进行反转的相关文章

数据结构学习——栈的链表实现(程序化)

关于栈的基本概念以及和Catalan数的关系,可以参见我的其他文章 参考资料<数据结构与算法分析--C语言描述> #include<stdio.h> #include<stdlib.h> /*栈的链表实现*/ typedef struct StackNode { struct StackNode *next; int data; }StackNode,*Stack; Stack CreateStack(void);//创建一个空栈 void MakeEmpty(Stac

数据结构应用实例#栈&amp;单链表#简易计算器

使用数据结构中的栈以及链表来制作一个能够处理中缀表达式的计算程序. 在该程序中输入的数字可以是任意正数.小数.(忘了添加对负数的支持,尽快补上) 输入的运算符可以是 + - * / ( ) 但请确保输入的表达式合法. 中缀表达式: 形如 9+(3-1)*3+10/2 的数学表达式.特点是运算符在被运算的两个数字中间,也就是我们日常接触的算式. 后缀表达式: 以上面的中缀表达式 9+(3-1)*3+10/2 为例 转换成后缀表达式为 9 3 1-3*+ 10 2/+ 特点是运算符紧跟在被运算的两个

数据结构---栈的链表实现

栈的链表实现C代码如下: #include <stdio.h> typedef int ElemType; typedef struct node { ElemType Data; struct node *next; }Node; typedef struct stack { Node *top; }Stack; //初始化栈 void InitStack(Stack *S) { S->top=NULL; } //入栈 int PushStackValue(Stack *S) { pr

C语言数据结构链栈(创建、入栈、出栈、取栈顶元素、遍历链栈中的元素)

/**创建链栈*创建一个top指针代表head指针*采用链式存储结构*采用头插法创建链表*操作 创建 出栈 入栈 取栈顶元素*创建数据域的结构体*创建数据域的名称指针*使用随机函数对数据域的编号进行赋值*/ #include<stdio.h>#include<stdlib.h>#include<string.h>#include<time.h>#define OK 1#define ERROR 0#define NAMESIZE 255//字符串的最大长度

数据结构之栈和队列

数据结构学习继续向前推进,之前对线性表进行了学习,现在我们进入栈和队列的学习.同样我们先学习一些基本概念以及堆栈的ADT. 栈和队列是两种中重要的线性结构.从数据结构角度看,栈和队列也是线性表,只不过是受限的线性表.因此可以称为限定性数据结构.但从数据类型来看,他们是和线性表大不相同的两类重要的抽象数据类型. 栈:(stack)是限定仅在表尾进行相应插入和删除操作的线性表.因此,对栈来说,表尾有其特殊含义,称为栈顶,表头称为栈底,不含元素的空表称为空栈.栈一个重要特性就是后进先出.OK,我们来看

【转载】浅谈算法和数据结构: 一 栈和队列

作者:yangecnu(yangecnu's Blog on 博客园) 出处:http://www.cnblogs.com/yangecnu/ 最近晚上在家里看Algorithms,4th Edition,我买的英文版,觉得这本书写的比较浅显易懂,而且“图码并茂”,趁着这次机会打算好好学习做做笔记,这样也会印象深刻,这也是写这一系列文章的原因.另外普林斯顿大学在Coursera 上也有这本书同步的公开课,还有另外一门算法分析课,这门课程的作者也是这本书的作者,两门课都挺不错的. 计算机程序离不开

Java数据结构和算法之链表

三.链表 链结点 在链表中,每个数据项都被包含在'点"中,一个点是某个类的对象,这个类可认叫做LINK.因为一个链表中有许多类似的链结点,所以有必要用一个不同于链表的类来表达链结点.每个LINK对象中都包含一个对下一个点引用的字段(通常叫做next)但是本身的对象中有一个字段指向对第一个链结点的引用. 单链表 用一组地址任意的存储单元存放线性表中的数据元素. 以元素(数据元素的映象)  + 指针(指示后继元素存储位置)  = 结点(表示数据元素 或 数据元素的映象) 以"结点的序列&q

浅谈算法和数据结构: 一 栈和队列

最近晚上在家里看Algorithems,4th Edition,我买的英文版,觉得这本书写的比较浅显易懂,而且"图码并茂",趁着这次机会打算好好学习做做笔记,这样也会印象深刻,这也是写这一系列文章的原因.另外普林斯顿大学在Coursera 上也有这本书同步的公开课,还有另外一门算法分析课,这门课程的作者也是这本书的作者,两门课都挺不错的. 计算机程序离不开算法和数据结构,本文简单介绍栈(Stack)和队列(Queue)的实现,.NET中与之相关的数据结构,典型应用等,希望能加深自己对这

顺序栈与链表栈的实现

栈是一种常见的数据结构,它虽然有栈顶和栈底之分,但它只能从一端操作(插入或删除),从而是一种"先进后出"的操作模式.向栈内进数据称为压栈(Push),从栈里取出数据叫出栈(POp).例如压栈顺序为1.2.3.4.5,着出栈的顺序为5.4.3.2.1(只考虑一次性出栈的情况). 栈按照存储的方式,又分为顺序栈和链表栈.顺序栈基于数组实现,所以顺序栈存储数据的内存是连续的,在创建栈时规定好栈的大小,这样对内存的使用效率并不高.而链式栈则是采用了链表来实现,其元素的存储地址是不连续的,而且是