C语言中栈的表示和实现

2024-01-09 22:44
文章标签 语言 实现 表示 中栈

本文主要是介绍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”函数遍历链表并打印每个节点的数据。

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

 

这篇关于C语言中栈的表示和实现的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/588686

相关文章

springboot+redis实现订单过期(超时取消)功能的方法详解

《springboot+redis实现订单过期(超时取消)功能的方法详解》在SpringBoot中使用Redis实现订单过期(超时取消)功能,有多种成熟方案,本文为大家整理了几个详细方法,文中的示例代... 目录一、Redis键过期回调方案(推荐)1. 配置Redis监听器2. 监听键过期事件3. Redi

SpringBoot全局异常拦截与自定义错误页面实现过程解读

《SpringBoot全局异常拦截与自定义错误页面实现过程解读》本文介绍了SpringBoot中全局异常拦截与自定义错误页面的实现方法,包括异常的分类、SpringBoot默认异常处理机制、全局异常拦... 目录一、引言二、Spring Boot异常处理基础2.1 异常的分类2.2 Spring Boot默

基于SpringBoot实现分布式锁的三种方法

《基于SpringBoot实现分布式锁的三种方法》这篇文章主要为大家详细介绍了基于SpringBoot实现分布式锁的三种方法,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录一、基于Redis原生命令实现分布式锁1. 基础版Redis分布式锁2. 可重入锁实现二、使用Redisso

SpringBoo WebFlux+MongoDB实现非阻塞API过程

《SpringBooWebFlux+MongoDB实现非阻塞API过程》本文介绍了如何使用SpringBootWebFlux和MongoDB实现非阻塞API,通过响应式编程提高系统的吞吐量和响应性能... 目录一、引言二、响应式编程基础2.1 响应式编程概念2.2 响应式编程的优势2.3 响应式编程相关技术

C#实现将XML数据自动化地写入Excel文件

《C#实现将XML数据自动化地写入Excel文件》在现代企业级应用中,数据处理与报表生成是核心环节,本文将深入探讨如何利用C#和一款优秀的库,将XML数据自动化地写入Excel文件,有需要的小伙伴可以... 目录理解XML数据结构与Excel的对应关系引入高效工具:使用Spire.XLS for .NETC

Nginx更新SSL证书的实现步骤

《Nginx更新SSL证书的实现步骤》本文主要介绍了Nginx更新SSL证书的实现步骤,包括下载新证书、备份旧证书、配置新证书、验证配置及遇到问题时的解决方法,感兴趣的了解一下... 目录1 下载最新的SSL证书文件2 备份旧的SSL证书文件3 配置新证书4 验证配置5 遇到的http://www.cppc

Nginx之https证书配置实现

《Nginx之https证书配置实现》本文主要介绍了Nginx之https证书配置的实现示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起... 目录背景介绍为什么不能部署在 IIS 或 NAT 设备上?具体实现证书获取nginx配置扩展结果验证

SpringBoot整合 Quartz实现定时推送实战指南

《SpringBoot整合Quartz实现定时推送实战指南》文章介绍了SpringBoot中使用Quartz动态定时任务和任务持久化实现多条不确定结束时间并提前N分钟推送的方案,本文结合实例代码给大... 目录前言一、Quartz 是什么?1、核心定位:解决什么问题?2、Quartz 核心组件二、使用步骤1

使用Redis实现会话管理的示例代码

《使用Redis实现会话管理的示例代码》文章介绍了如何使用Redis实现会话管理,包括会话的创建、读取、更新和删除操作,通过设置会话超时时间并重置,可以确保会话在用户持续活动期间不会过期,此外,展示了... 目录1. 会话管理的基本概念2. 使用Redis实现会话管理2.1 引入依赖2.2 会话管理基本操作

mybatis-plus分表实现案例(附示例代码)

《mybatis-plus分表实现案例(附示例代码)》MyBatis-Plus是一个MyBatis的增强工具,在MyBatis的基础上只做增强不做改变,为简化开发、提高效率而生,:本文主要介绍my... 目录文档说明数据库水平分表思路1. 为什么要水平分表2. 核心设计要点3.基于数据库水平分表注意事项示例