用 C 语言完成堆栈实现

标签 c struct stack

我被要求做一个堆栈实现。我需要以下功能;

  1. 推送
  2. 流行音乐
  3. 已满
  4. 为空
  5. 偷看
  6. 显示整个数组

这是我写的。

#include <stdio.h>
#include <stdlib.h>
#define SIZE 5

/* Stack Structure  */
struct stack
{
    int s[SIZE];
    int top;
}st;

int main()
{

int option;

printf("+-------------------------------------+\n");
printf("1.Push\n2.Pop\n3.Check whether the stack is full\n4.Check whether the stack is empty\n5.Check the Top Element\n6.Display the Stack\n7.Exit\n");
printf("+-------------------------------------+\n");
printf("Enter Choice:\t");
scanf("%d", &option);

while(option == -99)
{
    switch(option)
    {
    case 1:
        push();
        break;
    case 2:
        pop();
        break;
    case 3:
        isFull();
        break;
    case 4:
        isEmpty();
        break;
    case 5:
        peek();
        break;
    case 6:
        display();
        break;
    case 7:
        printf("You Exited from the program");
        break;
    }
}
return 0;
}

/*Function to add an element to the stack*/
void push ()
{
int num;
if (st.top == (SIZE - 1))
{
    printf ("Stack is Full\n");
}
else
{
    printf ("Enter the element to be pushed\n");
    scanf ("%d", &num);
    st.top ++;
    st.s[st.top] = num;
}
}

/*Function to delete an element to the stack*/
int pop()
{
int num;
if (st.top == -1)
{
    printf ("Stack is Empty\n");
    return st.top;
}
else
{
    num = st.s[st.top];
    printf ("Popped element is = %d", st.s[st.top]);
    st.top --;
}
return (num);
}

/*Function to Check whether the stack is full*/
 void isFull()
 {

   if(st.top == SIZE - 1)
  printf("Stack is Full");
   else
  printf("Stack has %d elements", st.top - 1);
}

/*Function to Check whether the stack is Empty*/
void isEmpty()
{
if(st.top == -1)
  printf("Stack is Empty");
else
  printf("Stack has %d elements", st.top - 1);
}

/* Function to display the top element*/
void peek()
{
printf("Top most element: \t%d", st.s[st.top]);
}

/* Function to display the stack*/
void display ()
{
int i;
if (st.top == -1)
{
    printf ("Stack is empty\n");
}
else
{
    printf ("\n The status of the stack is \n");
    for (i = st.top; i >= 0; i--)
    {
        printf ("%d\n", st.s[i]);
    }
}
printf ("\n");
}

显示 0 个错误,11 个警告。

但是当我运行该程序时,它会在询问选择后结束。

输出:

+-------------------------------------+
1.Push
2.Pop
3.Check whether the stack is full
4.Check whether the stack is empty
5.Check the Top Element
6.Display the Stack
7.Exit
+-------------------------------------+
Enter Choice:   1

Process returned 0 (0x0)   execution time : 6.304 s
Press any key to continue.

我真的需要完成它。这是我的任务之一。请帮助我,非常感谢您抽出时间。 :-)

最佳答案

您正在退出,因为您输入的选项介于 1 和 7 之间,但 while 循环正在检查 -99。因此 while 循环会被跳过并退出。

我猜你真正想要做的是不断提示用户执行操作,直到他们退出。尝试考虑您实际希望在程序中循环哪些功能。

也不要害怕在代码中放入 print 语句并逐行跟踪流程。这将对您的调试有很大帮助。

祝你任务顺利!

关于用 C 语言完成堆栈实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47251127/

相关文章:

c++ - 使用结构统计()

c++ - 如何使用 CaptureStackBackTrace 来捕获异常堆栈,而不是调用堆栈?

c - 系统小程序 : Assertion failed - How can I solve?

c - 手动创建的 .exe 抛出错误 "This app can not run on your PC"

c - 如何将 "zd"说明符与 `printf()` 一起使用?

c - 将指针传递给 C 中的函数

在 C 中将项目强制转换到链表末尾

c - pthread 锁定不起作用

stack - 从向量中重复 pop() 项目的更简洁的方法是什么?

c++ - std::vector<std::array<T, N>> 或 std::array<std::vector<T>,N> 类型的数组如何存储在内存中?