Basic operation and menu application of data structure stack (C program implementation)

//The reference book is the data structure and algorithm analysis of the Machinery Industry Press (C language description);

//The implementation of the stack stack has 2 packages, which should be run after comparison;

//The two stacks are implemented in different ways, but the test program is the same main program, which has strong portability;

//This program is a portability program;

//Can compile and run under Linux/Mac os/Windows;

//If you have any shortcomings, please raise them, and the blogger will do their best to modify;

//If it is useful to you, please like to collect or share with others;

// Plagiarism and reprinting without permission are strictly prohibited;

//The source code is here, I hope to inspire you;

//----------------------------------------------------------------------------
//main.c

#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include "stack.h"

int show_menu(void);            //显示主菜单;
int get_first(void);            //获取用户输入的第1个字符;
void eatline(void);             //清空输入缓冲区;
elemtype input(void);           //获取用户输入的数据元素;
void choice(int ch, stack *st); //操作栈中的数据元素;

int main(void)
{
    
    
    int ch;
    stack st;

    init_stack(&st);
    while ((ch = show_menu()) != 'q')
    {
    
    
        choice(ch, &st);
    }
    destroy_stack(&st);
    puts("本程序完成!");

    return 0;
}

int show_menu(void)
{
    
    
    int ch;

    puts("========================================");
    puts("a) 判断栈是否为空");
    puts("b) 查看栈当前长度");
    puts("c) 获取栈顶数据元素");
    puts("d) 添加一个数据元素至栈顶");
    puts("e) 令栈顶数据元素出栈");
    puts("f) 遍历此栈");
    puts("q) 退出本程序");
    puts("========================================");
    printf("请您输入选择:");
    ch = get_first();
    while (strchr("abcdefq", ch) == NULL)
    {
    
    
        printf("您的选择无效!请重新输入:");
        ch = get_first();
    }
    return ch;
}

int get_first(void)
{
    
    
    int ch;

    do
    {
    
    
        ch = tolower(getchar());
    } while (isspace(ch));
    eatline();

    return ch;
}

void eatline(void)
{
    
    
    while (getchar() != '\n')
        continue;
    return;
}

elemtype input(void)
{
    
    
    elemtype value;

    printf("请您输入一个数据元素(整型元素):");
    while (scanf("%d", &value) != 1)
    {
    
    
        eatline();
        printf("数据无效!请重新输入:");
    }
    eatline();
    return value;
}

void choice(int ch, stack *st)
{
    
    
    elemtype val;

    switch (ch)
    {
    
    
    case 'a':
    {
    
    
        printf("此栈%s!\n", isempty(st) ? "为空" : "非空");
        break;
    }
    case 'b':
    {
    
    
        printf("当前栈中数据元素个数:%d个\n", stack_length(st));
        break;
    }
    case 'c':
    {
    
    
        val = get_top_value(st);
        if (val != -1)
        {
    
    
            printf("栈顶元素是: %d\n", val);
        }
        break;
    }
    case 'd':
    {
    
    
        val = input();
        push(st, val);
        break;
    }
    case 'e':
    {
    
    
        pop(st, &val);
        break;
    }
    case 'f':
    {
    
    
        if (isempty(st))
        {
    
    
            printf("此栈为空栈!无法遍历!\n");
            break;
        }
        traverse(st);
        break;
    }
    }
    printf("\n\n\n\n\n\n\n\n\n\n\n\n");
    return;
}

//stack(array).h

#ifndef STACK_H_
#define STACK_H_
#include <stdbool.h>
#define MAXSIZE 100

typedef int elemtype;

typedef struct
{
    
    
    elemtype *array; //动态数组;
    int capacity;    //栈的容量;
    int top;         //栈顶指针;
} stack;

//1) 构造一个空栈;
void init_stack(stack *st);
/*----------------------------------------------------------------------*/

//2) 销毁栈存储空间;
void destroy_stack(stack *st);
/*----------------------------------------------------------------------*/

//3) 判断栈是否为空;
bool isempty(stack *st);
/*----------------------------------------------------------------------*/

//4) 判断栈是否已满;
bool isfull(stack *st);
/*----------------------------------------------------------------------*/

//5) 获取栈的当前数据元素个数;
int stack_length(stack *st);
/*----------------------------------------------------------------------*/

//6) 若栈非空则获取栈顶数据元素;
elemtype get_top_value(stack *st);
/*----------------------------------------------------------------------*/

//7) 添加一个数据元素到栈顶;
void push(stack *st, elemtype val);
/*----------------------------------------------------------------------*/

//8) 若栈非空则令栈顶元素出栈;
void pop(stack *st, elemtype *val);
/*----------------------------------------------------------------------*/

//9) 若栈非空则遍历栈中数据元素;
void traverse(stack *st);
/*----------------------------------------------------------------------*/

#endif

//stack(array).c

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

