C语言——栈的实现(顺序栈,两个顺序栈共享空间,链式栈)

1.什么是栈

栈是一种只能在一端进行插入或者删除操作的线性表)。其中允许进行插入或者删除操作的一端称为栈顶。栈的插入和删除一般叫入栈和出栈。栈的顺序存储结构叫做==顺序栈==,栈的链式存储结构叫做==链栈==。

2.栈的特点

栈的特点是==后进先出==

3.顺序栈

头文件

#ifndef Stack
#define Stack
#define SIZE 10
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<stdbool.h>
typedef struct data {
    int num[SIZE];
    int top;
}stack;
bool init(stack * st);
bool push(stack * s, int n);
bool pop(stack * s);
void print(stack * s);
#endif

资源文件

#include"stack.h"
bool init(stack * st)
{
    st->top = -1;
    return true;
}
bool push(stack * s, int n)
{
    if (s->top == SIZE - 1) {
        return false;
    }
    s->top++;
    s->num[s->top] = n;
    printf("%d已入栈\n", n);
    return true;
}
bool pop(stack * s)
{
    if (s->top == -1) {
        return false;
    }
    printf("%d已被弹出\n", s->num[s->top]);
    s->top--;
    return true;
}
void print(stack * s)
{
    while (s->top != -1) {
        printf("%d\n", s->num[s->top--]);
    }
    return;
}

主函数

#include"stack.h"
int main(void)
{
    stack * a;
    a = (stack *)malloc(sizeof(stack));
    init(a);
    puts("请输入您需要入栈的个数");
    int n;
    scanf("%d", &n);
    int t;//临时变量
    for (int i = 0; i < n; i++) {
        printf("请输入值:");
        scanf("%d", &t);
        if (!push(a, t)) {
            printf("栈已满!\n");
        }
    }
    if (!pop(a)) {
        printf("栈是空的!\n");
    }
    print(a);
    return 0;
}

4.两栈共享空间

思想:在顺序栈的两个头都进行进栈出栈操作。

注意:栈是否满,以及栈是否空的判断条件是什么?


栈空:对栈底而言,top == -1即为空,对栈顶而言,top2 == SIZE即为栈空。

栈满:top + 1 == top2即为栈满


代码如下


stack.h

#ifndef STACK
#define STACK
#define SIZE 10
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
typedef struct stack {
    int top;
    int top2;
    int data[SIZE];
}stack;
void init(stack * s);
bool Pop(stack * s, int flag);
bool Push(stack * s, int n, int flag);
#endif

stack.c

#include"stack.h"
void init(stack * s)
{
    s->top = -1;
    s->top2 = SIZE;
    return;
}
bool Push(stack * s,int n, int flag)
{
    if (s->top2 + 1 == s->top2) {
        printf("栈已满!\n");
        return false;
    }
    if (flag == 1) {
        s->data[s->top] = n;
        s->top++;
        printf("%d已入栈!\n", n);
    }
    else if (flag == 2) {
        s->data[s->top2] = n;
        s->top2--;
        printf("%d已入栈!\n", n);
    }

    return true;
}
bool Pop(stack * s, int flag)
{
    if (flag == 1) {
        if (s->top == -1) {
            printf("空栈\n");
            return false;
        }
        printf("%d已出栈!\n", s->data[s->top--]);
    }
    if (flag == 2) {
        if (s->top2 == SIZE) {
            printf("空栈\n");
            return false;
        }
        printf("%d已出栈!\n", s->data[s->top2++]);
    }
    return true;
}

c.c

#include"stack.h"
int main(int argc, char * argv[])
{
    stack * s;
    s = (stack*)malloc(sizeof(stack));
    init(s);
    int flag;
    int val;//临时变量
    int n;
    printf("请输入您要操作的栈的序号(1 normal), (2 reverse):");
    scanf("%d", &flag);
    printf("请输入您需要入栈的个数:");
    scanf("%d", &n);
    for (int i = 0; i < n; i++) {
        scanf("%d", &val);
        Push(s, val, flag);
    }
    Pop(s, 1);
    return 0;
}

5.链式栈

结构与单链表相同,不同的是,需要将一个==top==指针,指向该链表的头部,需要注意的是,每次的操作,都是通过top指针来完成的,代码如下:

头文件

#ifndef STACK
#define STACK
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<stdbool.h>
#define SIZE 1000
typedef struct node {
    int data;
    struct node * next;
}stacknode;
typedef struct node2 {
    stacknode * top;
    int count;
}stack;
void init(stack * s);
bool push(stack * s, int n);
bool pop(stack * s);
void print(stack * s);
//void print(stack * s);
#endif

stack.c文件

#include"stack.h"
void init(stack *s)
{
    s->top = NULL;
    s->count = 0;
}
bool push(stack * s, int n)
{
    stacknode *p = (stacknode *)malloc(sizeof(stacknode));
    p->data = n;
    p->next = s->top;
    s->top = p;
    s->count++;
    printf("%d已入栈\n", p->data);
    return true;
}
bool pop(stack * s)
{
    if (s->top == NULL) {
        return false;
    }
    stacknode * p;
    p = s->top;
    s->top = s->top->next;
    s->count--;
    printf("%d已出栈\n", p->data);
    free(p);
    return true;
}
void print(stack * s)
{
    stacknode * p;
    p = s->top;
    if (p == NULL) {
        printf("No data\n");
        return;
    }
    while (p) {
        printf("%d ", p->data);
        p = p->next;
    }
    putchar('\n');
    return;
}

c.c

#include"stack.h"
int main(void)
{
    stack * s = (stack *)malloc(sizeof(stack));
    init(s);
    printf("请输入您需要入栈的个数:");
    int n;
    scanf("%d", &n);
    int t;
    for (int i = 0; i < n; i++) {
        scanf("%d", &t);
        push(s, t);
    }
    printf("该栈中一共有%d项\n",s->count);
    pop(s);
    print(s);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_40596811/article/details/82382099