Data structure _ Stack

Source: Internet
Author: User

#ifndef __stack_base_array_h__#define__stack_base_array_h__typedefstruct_stack{int*arr;//Storage Stack Data    intcap;//Stack Capacity    intTop//Top of Stack}stack;/*Creating Stacks*/STACK*stack_create (intcap);/*Destroying Stacks*/voidStack_destroy (STACK *stack);/*Press Stack*/voidStack_push (STACK *stack,intdata);/*Pop Stacks*/intStack_pop (STACK *stack);/*empty sentence*/intStack_empty (STACK *stack);/*Full sentence*/intStack_full (STACK *stack);#endif
#include"Stack_base_array.h"#include<stdio.h>#include<stdlib.h>/*Creating Stacks*/STACK*stack_create (intcap) {STACK*stack =malloc(sizeof(STACK)); Stack->arr =malloc(Cap *sizeof(int)); Stack->cap =cap; Stack->top =0; returnStack;}/*Destroying Stacks*/voidStack_destroy (STACK *stack) {     Free(stack->arr); Stack->arr = NULL;//Prevent Wild hands     Free(stack); Stack= NULL;//Prevent Wild hands}/*Press Stack*/voidStack_push (STACK *stack,intdata) {Stack->arr[stack->top] =data; Stack->top++;}/*Pop Stacks*/intStack_pop (STACK *stack) {    returnstack->arr[--(stack->top)];}/*NULL if NULL returns 1, otherwise returns 0*/intStack_empty (STACK *stack) {    return!stack->top;}/*full, if full return 1, otherwise return 0*/intStack_full (STACK *stack) {    if(Stack->top = = stack->cap) {            return 1; }    return 0;}
#include <stdio.h>#include"Stack_base_array.h"intMain () {STACK*stack = Stack_create (Ten); inti =1; /*Stack Dissatisfaction*/     while(!stack_full (Stack)) {        /*Press Stack*/Stack_push (Stack, I++); }    /*stack non-empty*/     while(!stack_empty (Stack)) {printf ("%d\n", Stack_pop (stack));    } stack_destroy (stack); return 0;}
#ifndef __stack_base_list_h__#define__stack_base_list_h__/*represents a node in a stack*/typedefstructstacknode{intData//Valid data    structStacknode *next;//refers to a node down}stack_node;/*represents the stack structure*/typedefstructstack{Stack_node*top;} STACK;/*Create a node*/Stack_node*node_create (intData,stack_node *next);/*Destroying Nodes*/Stack_node*node_destroy (Stack_node *node);/*Creating Stacks*/STACK*stack_create (void);/*Destroying Stacks*/voidStack_destroy (STACK *stack);/*Press Stack*/voidStack_push (STACK *stack,intdata);/*Pop Stacks*/intStack_pop (STACK *stack);/*empty sentence*/intStack_empty (STACK *stack);/*Full sentence*/intStack_full (STACK *stack);#endif
#include"stack_base_list.h"#include<stdio.h>#include<stdlib.h>/*Create a node*/Stack_node*node_create (intData,stack_node *next) {Stack_node*node =malloc(sizeof(Stack_node)); Node->data =data; Node->next =Next; returnnode;}/*Destroying Nodes*/Stack_node* Node_destroy (Stack_node *node) {Stack_node*next = node->Next;  Free(node); returnnext;}/*Creating Stacks*/STACK*stack_create (void) {STACK*stack =malloc(sizeof(STACK)); Stack->top =NULL; returnStack;}voidStack_clear (STACK *stack) {     while(stack->top) {Stack->top = Node_destroy (stack->top); }}/*Destroying Stacks*/voidStack_destroy (STACK *stack) {    /*Empty Stack*/stack_clear (stack);  Free(stack);// ?????}/*Press Stack*/voidStack_push (STACK *stack,intdata) {    /** 1 Apply the node and initialize it so that the next point of the node points to the old stack top * 2 top pointer pointing to the new stack top*/Stack->top = node_create (data, stack->top);}/*Pop Stacks*/intStack_pop (STACK *stack) {    intdata = stack->top->data; Stack->top = Node_destroy (stack->top); returndata;}/*NULL if NULL returns 1, otherwise returns 0*/intStack_empty (STACK *stack) {    return!stack->top;}/*Full sentence*/intStack_full (STACK *stack) {    return 0;}

Data structure _ Stack

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.