纯C的Stack实现

      写教程,想要说明白面向过程和面向对象的区别,想用Stack作为最简单的例子。网上搜索C stack头几个写得乱七八糟。干脆自己动手写个。

=======================================stack.c============================================
#include <stdio.h>
#include <stdlib.h>

typedef struct
{
      int* elements;
      int top;
      int capacity;
} Stack;

void init(Stack* st, int capacity)
{
      st->top = 0;
      st->capacity = capacity;
      st->elements = (int*)malloc(sizeof(int) * capacity);
}
void destory(Stack* st)
{
      if (st->elements != NULL)
      {
            free(st->elements);
            st->elements = NULL;
      }
}
int isFull(Stack* st)
{
      return (st->top >= st->capacity);
}
void push(Stack* st, int val)
{
      st->elements[st->top] = val;
      (st->top)++;      
}
int isEmpty(Stack* st)
{
      return (st->top == 0);
}
int pop(Stack* st)
{
      (st->top)--;
      return (st->elements[st->top]);   
}
int size(Stack* st)
{
      return (st->top);
}
int capacity(Stack* st)
{
      return (st->capacity);
}
void print(Stack* st)
{
      int i = 0;
      if (st->top == 0)
      {
            printf("stack is empty.\n");
      }
      else
      {
            printf("stack contents: ");
            for (i = 0; i < st->top; i++)
            {
                  printf("%d, ",st->elements[i]);
            }
            printf("\n");
      }
}

int main()
{
      Stack st;
      int i = 0;     
      init(&st, 10);
      isEmpty(&st) ? printf("stack is empty\n") : printf("stack is not empty\n");
      isFull(&st) ? printf("stack is full\n") : printf("stack is not full\n");
      for (i = 0; i < 10; i++)
      {
            push(&st, i);
            printf("push %d\n", i);
      }
      isEmpty(&st) ? printf("stack is empty\n") : printf("stack is not empty\n");
      isFull(&st) ? printf("stack is full\n") : printf("stack is not full\n");
      for (i = 0; i < 5; i++)
      {
            printf("pop %d\n", pop(&st));
      }
      isEmpty(&st) ? printf("stack is empty\n") : printf("stack is not empty\n");
      isFull(&st) ? printf("stack is full\n") : printf("stack is not full\n");
      printf("capacity of stack is %d, size of statck is %d\n", capacity(&st), size(&st));
      print(&st);
      destory(&st);
      return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
链栈是一种基于链表实现的栈结构,其基本操作包括入栈、出栈、判断栈是否为空和获取栈顶元素等。 首先,我们需要定义链栈的节点结构体: ```c typedef struct node { int data; // 数据域 struct node *next; // 指向下一节点的指针 } Node; ``` 然后定义链栈结构体: ```c typedef struct { Node *top; // 栈顶指针 int size; // 栈内元素个数 } LinkedStack; ``` 接下来,我们可以实现链栈的基本操作: 1. 初始化链栈 ```c void initStack(LinkedStack *stack) { stack->top = NULL; stack->size = 0; } ``` 2. 判断链栈是否为空 ```c int isEmpty(LinkedStack *stack) { return stack->top == NULL; } ``` 3. 入栈操作 ```c void push(LinkedStack *stack, int value) { Node *newNode = (Node *)malloc(sizeof(Node)); newNode->data = value; newNode->next = stack->top; stack->top = newNode; stack->size++; } ``` 4. 出栈操作 ```c int pop(LinkedStack *stack) { if (isEmpty(stack)) { printf("Stack is empty.\n"); return -1; } int value = stack->top->data; Node *temp = stack->top; stack->top = stack->top->next; stack->size--; free(temp); return value; } ``` 5. 获取栈顶元素 ```c int getTop(LinkedStack *stack) { if (isEmpty(stack)) { printf("Stack is empty.\n"); return -1; } return stack->top->data; } ``` 完整的代码如下所示: ```c #include <stdio.h> #include <stdlib.h> typedef struct node { int data; // 数据域 struct node *next; // 指向下一节点的指针 } Node; typedef struct { Node *top; // 栈顶指针 int size; // 栈内元素个数 } LinkedStack; void initStack(LinkedStack *stack) { stack->top = NULL; stack->size = 0; } int isEmpty(LinkedStack *stack) { return stack->top == NULL; } void push(LinkedStack *stack, int value) { Node *newNode = (Node *)malloc(sizeof(Node)); newNode->data = value; newNode->next = stack->top; stack->top = newNode; stack->size++; } int pop(LinkedStack *stack) { if (isEmpty(stack)) { printf("Stack is empty.\n"); return -1; } int value = stack->top->data; Node *temp = stack->top; stack->top = stack->top->next; stack->size--; free(temp); return value; } int getTop(LinkedStack *stack) { if (isEmpty(stack)) { printf("Stack is empty.\n"); return -1; } return stack->top->data; } int main() { LinkedStack stack; initStack(&stack); // 入栈 push(&stack, 1); push(&stack, 2); push(&stack, 3); // 获取栈顶元素并输出 printf("Top element: %d\n", getTop(&stack)); // 出栈 printf("Pop: %d\n", pop(&stack)); printf("Pop: %d\n", pop(&stack)); printf("Pop: %d\n", pop(&stack)); // 判断栈是否为空 printf("Is empty: %d\n", isEmpty(&stack)); return 0; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值