栈 队列 基础数据结构

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)
 
请帮忙修改一下

 #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;
}
}

只写这个吧,另一个一样

 #include"iostream"
using namespace std;
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("堆栈满"); }
arr[top] = a;
}

int main()
{
    while(true){
        int n;
        cout<<"请选择操作1.push()2.pop()"<<endl;
        cin>>n;
        if(n==1){
            int m;
            cout<<"请输入数字"<<endl;
            cin>>m;
            push(m);
        }else if(n==2)
        {
            cout<<pop()<<endl;
        }else{
            cout<<"你的输入有误";
        }
    }
}

数组在解决这种变长数据结构非常合适,用双向链表不是更好

#include"iostream"
using namespace std;
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("堆栈满"); }
arr[top] = a;
}

int main()
{
while(true){
int n;
cout<<"请选择操作1.push()2.pop()"< cin>>n;
if(n==1){
int m;
cout<<"请输入数字"< cin>>m;
push(m);
}else if(n==2)
{
cout<<pop()<<endl;
}else{
cout<<"你的输入有误";
}
}
}

#include"iostream"
using namespace std;
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("堆栈满"); }
arr[top] = a;
}

int main()
{
while(true){
int n;
cout<<"请选择操作1.push()2.pop()"< cin>>n;
if(n==1){
int m;
cout<<"请输入数字"< cin>>m;
push(m);
}else if(n==2)
{
cout<<pop()<<endl;
}else{
cout<<"你的输入有误";
}
}
}