C语言中栈的表示和实现

在 C 编程语言中,可以使用数组或链表实现堆栈。这两种实现都有其优点和注意事项,因此让我们探讨这两种方法。

1. 使用数组的堆栈实现:
   在此实现中,我们使用数组来表示堆栈。数组将具有固定大小,变量将跟踪堆栈的顶部元素。

''''c
   

#include <stdio.h>

#define MAX_SIZE 100

 

int stack[MAX_SIZE];

int top = -1;

 

// Function to check if the stack is empty

int isEmpty() {

    return (top == -1);

}

 

// Function to check if the stack is full

int isFull() {

    return (top == MAX_SIZE - 1);

}

 

// Function to push an element onto the stack

void push(int item) {

    if (isFull()) {

        printf("Stack Overflow\n");

        return;

    }

    stack[++top] = item;

}

 

// Function to pop an element from the stack

int pop() {

    if (isEmpty()) {

        printf("Stack Underflow\n");

        return -1; // Return an invalid value or handle error appropriately

    }

    return stack[top--];

}

 

// Function to get the top element of the stack

int peek() {

    if (isEmpty()) {

        printf("Stack is empty\n");

        return -1; // Return an invalid value or handle error appropriately

    }

    return stack[top];

}

 

// Function to display the elements of the stack

void display() {

    if (isEmpty()) {

        printf("Stack is empty\n");

        return;

    }

    printf("Stack elements: ");

    for (int i = top; i >= 0; i--) {

        printf("%d ", stack[i]);

    }

    printf("\n");

}

 

// Example usage

int main() {

    push(10);

    push(20);

    push(30);

    display(); // Output: Stack elements: 30 20 10

    printf("%d\n", pop()); // Output: 30

    printf("%d\n", peek()); // Output: 20

    display(); // Output: Stack elements: 20 10

    return 0;

}

```


   '''

2. 使用链表的堆栈实现:
   在此实现中,链表用于表示堆栈。链表的每个节点都包含数据和指向下一个节点的指针。

''''c

 

#include <stdio.h>

#include <stdlib.h>

 

struct Node {

    int data;

    struct Node* next;

};

 

struct Node* top = NULL;

 

// Function to check if the stack is empty

int isEmpty() {

    return (top == NULL);

}

 

// Function to push an element onto the stack

void push(int item) {

    struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));

    if (newNode == NULL) {

        printf("Memory allocation failed\n");

        return;

    }

    newNode->data = item;

    newNode->next = top;

    top = newNode;

}

 

// Function to pop an element from the stack

int pop() {

    if (isEmpty()) {

        printf("Stack Underflow\n");

        return -1; // Return an invalid value or handle error appropriately

    }

    struct Node* temp = top;

    int item = temp->data;

    top = top->next;

    free(temp);

    return item;

}

 

// Function to get the top element of the stack

int peek() {

    if (isEmpty()) {

        printf("Stack is empty\n");

        return -1; // Return an invalid value or handle error appropriately

    }

    return top->data;

}

 

// Function to display the elements of the stack

void display() {

    if (isEmpty()) {

        printf("Stack is empty\n");

        return;

    }

    printf("Stack elements: ");

    struct Node* current = top;

    while (current != NULL) {

        printf("%d ", current->data);

        current = current->next;

    }

    printf("\n");

}

 

// Example usage

int main() {

    push(10);

    push(20);

    push(30);

    display(); // Output: Stack elements: 30 20 10

    printf("%d\n", pop()); // Output: 30

    printf("%d\n", peek()); // Output: 20

    display(); // Output: Stack elements: 20 10

    return 0;

}

```


   '''

这两种实现都提供了堆栈的基本操作:“push”用于添加元素,“pop”用于删除顶部元素,“peek”用于检索顶部元素而不删除它,“display”用于打印堆栈的元素。

在基于数组的实现中,我们使用固定大小的数组,并使用“top”变量跟踪顶部元素。函数“isEmpty”和“isFull”分别检查堆栈是空的还是满的,以处理潜在的错误。“push”函数通过递增“top”并将值分配给数组中的相应索引来向堆栈添加元素。'pop' 函数通过递减 'top' 并从数组中返回值来删除 top 元素。“peek”函数在不修改堆栈的情况下返回顶部元素的值。“display”函数以相反的顺序遍历元素并打印它们。

在链表实现中,我们为链表的节点定义了一个结构,其中包含数据和指向下一个节点的指针。“top”指针指向堆栈中的第一个节点。“isEmpty”函数检查“top”指针是否为“NULL”,以确定堆栈是否为空。“push”函数创建一个新节点,分配数据并更新“next”指针以指向上一个顶部节点。“pop”函数删除顶部节点,更新“top”指针,并返回数据。“peek”函数返回顶部节点的数据,而不修改堆栈。“display”函数遍历链表并打印每个节点的数据。

请记住根据特定要求适当地处理错误情况,例如堆栈溢出或下溢。

 

  • 12
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

BlurryFace36549

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值