1-2 栈:链表实现

栈:链表实现

栈是一种遵循后进先出(LIFO)原则的线性数据结构。它可以通过链表来实现,其中栈中的每个元素都表示为一个节点。链表的头节点即作为栈顶。

使用链表声明栈

栈可以通过链表实现,其中我们需要维护:

  • 一个包含以下内容的节点结构/类:
    data → 用于存储元素。
    next → 指向栈中下一个节点的指针/引用。
  • 一个指针/引用 top,它始终指向栈的当前顶部节点。
    初始时,top = null 表示空栈。
// Node structure
class Node 
{
public:
    int data;
    Node* next;
    
    Node(int x) 
    {
        data = x;
        next = NULL;
    }
};

// Stack class
class Stack 
{
    
    // pointer to top node
    Node* top;  
    
public:
    Stack() 
    {
        
        // initially stack is empty
        top = NULL;   
    }
};

节点结构

class Node 
{
public:
    int data;
    Node* next;
    
    Node(int x) 
    {
        data = x;
        next = NULL;
    }
};

栈类

class Stack 
{
    
    // pointer to top node
    Node* top;  
    
public:
    Stack() 
    {
        
        // initially stack is empty
        top = NULL;   
    }
};

栈的操作(链表实现)

入栈操作(Push)

将元素添加到栈中。与数组实现不同,链表没有固定容量。只有当内存耗尽时才会发生溢出。

步骤:

  1. 创建一个新节点,值为给定数据
  2. 将新节点的 next 指针指向当前栈顶
  3. 更新 top 指针指向新节点
void push(int x) 
{
    Node* temp = new Node(x);
    temp->next = top;
    top = temp;
}
  • 时间复杂度: O(1)
  • 辅助空间: O(1)

出栈操作(Pop)

移除栈顶元素。如果栈为空,则称为下溢(Underflow)条件。

步骤:

  1. 检查栈是否为空(top == NULL
  2. 如果栈为空,发生下溢,无法删除
  3. 否则,将当前栈顶节点存储在临时指针中
  4. top 指针移动到下一个节点
  5. 删除临时节点以释放内存
int pop() 
    {

    if (top == NULL) 
    {
        cout << "Stack Underflow" << endl;
        return -1;
    }

    Node* temp = top;
    top = top->next;
    int val = temp->data;

    delete temp;
    return val;
}
  • 时间复杂度: O(1)
  • 辅助空间: O(1)

查看栈顶(Peek / Top)

返回栈顶元素的值,但不移除它。

int peek() 
{

    if (top == NULL) 
    {
        cout << "Stack is Empty" << endl;
        return -1;
    }

    return top->data;
}
  • 时间复杂度: O(1)
  • 辅助空间: O(1)

判空操作(isEmpty)

检查栈是否没有元素。

bool isEmpty() {
    return top == NULL;
}
  • 时间复杂度: O(1)
  • 辅助空间: O(1)

完整代码实现

#include <iostream>

// Node structure
class Node 
{
public:
    int data;
    Node* next;
    
    Node(int x) 
    {
        data = x;
        next = NULL;
    }
};

// Stack implementation using linked list
class Stack 
{
    Node* top;
    
    // To Store current size of stack
    int count;
    
public:
    Stack() 
    {
        
        // initially stack is empty
        top = NULL;
        count = 0;
    }

    // push operation
    void push(int x) 
    {
        Node* temp = new Node(x);
        temp->next = top;
        top = temp;
        
        count++;
    }

    // pop operation
    int pop() 
    {
        if (top == NULL) 
        {
            std::cout << "Stack Underflow" << endl;
            return -1;
        }
        Node* temp = top;
        top = top->next;
        int val = temp->data;
        
        count--;
        delete temp;
        return val;
    }

    // peek operation
    int peek() 
    {
        if (top == NULL) 
        {
            std::cout << "Stack is Empty" << endl;
            return -1;
        }
        return top->data;
    }

    // check if stack is empty
    bool isEmpty() 
    {
        return top == NULL;
    }

    // size of stack
    int size() 
    {
        return count;
    }
};

int main() 
{
    Stack st;

    // pushing elements
    st.push(1);
    st.push(2);
    st.push(3);
    st.push(4);

    // popping one element
    std::cout << "Popped: " << st.pop() << endl;

    // checking top element
    std::cout << "Top element: " << st.peek() << endl;

    // checking if stack is empty
    std::cout << "Is stack empty: " << (st.isEmpty() ? "Yes" : "No") << endl;

    // checking current size
    std::cout << "Current size: " << st.size() << endl;

    return 0;
}

输出结果

Popped: 4
Top element: 3
Is stack empty: No
Current size: 3
posted @ 2026-03-20 16:53  游翔  阅读(0)  评论(0)    收藏  举报