void init_stack(stack *st)
{
    
    
    st->array = (elemtype *)malloc(sizeof(elemtype));
    if (NULL == st->array)
    {
    
    
        fprintf(stderr, "动态内存分配失败!本程序退出!\n");
        exit(EXIT_FAILURE);
    }
    st->capacity = MAXSIZE;
    st->top = -1;
    return;
}

void destroy_stack(stack *st)
{
    
    
    free(st->array);
    return;
}

bool isempty(stack *st)
{
    
    
    return -1 == st->top;
}

bool isfull(stack *st)
{
    
    
    return st->top == st->capacity - 1;
}

int stack_length(stack *st)
{
    
    
    return st->top + 1;
}

elemtype get_top_value(stack *st)
{
    
    
    if (!isempty(st))
    {
    
    
        return st->array[st->top];
    }
    fprintf(stderr, "空栈无法获取栈顶元素!\n");
    return -1;
}

void push(stack *st, elemtype val)
{
    
    
    if (isfull(st))
    {
    
    
        printf("栈已满!元素无法进行入栈操作!\n");
        return;
    }
    st->array[++st->top] = val;
    printf("成功添加数据元素%d至栈顶!\n", val);
    return;
}

void pop(stack *st, elemtype *val)
{
    
    
    if (isempty(st))
    {
    
    
        printf("空栈!无法进行出栈操作!\n");
        return;
    }
    *val = st->array[st->top--];
    printf("栈顶元素%d出栈成功!\n", *val);
    return;
}

void traverse(stack *st)
{
    
    
    int i;

    printf("从栈顶到栈底元素如下:\n");
    for (i = st->top; i >= 0; i--)
    {
    
    
        printf("%d ", st->array[i]);
    }
    return;
}

//stack(linkedlist).h

#ifndef STACK_H_
#define STACK_H_
#include <stdbool.h>

typedef int elemtype;

typedef struct node
{
    
    
    elemtype data;
    struct node *next;
} stack;
typedef struct node pnode;

//1) 构造一个空栈;
void init_stack(stack *st);
/*----------------------------------------------------------------------*/

//2) 销毁栈存储空间;
void destroy_stack(stack *st);
/*----------------------------------------------------------------------*/

//3) 判断栈是否为空;
bool isempty(stack *st);
/*----------------------------------------------------------------------*/

//4) 获取栈的当前数据元素个数;
int stack_length(stack *st);
/*----------------------------------------------------------------------*/

//5) 若栈非空则获取栈顶数据元素;
elemtype get_top_value(stack *st);
/*----------------------------------------------------------------------*/

//6) 添加一个数据元素到栈顶;
void push(stack *st, elemtype val);
/*----------------------------------------------------------------------*/

//7) 若栈非空则令栈顶元素出栈;
void pop(stack *st, elemtype *val);
/*----------------------------------------------------------------------*/

//8) 若栈非空则遍历栈中数据元素;
void traverse(stack *st);
/*----------------------------------------------------------------------*/

#endif

//stack(linkedlist).c

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

void init_stack(stack *st)
{
    
    
    st->next = NULL;
    return;
}

void destroy_stack(stack *st)
{
    
    
    elemtype val;

    while (!isempty(st))
    {
    
    
        pop(st, &val);
    }
    return;
}

bool isempty(stack *st)
{
    
    
    return NULL == st->next;
}

int stack_length(stack *st)
{
    
    
    int len = 0;
    pnode *pos = st->next;

    while (pos != NULL)
    {
    
    
        ++len;
        pos = pos->next;
    }
    return len;
}

elemtype get_top_value(stack *st)
{
    
    
    if (!isempty(st))
    {
    
    
        return st->next->data;
    }
    fprintf(stderr, "空栈无法获取栈顶元素!\n");
    return -1;
}

void push(stack *st, elemtype val)
{
    
    
    pnode *new_node = (pnode *)malloc(sizeof(pnode));
    if (NULL == new_node)
    {
    
    
        fprintf(stderr, "动态内存分配失败!本程序退出!\n");
        exit(EXIT_FAILURE);
    }
    new_node->data = val;
    new_node->next = st->next;
    st->next = new_node;
    printf("成功添加数据元素%d至栈顶!\n", val);
    return;
}

void pop(stack *st, elemtype *val)
{
    
    
    if (isempty(st))
    {
    
    
        printf("空栈!无法进行出栈操作!\n");
        return;
    }
    pnode *temp;
    *val = st->next->data;
    temp = st->next;
    st->next = temp->next;
    free(temp);
    printf("栈顶元素%d出栈成功!\n", *val);
    return;
}

void traverse(stack *st)
{
    
    
    pnode *pos = st->next;

    printf("从栈顶到栈底元素如下:\n");
    while (pos != NULL)
    {
    
    
        printf("%d ", pos->data);
        pos = pos->next;
    }
    return;
}

//----------------------------------------------------------------------------

//----------------------------January 12, 2021-------------- -----------------

Guess you like

Origin blog.csdn.net/m0_46181359/article/details/112546133