21xrx.com
2024-11-05 12:14:39 Tuesday
登录
文章检索 我的文章 写文章
实现栈数据结构的C程序
2021-07-08 14:42:57 深夜i     --     --
C

C 语言中的堆栈程序:使用数组实现堆栈的 C 程序。

C程序

#include <stdio.h>
#include <stdlib.h>


int stack[5];
void push();
int pop();
void traverse();
int is_empty();
int top_element();
int top = 0;

int main()
{
  int element, choice;

  for (;;)
  {
    printf("Stack Operations.\n");
    printf("1. Insert into stack (Push operation).\n");
    printf("2. Delete from stack (Pop operation).\n");
    printf("3. Print top element of stack.\n");
    printf("4. Check if stack is empty.\n");
    printf("5. Traverse stack.\n");
    printf("6. Exit.\n");
    printf("Enter your choice.\n");
    scanf("%d",&choice);

    switch (choice)
    {
      case 1:
        if (top == 5)
          printf("Error: Overflow\n\n");
        else {
          printf("Enter a value to insert.\n");
          scanf("%d", &element);
          push(element);
        }
        break;

      case 2:
        if (top == 0)
          printf("Error: Underflow.\n\n");
        else {
          element = pop();
          printf("Element removed from the stack is %d.\n", element);
        }
        break;

      case 3:
        if (!is_empty()) {
          element = top_element();
          printf("Element at the top of the stack is %d\n\n", element);
        }
        else
          printf("The stack is empty.\n\n");
        break;

      case 4:
        if (is_empty())
          printf("The stack is empty.\n\n");
        else
          printf("The stack isn't empty.\n\n");
        break;

      case 5:
        traverse();
        break;

      case 6:
        exit(0);
    }
  }
}

void push(int value) {
  stack[top] = value;
  top++;
}

int pop() {
  top--;
  return stack[top];
}

void traverse() {
  int d;

  if (top == 0) {
    printf("The stack is empty.\n\n");
    return;
  }

  printf("There are %d elements in the stack.\n", top);

  for (d = top - 1; d >= 0; d--)
    printf("%d\n", stack[d]);
  printf("\n");
}

int is_empty() {
  if (top == 0)
    return 1;
  else
    return 0;
}

int top_element() {
  return stack[top-1];
}

 

  
  

评论区

{{item['qq_nickname']}}
()
回复
回复