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

相关文章

Spring Shell 命令行实现交互式Shell应用开发

《SpringShell命令行实现交互式Shell应用开发》本文主要介绍了SpringShell命令行实现交互式Shell应用开发,能够帮助开发者快速构建功能丰富的命令行应用程序,具有一定的参考价... 目录引言一、Spring Shell概述二、创建命令类三、命令参数处理四、命令分组与帮助系统五、自定义S

SpringBatch数据写入实现

《SpringBatch数据写入实现》SpringBatch通过ItemWriter接口及其丰富的实现,提供了强大的数据写入能力,本文主要介绍了SpringBatch数据写入实现,具有一定的参考价值,... 目录python引言一、ItemWriter核心概念二、数据库写入实现三、文件写入实现四、多目标写入

Android Studio 配置国内镜像源的实现步骤

《AndroidStudio配置国内镜像源的实现步骤》本文主要介绍了AndroidStudio配置国内镜像源的实现步骤,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,... 目录一、修改 hosts,解决 SDK 下载失败的问题二、修改 gradle 地址,解决 gradle

SpringSecurity JWT基于令牌的无状态认证实现

《SpringSecurityJWT基于令牌的无状态认证实现》SpringSecurity中实现基于JWT的无状态认证是一种常见的做法,本文就来介绍一下SpringSecurityJWT基于令牌的无... 目录引言一、JWT基本原理与结构二、Spring Security JWT依赖配置三、JWT令牌生成与

Go 语言中的select语句详解及工作原理

《Go语言中的select语句详解及工作原理》在Go语言中,select语句是用于处理多个通道(channel)操作的一种控制结构,它类似于switch语句,本文给大家介绍Go语言中的select语... 目录Go 语言中的 select 是做什么的基本功能语法工作原理示例示例 1:监听多个通道示例 2:带

SpringBoot实现微信小程序支付功能

《SpringBoot实现微信小程序支付功能》小程序支付功能已成为众多应用的核心需求之一,本文主要介绍了SpringBoot实现微信小程序支付功能,文中通过示例代码介绍的非常详细,对大家的学习或者工作... 目录一、引言二、准备工作(一)微信支付商户平台配置(二)Spring Boot项目搭建(三)配置文件

基于Python实现高效PPT转图片工具

《基于Python实现高效PPT转图片工具》在日常工作中,PPT是我们常用的演示工具,但有时候我们需要将PPT的内容提取为图片格式以便于展示或保存,所以本文将用Python实现PPT转PNG工具,希望... 目录1. 概述2. 功能使用2.1 安装依赖2.2 使用步骤2.3 代码实现2.4 GUI界面3.效

MySQL更新某个字段拼接固定字符串的实现

《MySQL更新某个字段拼接固定字符串的实现》在MySQL中,我们经常需要对数据库中的某个字段进行更新操作,本文就来介绍一下MySQL更新某个字段拼接固定字符串的实现,感兴趣的可以了解一下... 目录1. 查看字段当前值2. 更新字段拼接固定字符串3. 验证更新结果mysql更新某个字段拼接固定字符串 -

java实现延迟/超时/定时问题

《java实现延迟/超时/定时问题》:本文主要介绍java实现延迟/超时/定时问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录Java实现延迟/超时/定时java 每间隔5秒执行一次,一共执行5次然后结束scheduleAtFixedRate 和 schedu

Java Optional避免空指针异常的实现

《JavaOptional避免空指针异常的实现》空指针异常一直是困扰开发者的常见问题之一,本文主要介绍了JavaOptional避免空指针异常的实现,帮助开发者编写更健壮、可读性更高的代码,减少因... 目录一、Optional 概述二、Optional 的创建三、Optional 的常用方法四、Optio