栈的C语言实现

栈的链表实现

#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include "windows.h"

struct stack_node {
    int data;
    struct stack_node *next;
};

typedef struct stack_node *PtrToNode;
typedef PtrToNode Stack;

Stack create_stack();
void push_stack(Stack s, int data);
void pop_stack(Stack s);
int top_stack(Stack s);
int stack_is_empty(Stack s);

int main() 
{
    Stack stack = create_stack();        // 新建一个空栈
    int top_data,i;
    // 压栈操作,执行10次
    for (i = 0;i < 10;i++) {
        push_stack(stack, i);
    }
    // 出栈操作,执行1次
    pop_stack(stack);
    // 返回栈顶元素的值
    top_data = top_stack(stack);
    printf("%d\n", top_data);

    system("pause");
}

/* 创建一个空栈 */
Stack create_stack()
{
    Stack S;

    S = (Stack)malloc(sizeof(struct stack_node));
    if (S == NULL)
        printf("malloc fair!\n");
    S->next = NULL;

    return S;
}

/* PUSH 操作 */
void push_stack(Stack s,int data) 
{
    // 新建一个结点,用于存放压入栈内的元素,即新的栈顶
    PtrToNode head_node = (PtrToNode)malloc(sizeof(struct stack_node));
    if (head_node == NULL)
        printf("malloc fair!\n");

    head_node->data = data;            // 添加数据
    head_node->next = s->next;        // 新的栈顶 head_node 的 next 指针指向原来的栈顶 s->next
    s->next = head_node;            // s->next 现在指向新的栈顶
}

/* POP 操作 */
void pop_stack(Stack s) 
{
    PtrToNode head_node = (PtrToNode)malloc(sizeof(struct stack_node));
    if (head_node == NULL)
        printf("malloc fair!\n");

    // 先判断栈是否为空,若栈为空,则不能再进行出栈操作,报错
    if (stack_is_empty(s)) {
        printf("Error! Stack is empty!\n");
    }
    else {
        head_node = s->next;            // head_node 为栈顶
        s->next = head_node->next;        // s->next 指向 head_node->next ,即新的栈顶
        free(head_node);                // 释放原来栈顶元素所占的内存
    }
}

/* 查看栈顶元素 */
int top_stack(Stack s) 
{
    if (stack_is_empty(s)) {
        printf("Error! Stack is empty!\n");
        return 0;
    }
    else {
        return s->next->data;
    }
}

/* 判断栈是否为空 */
int stack_is_empty(Stack s) 
{
    return s->next == NULL;
}

栈的数组实现

#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include "windows.h"

#define MinStackSize 5
#define EmptyTOS -1

struct stack_array {
    int capacity;            // 栈的容量
    int top_of_stack;        // 栈顶的下标
    int *array;                // 用于存放栈的数组
};

typedef struct stack_array *ArrayRecord;
typedef ArrayRecord Stack;

Stack create_stack(int stack_capacity);
void make_empty(Stack s);
void push_stack(Stack s, int data);
int top_stack(Stack s);
void pop_stack(Stack s);
int stack_is_empty(Stack s);
int stack_is_full(Stack s);

int main()
{
    Stack stack = create_stack(100);
    int topdata, i;
    for (i = 0;i < 10;i++) {
        push_stack(stack, i);
    }
    pop_stack(stack);
    pop_stack(stack);
    topdata = top_stack(stack);
    printf("%d\n", topdata);

    system("pause");
}

/* 创建一个栈 */
Stack create_stack(int stack_capacity)
{
    Stack S;

    if (stack_capacity < MinStackSize)
        printf("Error! Stack size is too small!\n");

    S = (Stack)malloc(sizeof(struct stack_array));
    if (S == NULL)
        printf("malloc error!\n");

    S->array = (int *)malloc(sizeof(struct stack_array) * stack_capacity);
    if (S->array == NULL)
        printf("malloc error!\n");
    S->capacity = stack_capacity;

    make_empty(S);
    return S;
}

/* 创建一个空栈 */
void make_empty(Stack s)
{
    // 栈顶的下标为 -1 表示栈为空
    s->top_of_stack = EmptyTOS;
}

/* PUSH 操作 */
void push_stack(Stack s, int data)
{
    if (stack_is_full(s)) {
        printf("Error! Stack is full!\n");
    }
    else {
        s->top_of_stack++;
        s->array[s->top_of_stack] = data;
    }
}

/* POP 操作 */
void pop_stack(Stack s)
{
    if (stack_is_empty(s)) {
        printf("Error! Stack is empty!\n");
    }
    else {
        s->top_of_stack--;
    }
}

/* 返回栈顶元素 */
int top_stack(Stack s)
{
    if (stack_is_empty(s)) {
        printf("Error! Stack is empty!\n");
        return 0;
    }
    else {
        return s->array[s->top_of_stack];
    }
}

/* 检测栈是否为空栈 */
int stack_is_empty(Stack s)
{    
    // 栈顶的下标为 -1 表示栈为空
    return s->top_of_stack == EmptyTOS;
}


/* 检测栈是否为满栈 */
int stack_is_full(Stack s)
{
    // 栈顶的下标为 capacity - 1 表示栈满了(数组下标从 0 开始)
    return s->top_of_stack == --s->capacity;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值