码迷,mamicode.com
首页 > 编程语言 > 详细

栈(c语言)链表实现

时间:2020-07-30 22:10:51      阅读:73      评论:0      收藏:0      [点我收藏+]

标签:style   struct   链表实现   std   node   链表   strong   val   his   

节点的结构

struct Node
{
    int value;
    struct Node* next;
};
typedef struct Node Node;

基本功能和操作演示

#include <stdio.h>
#include <stdlib.h>
struct Node { int value; struct Node* next; }; typedef struct Node Node;
struct Stack { Node *node; int size; }; typedef struct Stack Stack; Node *createNode(int value) { Node *node = (Node *) malloc(sizeof(Node)); node->next = NULL; node->value = value; return node; } Stack *createStack() { Stack *stack = (Stack *) malloc(sizeof(Stack)); stack->node = NULL; stack->size = 0; return stack; } void push(Stack *stack, Node *node) { node->next = stack->node; stack->node = node; stack->size++; } int isEmpty(Stack *stack) { return stack->size <= 0 ? 1 : 0; } void pop(Stack *stack) { if (isEmpty(stack)) { printf("this stack is empty!"); return; } Node *node = stack->node; stack->node = stack->node->next; stack->size--; free(node); } Node *top(Stack *stack) { if (isEmpty(stack)) { printf("this stack is empty!"); return NULL; } return stack->node; } int size(Stack *stack) { return stack->size; } int main() { Stack *stack = createStack(); Node *node = createNode(10); push(stack, node); node = createNode(20); push(stack, node); printf("size = %d\n", size(stack)); while (!isEmpty(stack)) { node = top(stack); printf("%d ", node->value); pop(stack); } printf("\n"); printf("size = %d\n", size(stack)); return 0; }

 

栈(c语言)链表实现

标签:style   struct   链表实现   std   node   链表   strong   val   his   

原文地址:https://www.cnblogs.com/li1234567980/p/13406107.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!