C语言实现栈

1、顺序栈

注意:入栈时从高地址(N-1)到低地址(0),出栈时从低地址(0)到高地址(N-1)。实现方式可能与部分教材不相同,但基本思想相同。

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

typedef struct{
    char *studentID;
    char *studentName;
    char *sex;
    int age;
    char *class;
}DataType;

typedef struct{
    int size;               // 栈大小
    int top;                // 栈顶元素索引(下标)
    DataType *array;        // 存放栈内元素
}AStack;

AStack create(int size);                            // 创建栈
void clear(AStack *s);                              // 清空栈
void push(AStack *s, DataType val);                 // 入栈
DataType pop(AStack *s);                            // 出栈
DataType topValue(AStack s);                        // 栈顶元素
int length(AStack s);                               // 已有元素的长度


AStack create(int size){
    AStack *s = (AStack *)malloc(sizeof(AStack));
    s->size = size;
    s->top = size;  // 从高地址到低地址
    s->array = (DataType *)malloc(sizeof(DataType)*size);
    return *s;
}

void clear(AStack *s){
    s->top = s->size;
}

void push(AStack *s, DataType val){
    if(s->top == 0){
        printf("AStack is full");
        exit(0);
    }
    s->top--;
    s->array[s->top] = val;
}

DataType pop(AStack *s){
    if(s->top == s->size){
        printf("AStack is empty");
        exit(0);
    }
    DataType val = s->array[s->top];
    s->top++;
    return val;
}

DataType topValue(AStack s){
    return s.array[s.top];
}

int length(AStack s){
    return s.size - s.top;
}

测试代码

#include"AStack.h"

int main(){
    AStack s = create(3);
    DataType stud1 = {"20181745", "ycl", "male", 21, "5ban"};
    DataType stud2 = {"20181750", "ydx", "male", 23, "5ban"};
    DataType stud3 = {"20181736", "wdl", "male", 22, "5ban"};
    push(&s, stud1);
    push(&s, stud2);
    push(&s, stud3);
    while(s.top != s.size){
        DataType temp = pop(&s);
        printf("%s\t", temp.studentID);
        printf("%s\t", temp.studentName);
        printf("%s\t", temp.sex);
        printf("%d\t", temp.age);
        printf("%s\n", temp.class);
    }
    return 0;
}

2、链式栈

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

typedef struct{
    char *studentID;
    char *studentName;
    char *sex;
    int age;
    char *class;
}DataType;

typedef struct node{
    DataType value;
    struct node *next;
}node;

typedef struct{
    int count;              // 栈内元素数量
    node *top;              // 栈顶元素
}LStack;

LStack create();                                    // 创建栈
void clear(LStack *s);                              // 清空栈
void push(LStack *s, DataType val);                 // 入栈
DataType pop(LStack *s);                            // 出栈
DataType topValue(LStack s);                        // 栈顶元素
int length(LStack s);                               // 已有元素的长度


LStack create(){
    LStack *s = (LStack *)malloc(sizeof(LStack));
    s->top = NULL;
    s->count = 0;
    return *s;
}

void clear(LStack *s){
    while(s->top != NULL){
        node *temp = s->top;
        s->top = s->top->next;
        free(temp);
    }
    s->count = 0;
}

void push(LStack *s, DataType val){
    node *n = (node *)malloc(sizeof(node));
    n->value = val;
    n->next = s->top;
    s->top = n;
    s->count++;
}

DataType pop(LStack *s){
    if(s->top == NULL){
        printf("LStack is empty");
        exit(0);
    }
    DataType val = s->top->value;
    node *temp = s->top;
    s->top = s->top->next;
    free(temp);
    s->count--;
    return val;
}

DataType topValue(LStack s){
    return s.top->value;
}

int length(LStack s){
    return s.count;
}

测试代码

#include"LStack.h"

int main(){
    LStack s = create(3);
    DataType stud1 = {"20181745", "ycl", "male", 21, "5ban"};
    DataType stud2 = {"20181750", "ydx", "male", 23, "5ban"};
    DataType stud3 = {"20181736", "wdl", "male", 22, "5ban"};
    push(&s, stud1);
    push(&s, stud2);
    push(&s, stud3);
    while(s.top != NULL){
        DataType temp = pop(&s);
        printf("%s\t", temp.studentID);
        printf("%s\t", temp.studentName);
        printf("%s\t", temp.sex);
        printf("%d\t", temp.age);
        printf("%s\n", temp.class);
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值