C Language implementation of stacks (linked lists and arrays)

Source: Internet
Author: User

implementation of array stacks

Stack_array.h

#ifndef _stack_array_h#define Emptytos ( -1)#define MINSTACKSIZE (5)#define TRUE 1#define FALSE 0typedefintElementtype;typedefstructstackarraynode{intcapacity;intTopofstack; ElementType *array;} Stackarraynode,*stack;intIsEmpty (Stack S);intIsfull (Stack S); Stack Creatstack (intmaxStackSize);voidFreestack (Stack S);voidPush (ElementType x, Stack S);voidPop (Stack S); ElementType Top (Stack S); ElementType Topandpop (Stack S);#endif

Stack_array.c

#include <stdio.h>#include <stdlib.h>#include "stack_array.h"intIsEmpty (Stack S) {if(S->topofstack = = Emptytos)returnTRUE;Else         returnFALSE;}intIsfull (Stack S) {if(S->topofstack = = s->capacity)returnTRUE;Else         returnFALSE;} Stack Creatstack (intmaxStackSize) {Stack S;if(maxStackSize < Minstacksize)printf("Stack is too small\n"); S = (Stack)malloc(sizeof(structStackarraynode));if(S = = NULL)Exit(FALSE); S->array = (ElementType *)malloc(sizeof(ElementType) *maxstacksize);if(S->array = = NULL)Exit(FALSE);    s->capacity = maxStackSize; S->topofstack = Emptytos;}voidFreestack (Stack S) {if(S! = NULL) { Free(S->array); Free(S); }}voidPush (ElementType x, Stack S) {if(Isfull (S))printf("Stack is full\n");Else{s->topofstack + +;    S->array[s->topofstack] = x; }}voidPop (Stack S) {if(IsEmpty (S))printf("Stack is empty\n");Else{S->topofstack--; }}elementtype Top (Stack S) {if(! IsEmpty (S))returns->array[s->topofstack];printf("Stack is empty\n");returnFALSE;} ElementType Topandpop (Stack S) {if(! IsEmpty (S))returns->array[s->topofstack--];printf("Stack is empty\n");returnFALSE;}intMain () {Stack S; S = Creatstack (Ten); Push (1, S); Push (2, S); Push (4, S);printf("%d\n", Top (S)); Pop (S);printf("%d\n", Top (S)); Pop (S);printf("%d\n", Top (S));}
the implementation of the chain list stack

Stack.h

#ifndef_stack_h#define TRUE 1#define FALSE 0typedef int ELEMENTTYPE;TYPEDEF struct stacknode{ElementTypeData; struct Stacknode*Next;} Stacknode,*Stack; int IsEmpty (StackS);StackCreatestack (void);voidMakeempty (StackS);voidPush (ElementType x,StackS);voidPop (StackS); ElementType Top (StackS);#endif   /*_stack_h * *

Stack.c

#include <Stdlib.H>#include <Stdio.H>#include "Stack.h"int IsEmpty (StackS) {if(S -Next== NULL)return TRUE;Else         return FALSE;}voidMakeempty (StackS) {if(S== NULL) printf ("Fata Error");Else{ while(!IsEmpty (s)) Pop (s); }}StackCreatestack (void){StackS=(Stack) malloc (sizeof (struct stacknode));if(S== NULL) printf ("Fata Error"); S -Next= NULL; Makeempty (S);returnS;}voidDeletestack (Stacks) {makeempty (s); Free (S);}voidPush (ElementType x,StackS) {StackTmpstack=(Stack) malloc (sizeof (struct stacknode));if(Tmpstack== NULL) printf ("Fata Error");Else{Tmpstack -Data =X Tmpstack -Next=S -Next S -Next=Tmpstack; }}voidPop (StackS) {StackTmpstack;if(IsEmpty (S)) printf ("Stack is Empty");Else{Tmpstack=S -Next S -Next=S -Next -Next    Free (tmpstack); }}elementtype Top (StackS) {if(IsEmpty (S))return FALSE;Else        returnS -Next -Data;} int main () {StackS=Createstack (); Push (1, S); Push (2, S); Push (4, S); printf"%d\n", Top (S));    Pop (S); printf"%d\n", Top (S));    Pop (S); printf"%d\n", Top (S));}

Reference to data structure and algorithm analysis

C Language implementation of stacks (linked lists and arrays)

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.