C 程序实现栈


2022年5月1日, Learn eTutorial
1726

为了更好地理解,我们始终建议您学习下面列出的C语言编程基础主题

什么是栈?

在这个 C 程序中,我们需要实现一个栈。栈是一种线性数据结构,它使用**后进先出 (LIFO)** 的原则来执行 **push**(插入操作)、**pop**(删除操作)和 **display** 栈。为此,我们必须更多地了解栈及其功能。栈仅仅是一种线性数据结构,其中的操作按照特定顺序执行。在此程序中,我们使用 3 种操作。它们是

  1. 使用三个函数进行操作,如 **Push**、**Pop** 和 **Display**。
  2. 使用 Switch case 语句访问函数
  3. 退出

对栈执行哪些操作?

栈中主要执行三种操作。它们是

  • Push  - 将项目添加到栈中。如果栈因此而满,则称为溢出条件。
  • Pop  - 它与 push 函数相反,即它从栈中删除一个项目。此处项目以与它们被 push 的顺序相反的顺序出现。当栈为空时,此处发生下溢条件。
  • IsEmpty - 如果栈为空,则返回 true,否则返回 false。

如何在 C 中实现栈?

我们主要可以通过两种方式实现栈。它们是

  • 使用数组
  • 使用链表

栈中如何完成 push、pop 和 display?

在这个 C 程序中,在将头文件导入程序后,我们声明栈的变量和结构以及 push、pop 和 display 函数。现在我们为用户显示菜单,以便选择要对栈执行的操作。使用 switch case 语句选择对栈的操作。选择的 case 取决于用户的选择。现在我们调用相应的函数来执行 push、pop 或 display 等操作。

在 push 操作中,我们声明一个变量,并为用户提供一个窗口来输入元素。现在将元素插入栈中。同样,我们为用户提供一个窗口来删除。在删除任何内容之前检查栈是否不为空。如果栈不为空,则删除顶部并将顶部设置为顶部 -1。最后,使用 for 循环显示栈元素。

算法

步骤 1:包含头文件以在 C 程序中使用内置头文件。

步骤 2:声明带有成员 **stk[], top** 的结构化栈。

步骤 3:声明类型为 **STACK** 的变量 s。

步骤 4:声明函数 void **push(void), void pop(void), void display(void)**。

步骤 5:声明变量 **choice**、**option** 为整数。

步骤 6:设置 **s.top=-1** 并显示 STACK OPERATION

步骤 7:使用带有选项的 `while loop`,将菜单显示为 1 用于 push,2 用于 pop,3 用于 display,4 用于 exit。

步骤 8:将选择读入变量 **choice**。

步骤 9:如果选择是 1,则调用函数 **push()**。

步骤 10:如果选择是 2,则调用函数 **pop()**。

步骤 11:如果选择是 3,则调用函数 **display()**。

步骤 12:如果选择是 4,则调用 return。

步骤 13:显示“您想继续吗”并将选项读入 option。并重复步骤 8。

函数 void push()

步骤 1:声明整数变量 **num**。

步骤 2:检查如果 **s.top=MAXSIZE-1** 则显示栈已满并返回,否则执行步骤 3。

步骤 3:将要 push 的元素读入变量 **num**,**s.top=s.top+1** 增量并将 **s.stk[s.top]=num** 放置并返回。

函数 void pop()

步骤 1:声明整数变量 **num**。

步骤 2:检查如果 **s.top=-1** 则显示栈为空并返回 s.top,否则执行步骤 3。

步骤 3:设置 **num=s.stk[s.top]**。

步骤 4:将弹出的元素显示为 **s.stk[s.top]**

步骤 5:递减 **s.top=s.top-1**。

步骤 5:返回 **num**。

函数 void display()

步骤 1:声明整数变量 **i**。

步骤 2:检查如果 **s.top=-1** 则显示栈为空并返回 s.top,否则执行步骤 3。

步骤 3:显示栈的状态。

步骤 4:使用带有条件 **i>=0** 的 `for loop` 显示 **s.stk[i]**。

步骤 5:将“**i**”递减 1。并重复步骤 4。

C 语言源代码

                                          #include <stdio.h>


#define MAXSIZE 5
struct stack /* Structure definition for stack */ {
  int stk[MAXSIZE];
  int top;
};
typedef struct stack STACK;
STACK s;
void push(void); /* Function declaration/Prototype*/
int pop(void);
void display(void);
void main() {
  int choice;
  int option;

  s.top = -1;
  printf("STACK OPERATION\n");
  while (option) {
    printf("------------------------------------------\n");
    printf("   1 --> PUSH            \n");
    printf("   2 --> POP            \n");
    printf("   3 --> DISPLAY            \n");
    printf("   4 --> EXIT     \n");
    printf("------------------------------------------\n");
    printf("Enter your choice\n");
    scanf("%d", & choice);
    switch (choice) {
    case 1:
      push();
      break;
    case 2:
      pop();
      break;
    case 3:
      display();
      break;
    case 4:
      return;
    }
    fflush(stdin);
    printf("Do you want to continue(Type 0 or 1)?\n");
    scanf("%d", & option);
  }
}
void push() /*Function to add an element to the stack*/ {
  int num;
  if (s.top == (MAXSIZE - 1)) {
    printf("Stack is Full\n");
    return;
  } else {
    printf("Enter the element to be pushed\n");
    scanf("%d", & num);
    s.top = s.top + 1;
    s.stk[s.top] = num;
  }
  return;
}
int pop() /*Function to delete an element from the stack*/ {
  int num;
  if (s.top == -1) {
    printf("Stack is Empty\n");
    return (s.top);
  } else {
    num = s.stk[s.top];
    printf("poped element is = %d\n", s.stk[s.top]);
    s.top = s.top - 1;
  }
  return (num);
}
void display() /*Function to display the status of the stack*/ {
  int i;
  if (s.top == -1) {
    printf("Stack is empty\n");
    return;
  } else {
    printf("\nThe status of the stack is\n");
    for (i = s.top; i >= 0; i--) {
      printf("%d\n", s.stk[i]);
    }
  }
  printf("\n");
}
                                      

输出

STACK OPERATION
------------------------------------------
 1       -->     PUSH

        2       -->     POP

 3       -->     DISPLAY

        4       -->     EXIT
------------------------------------------
Enter your choice
1

Enter the element to be pushed
23

Do you want to continue(Type 0 or 1)?
1
------------------------------------------
 1       -->     PUSH

        2       -->     POP

 3       -->     DISPLAY

        4       -->     EXIT
------------------------------------------
Enter your choice
1

Enter the element to be pushed
45

Do you want to continue(Type 0 or 1)?
1
------------------------------------------
    1       -->     PUSH

        2       -->     POP

    3       -->     DISPLAY

        4       -->     EXIT
------------------------------------------
Enter your choice
1

Enter the element to be pushed
78

Do you want to continue(Type 0 or 1)?
1
------------------------------------------
        1       -->     PUSH

        2       -->     POP

 3       -->     DISPLAY

        4       -->     EXIT
------------------------------------------
Enter your choice
3

The status of the stack is
78
45
23

Do you want to continue(Type 0 or 1)?
1
------------------------------------------
        1       -->     PUSH

 2       -->     POP

        3       -->     DISPLAY

        4       -->     EXIT
------------------------------------------
Enter your choice
2

poped element is = 78
Do you want to continue(Type 0 or 1)?
1
------------------------------------------
        1       -->     PUSH

        2       -->     POP

        3       -->     DISPLAY

        4       -->     EXIT
------------------------------------------
Enter your choice
3

The status of the stack is
45
23
Do you want to continue(Type 0 or 1)?
0