有关 基础数据结构 的问题

  1. 利用数组实现两种基础的数据结构:队列(queue,先进先出)和栈(stack,先进后出)。 首先定义一个全局数组int arr[100]; (定义在所有函数之前,#include语句之后)。

1) 队列— 仅支持两种操作
int dequeue() 函数返回队列最前面的元素,并将其从队列中删除。
void enqueue(int a)函数将a的值插入队列的末尾。
2)栈支持两种操作
int pop() 函数返回栈最上面的元素,并将其从中删除。
void push(int a)函数将a的值插入栈的最上面。

半夜困了,如果你采纳了,可以帮你写一写。

是java还是c++?

是c 不是c++ 啊 要凑够10个字

1
2
3
4
队空9999999

#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 <stdio.h>
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());
}

4
3
2
1
堆栈空9999999