如何在 C 语言程序中实现堆栈

介绍

堆栈是一种线性数据结构,是一组相同类型的项目。

在堆栈中,元素的插入和删除只发生在一个终点上。堆栈的行为被描述为Last In, First Out(LIFO)。当一个元素被到堆栈时,它将成为首个从堆栈中跳出的元素。要达到最古老的输入元素,你必须跳出所有以前的元素。

在本文中,您将了解堆栈数据结构的概念及其使用C中的数组的实现。

在Stacks上执行的操作

以下是由stacks服务的基本操作。

●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●

Stacks 基础机械学

最初,设置一个指针(‘顶部’),以保持堆栈中顶部项目的跟踪,堆栈被初始化为‘-1’。

然后,通过将顶部-1进行比较来确定堆栈是否空。

随着元素添加到堆栈,更新顶部的位置。

一旦元素出现或删除,顶部元素将被删除,并更新顶部的位置。

C 中的 Stack 实施

堆栈可以使用结构、指针、数组或链接列表来表示。

此示例使用 C 中的数组来实现堆栈:

 1#include <stdio.h>
 2
 3#include <stdlib.h>
 4
 5#define SIZE 4
 6
 7int top = -1, inp_array[SIZE];
 8void push();
 9void pop();
10void show();
11
12int main()
13{
14    int choice;
15
16    while (1)
17    {
18        printf("\nPerform operations on the stack:");
19        printf("\n1.Push the element\n2.Pop the element\n3.Show\n4.End");
20        printf("\n\nEnter the choice: ");
21        scanf("%d", &choice);
22
23        switch (choice)
24        {
25        case 1:
26            push();
27            break;
28        case 2:
29            pop();
30            break;
31        case 3:
32            show();
33            break;
34        case 4:
35            exit(0);
36
37        default:
38            printf("\nInvalid choice!!");
39        }
40    }
41}
42
43void push()
44{
45    int x;
46
47    if (top == SIZE - 1)
48    {
49        printf("\nOverflow!!");
50    }
51    else
52    {
53        printf("\nEnter the element to be added onto the stack: ");
54        scanf("%d", &x);
55        top = top + 1;
56        inp_array[top] = x;
57    }
58}
59
60void pop()
61{
62    if (top == -1)
63    {
64        printf("\nUnderflow!!");
65    }
66    else
67    {
68        printf("\nPopped element: %d", inp_array[top]);
69        top = top - 1;
70    }
71}
72
73void show()
74{
75    if (top == -1)
76    {
77        printf("\nUnderflow!!");
78    }
79    else
80    {
81        printf("\nElements present in the stack: \n");
82        for (int i = top; i >= 0; --i)
83            printf("%d\n", inp_array[i]);
84    }
85}

此程序为用户提供了四种选项:

  1. 点击元素
  2. 点击元素
  3. 显示
  4. 结束

它等待用户输入一个号码。

如果用户选择1,该程序会处理push()。首先,它会检查顶部是否等于SIZE – 1。如果True显示Overflow!!否则,用户会被要求提供新元素添加到堆栈 *如果用户选择2,该程序会处理pop()。首先,它会检查顶部是否等于-1。如果True显示Underflow!!否则,顶部元素会被删除,并且程序会输出结果的堆栈 *如果用户选择3,该程序会处理show()。首先,它会检查顶部是否等于-1。如果,Underflow_M1显示。(如果用户选择了3,则`K

执行此代码到 push() 数字 "10"onto 堆栈:

 1[secondary_label Output]
 2Perform operations on the stack:
 31.Push the element
 42.Pop the element
 53.Show
 64.End
 7
 8Enter the choice: 1
 9
10Enter the element to be inserted onto the stack: 10

然后在堆栈上的元素`显示():

 1[secondary_label Output]
 2Perform operations on the stack:
 31.Push the element
 42.Pop the element
 53.Show
 64.End
 7
 8Enter the choice: 3
 9
10Elements present in the stack: 
1110

然后是`POP():

1[secondary_label Output]
2Perform operations on the stack:
31.Push the element
42.Pop the element
53.Show
64.End
7
8Enter the choice: 2
9Popped element: 10

现在,堆栈是空的. 再次尝试 pop():

1[secondary_label Output]
2Perform operations on the stack:
31.Push the element
42.Pop the element
53.Show
64.End
7
8Enter the choice: 3
9Underflow!!

继续尝试这个程序来了解堆栈是如何工作的。

Stack 操作的时间复杂性

只有一个元素可以在堆栈中一次访问。

在堆栈上执行‘push()’和‘pop()’操作时,需要‘O(1)’的时间。

结论

在本文中,您了解了堆栈数据结构的概念及其在C中使用数组的实现。

堆栈被用来解决一些一般问题,如:

  1. 哈诺伊之塔
  2. N-Queens 问题
  3. 注入前缀转换

继续学习如何在C中创建队列(https://andsky.com/tech/tutorials/queue-in-c)和如何在C中初始化阵列(https://andsky.com/tech/tutorials/initialize-an-array-in-c)。

Published At
Categories with 技术
comments powered by Disqus