程序博客网 > 网络直播可以回看吗
来源:互联网 发布:网络直播可以回看吗 编辑:程序博客网 时间:2024/04/28 17:09
/* Name: stack.c Copyright: personal Author: hojor Date: 07-06-10 10:22 Description: stack*/#include <stdio.h>#include <stdlib.h>#include "stack.h"#define EmptyTOS (-1)#define MinStackSize (5)#define ElementType intstruct StackRecord{ int Capacity; int TopOfStack; ElementType * Array;};typedef struct StackRecord * Stack;//function.listint IsEmpty(Stack S);int IsFull(Stack S);void DisposeStack(Stack S);void MakeEmpty(Stack S);void Push(ElementType X,Stack S);void Pop(Stack S);ElementType Top(Stack S);ElementType TopAndPop(Stack S);Stack CreateStack(int MaxElements);//create stack size of MaxElementsStack CreateStack( int MaxElements){ Stack S; if(MaxElements < MinStackSize) printf("Stack size is too small/n"); S = (Stack)malloc(sizeof(struct StackRecord)); if(S == NULL) printf("Out of space!!/n"); S->Array = (ElementType *)malloc(sizeof(ElementType)*MaxElements); if(S->Array == NULL) printf("Out of space!!/n"); S->Capacity = MaxElements; MakeEmpty(S); return S; }//release Stack routinesvoid DisposeStack(Stack S){ if(S != NULL) { free(S->Array); free(S); }}//Determine whether the stack is emptyint IsEmpty(Stack S){ return S->TopOfStack == EmptyTOS; }//Determine whether the stack is fullint IsFull(Stack S){ return S->TopOfStack == S->Capacity;}//create an empty stack routinesvoid MakeEmpty(Stack S){ S->TopOfStack = EmptyTOS;}//push stackvoid Push(ElementType x,Stack S){ if(IsFull(S)) printf("Full stack/n"); else S->Array[++S->TopOfStack]=x;}//return top of stackElementType Top(Stack S){ if(!IsEmpty(S)) return S->Array[S->TopOfStack]; printf("Empty stack"); return 0;} //pop stackvoid Pop(Stack S){ if(IsEmpty(S)) printf("/nEmpty stack/n"); else S->TopOfStack--;}//gave the top of stack and popElementType TopAndPop(Stack S){ if(!IsEmpty(S)) return S->Array[S->TopOfStack--]; printf("/nEmpty stack/n"); return 0;} int main(void){ int i; Stack s = CreateStack(10); for(i=0;i<10;i++) Push(i,s); for(i=0;i<10;i++) printf("%d ",TopAndPop(s)); DisposeStack(s); system("pause"); return 0;}