标签: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; }
标签:style struct 链表实现 std node 链表 strong val his
原文地址:https://www.cnblogs.com/li1234567980/p/13406107.html