栈的数据的--新增、删除、输出

本文介绍了一个简单的栈数据结构实现,包括入栈、出栈和显示栈内元素的功能。使用C语言编写,通过命令行界面进行交互。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

栈的数据的–新增、删除、输出

#include<stdio.h>
#include<stdlib.h>
#include<conio.h>

#define MAX 100
void push_f(void);         //入栈函数
void pop_f(void);          //出栈函数
void list_f(void);         //输出函数

char item[MAX][20];
int top = -1;

void main(void)
{
    char option;
    while (1)
    {
        printf("\n*************************************\n");
        printf("           <1>insert(push)\n");
        printf("           <2>delete(push)\n"); 
        printf("           <3>list(push)\n");
        printf("           <4>quit(push)\n");
        printf("\n*************************************\n");
        printf("Please enter your  choice......");
        option = _getche();
        switch (option)
        {
        case '1':
            push_f();
            break;
        case '2':
            pop_f();
            break;
        case'3':
            list_f();
            break;
        case'4':
            exit(0);
        }
    }
}
void push_f()
{
    if (top >= MAX - 1)         //当堆栈已满时,显示错误
        printf("\n\nStack is full!\n");
    else
    {
        top++;
        printf("\n\nPlease enter item to insert:");
        gets(item[top]);
    }
}
void pop_f()
{
    if (top < 0)                    //当堆栈没有数据存在时,则显示错误
        printf("\n\nNo item,stack is empty!\n");
    else
    {
        printf("\n\nItem %s deleted\n",item[top]);
        top--;
    }
}
void list_f()
{
    int count = 0, i;
    if (top < 0)
        printf("\n\nNo item,stack is empty\n");
    else
    {
        printf("\n\n ITEM\n");
        printf("---------------------------------------\n");
        for (i = 0; i <= top; i++)
        {
            printf("       %-20s\n",item[i]);
            count++;
            if (count % 20 == 0) getchar();
        }
        printf("---------------------------------------\n");
        printf("    Total item:%d\n",count);
        getchar();
    }
}

测试1
测试2
测试3

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值