A simple C stack implementation

Source: Internet
Author: User

A simple stack is implemented in C language. The basic idea is to define a stack structure with two pointers and an int representing the stack size. Two pointers to the bottom of the stack and the top of the stack, when the stack bottom pointer and the stack top pointer coincident, the description stack is empty, when the stack top pointer minus the value of the bottom of the stack is equal to the stack size, the stack is full.

//mystack.h#ifndef Mystack_h#defineMystack_h#include<stdio.h>#include<stdlib.h>#defineMystack_increase_num 2typedefintElementType;//tentative as inttypedefintBool;typedefstruct{ElementType* BOTTOM;//Stack Bottom pointerElementType * TOP;//stack Top pointer    intSize//Stack size} mystack; Mystack* Initstack (intsize);//InitializeBool freestack (mystack * stack);//Freeing MemoryBool Push (Mystack *stack, elementtype data); Bool Pop (Mystack*stack, ElementType *outputdata); Bool IsEmpty (Mystack*stack);voidMakestackempty (Mystack *stack);voidPrintstack (Mystack *stack);#endif
//Mystack. C#include"mystack.h"Mystack* Initstack (intsize) {Mystack* stack = (mystack*)malloc(sizeof(Mystack));//Allocating Memory    if(stack==NULL)returnNULL; Stack->bottom = (elementtype*)malloc(sizeof(ElementType) *size); if(Stack->bottom = =NULL)returnNULL; Stack->top = stack->bottom;//Initialize to empty stack top pointer and bottom pointer point to same positionstack->size = size;//Initialize Size    returnStack;} Bool Freestack (Mystack*stack) {    if(stack!=NULL) {         Free(stack->bottom);  Free(stack); return 1; }    Else    {        return 0; }}voidMakestackempty (Mystack *stack) {    if(stack!=NULL) Stack->top = stack->Bottom;} Bool IsEmpty (Mystack*stack) {    if(Stack! =NULL) {        if(Stack->bottom = = stack->top)return 1; Else            return 0; }    Else        return 0;} Bool Push (Mystack*stack, ElementType data) {    if(Stack->top-stack->bottom >= stack->size)//Stack full {stack->bottom = (elementtype*)realloc(stack->Bottom,//allocate memory, increase stack sizesizeof(elementtype*) * (Stack->size +mystack_increase_num)); if(Stack->bottom = =NULL)return 0; Stack->top = Stack->bottom + stack->size; Initializing stack top pointer stack->size + =Mystack_increase_num; }    * (Stack->top) =data;//Pre-existing valuestack->top++;//pointer plus 1, pointing to next memory cell    return 1;} Bool Pop (Mystack*stack, ElementType *outputdata) {    if(IsEmpty (Stack)) {printf ("Stack is empty\n"); return 0; } Stack->top--;//pointer minus 1 points to the topmost element in the stack*outputdata = * (Stack->top);//Take value    return 1;}voidPrintstack (Mystack *stack) {    if(stack!=NULL) {        if(Stack->bottom = = stack->top) {printf ("Stack is empty\n"); }        Else        {            /*for (int i = 0; i < stack->top-stack->bottom; i++) printf ("%d\n", Stack->bottom[i]); */ElementType* element = stack->Bottom;  for(; Element! = Stack->top; element++) printf ("%d\n",*element); }    }}
//stacktest.c#include"mystack.h"#include<stdio.h>intMainintargcChar Const*argv[]) {    /*Code*/    intA,b,c; Mystack* Stack = Initstack (2); Push (Stack,1); Push (Stack,2);       Printstack (stack); //expected output 1, 2Push (Stack,3);        Printstack (stack); //expected output 1, 2, 3Pop (stack,&a); Pop (Stack,&b); Push (Stack,4); Pop (Stack,&c); printf ("a=%d b=%d c=%d\n", a,b,c);//expected output a=3 b=2 c=4makestackempty (stack); Pop (Stack,&AMP;A);//expected output stack is emptyprintf"a=%d\n", a);//Expected output A=3freestack (stack); return 0;}

Reference: Baidu Library

http://blog.csdn.net/mci2004/article/details/7532205

A simple C stack implementation

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.