栈
#include <stdio.h> #include <stdlib.h> #include <string.h> #define ElementType int 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(ElementType X, Stack S); ElementType Top( Stack S); void Pop( Stack S); struct Node { ElementType Element; PtrToNode next; }; int IsEmpty(Stack S) { return S->next == NULL; } Stack CreateStack(void) { Stack S; S = malloc(sizeof(struct Node)); if(S == NULL) printf("ERROR"); S->next = NULL; MakeEmpty(S); return S; } void MakeEmpty(Stack S) { if( S == NULL) printf("ERROR Empty"); else while( !IsEmpty(S)) Pop(S); } void Push (ElementType X, Stack S) { PtrToNode TmpCell; TmpCell = malloc(sizeof(struct Node)); if(TmpCell == NULL) printf("ERROR "); else { TmpCell->Element = X; TmpCell->next = S->next; S->next = TmpCell; } } ElementType Top(Stack S) { if( !IsEmpty(S)) return S->next->Element; return 0; } void Pop(Stack S) { PtrToNode FirstCell; if( IsEmpty(S)) printf("Stack Empty"); else { FirstCell = S->next; S->next = S->next->next; free(FirstCell); } } int main() { Stack S; int n,num,m; int i; S = CreateStack(); printf( "Initialization complete.\n" ); printf( "Please input the number of elements in the stack:\n" ); scanf( "%d", &n ); printf( "Please input %d elements push into stack:\n", n ); for( i =0; i< n;i++) { scanf("%d",&num); Push(num,S); } printf( "Please input the numbers you want pop out from the stack(no more than:%d)\n", n ); scanf("%d",&n); printf( "Pop out from the stack %d elements in turns:\n", n ); for( i = 0; i < n; i++ ) { m = Top( S ); Pop( S ); printf( "%3d",m ); } printf( "\n" ); return 0; }
include <stdio.h> #include <stdlib.h> #include <string.h> struct StackRecord; typedef struct StackRecord *Stack; #define ElementType int int IsEmpty( Stack S); int IsFull(Stack S); Stack CreateStack( int MaxElement); void DisposeStack( Stack S); void MakeEmpty( Stack S); void Push(ElementType X, Stack S); ElementType Top( Stack S); void Pop(Stack S); ElementType TopAndPop( Stack S); #define EmptyTOS -1 #define MinStackSize 5 struct StackRecord { int Capactity; int TopOfStack; ElementType *Array; }; Stack CreateStack ( int MaxElement) { Stack S; if( MaxElement < MinStackSize) printf("ERROR"); S = malloc(sizeof(struct StackRecord)); if( S == NULL) printf("ERROR"); S->Array = malloc(sizeof(ElementType) * MaxElement); if( S->Array == NULL) printf("OUT OF SPACE"); S->Capactity = MaxElement; MakeEmpty(S); return S; } void DisposeStack( Stack S) { if( S != NULL) { free( S->Array); free(S); } } int IsEmpty( Stack S) { return S->TopOfStack == EmptyTOS; } void MakeEmpty(Stack S) { S->TopOfStack = EmptyTOS; } int IsFull(Stack S) { return S->TopOfStack = S->Capactity -1; } void Push(ElementType X, Stack S) { if(IsFull(S)) printf("Stack FULL"); else S->Array[++ S->TopOfStack] = X; } ElementType Top(Stack S) { if(!IsEmpty(S)) return S->Array[S->TopOfStack]; printf("Empty Stack"); return 0; } void Pop( Stack S) { if(IsEmpty(S)) printf("Empty Stack"); else S->TopOfStack --; } ElementType TopAndPop( Stack S) { if(!IsEmpty(S)) return S->Array[S->TopOfStack --]; printf("Empty Stack"); return 0; } int main() { Stack S; int n,num,m; int i; S = CreateStack(10); printf( "Initialization complete.\n" ); printf( "Please input the number of elements in the stack:\n" ); scanf( "%d", &n ); printf( "Please input %d elements push into stack:\n", n ); for( i = 0; i < n; i++ ) { scanf( "%d", &num ); Push( num, S ); } printf( "Top of the stack:%d\n", Top( S ) ); printf( "Please input the numbers you want pop out from the stack(no more than %d):\n", n ); scanf( "%d", &n ); printf( "Pop out from the stack %d elements in turns:\n", n ); for( i = 0; i < n; i++ ) { m = TopAndPop( S ); printf( "%3d", m ); } printf( "\n" ); return 0; }