Austin116 2017-12-01 08:17 采纳率: 100%
浏览 1140
已采纳

栈 队列 基础数据结构

1.利用数组实现两种基础的数据结构:队列(queue,先进先出)和栈(stack,先进后出)。
首先定义一个全局数组int arr[100]; (定义在所有函数之前,#include语句之后)。
 
1)队列— 仅支持两种操作
int dequeue() 函数返回队列最前面的元素,并将其从队列中删除。
void enqueue(int a)函数将a的值插入队列的末尾。
2)栈支持两种操作
int pop() 函数返回栈最上面的元素,并将其从中删除。
void push(int a)函数将a的值插入栈的最上面。

现在有两个程序 (C语言)
#include
int arr[100];
int qh = 100; //队头
int qt = 100; //队尾
int ERR = 9999999;
int dequeue()
{
if (qh <= qt) { printf("队空"); return ERR; }
int x = arr[qh];
qh--;
return x;
}
void enqueue(int a)
{
if (qt == 0) { printf("队满"); return ERR; }
arr[qt] = a;
qt--;
}
int main()
{
enqueue(1);
enqueue(2);
enqueue(3);
printf("%d\n", dequeue());
enqueue(4);
printf("%d\n", dequeue());
printf("%d\n", dequeue());
printf("%d\n", dequeue());
printf("%d\n", dequeue());
}

#include
int arr[100];
int top = -1;
int ERR = 9999999;
int pop()
{
if (top < 0) { printf("堆栈空"); return ERR; }
return arr[top--];
}
void push(int a)
{
top++;
if (top >= 100) { printf("堆栈满"); return ERR; }
arr[top] = a;
}
int main()
{
push(1);
push(2);
push(3);
push(4);
printf("%d\n", pop());
printf("%d\n", pop());
printf("%d\n", pop());
printf("%d\n", pop());
printf("%d\n", pop());
}

现在要加这样一个要求
在main函数中设计一个死循环,询问用户操作类型并根据操作类型返回结果(dequeue或pop),或者进一步询问用户操作数(enqueue或push)
 
请帮忙修改一下

  • 写回答

5条回答 默认 最新

  • threenewbee 2017-12-01 11:53
    关注
     #include <stdio.h>
    int arr1[100];
    int qh = 100; //队头
    int qt = 100; //队尾
    int ERR = 9999999;
    int dequeue()
    {
    if (qh <= qt) { printf("队空"); return ERR; }
    int x = arr1[qh];
    qh--;
    return x;
    }
    void enqueue(int a)
    {
    if (qt == 0) { printf("队满"); return ERR; }
    arr1[qt] = a;
    qt--;
    }
    int arr[100];
    int top = -1;
    int ERR = 9999999;
    int pop()
    {
    if (top < 0) { printf("堆栈空"); return ERR; }
    return arr[top--];
    }
    void push(int a)
    {
    top++;
    if (top >= 100) { printf("堆栈满"); return ERR; }
    arr[top] = a;
    }
    void testqueue()
    {
    int ch;
    printf("1 enqueue 2 dequeue 3 exit");
    while (1)
    {
    scanf("%d", &ch);
    int n;
    if (ch == 1) { printf("input number:"); scanf("%d", &n); enqueue(n); }
    if (ch == 2) printf("%d", dequeue();)
    if (ch == 3) break;
    }
    }
    void teststack()
    {
    int ch;
    printf("1 push 2 pop 3 exit");
    while (1)
    {
    scanf("%d", &ch);
    int n;
    if (ch == 1) { printf("input number:"); scanf("%d", &n); push(n); }
    if (ch == 2) printf("%d", pop();)
    if (ch == 3) break;
    }
    }
    int main()
    {
    int ch;
    printf("1 queue 2 stack 3 exit");
    while (1)
    {
    scanf("%d", &ch);
    if (ch == 1) testqueue();
    if (ch == 2) teststack();
    if (ch == 3) break;
    }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题