#ifndef _STACK_H #define _STACK_H struct Node; typedef struct Node *PtrToNode; typedef PtrToNode Stack; int IsEmpty( Stack S ); Stack CreateStack( void ); void DisposeStack( Stack S ); void MakeEmpty( Stack S ); void Push( int X, Stack S ); int Top( Stack S ); void Pop( Stack S ); #endif #include <stdio.h> #include <stdlib.h> #include <malloc.h> #include <assert.h> #include "stack.h" struct Node { int Element; PtrToNode Next; }; int IsEmpty( Stack S ) { return S->Next == NULL; } Stack CreateStack( void ) { Stack s; s = malloc( sizeof(struct Node)); assert( s!= NULL); s->Next = NULL; return s; } void Push( int X, Stack S ) { PtrToNode element; assert( S!= NULL ); element = malloc( sizeof(struct Node) ); assert( element != NULL); element->Element = X; element->Next = S->Next; S->Next = element; } int Top( Stack S ) { assert( S!= NULL ); if ( S->Next != NULL ) return S->Next->Element; return -1; } void Pop( Stack S ) { PtrToNode tmp; assert( S != NULL ); if ( S->Next != NULL) { tmp = S->Next; S->Next = S->Next->Next; free( tmp ); } } void MakeEmpty( Stack S ) { assert( S != NULL ); while( !IsEmpty(S) ) Pop(S); } int main() { int a; Stack s; s = CreateStack(); assert( s!= NULL ); Push(2, s); a = Top(s); printf("---%d---/n", a); Push(3, s); a = Top(s); printf("---%d---/n", a); Pop(s); a = Top(s); printf("---%d---/n", a); MakeEmpty(s); Push(3, s); a = Top(s); printf("---%d---/n", a); return 0; }