用数组实现栈

来源:互联网 发布:代码数据图 编辑:程序博客网 时间:2024/05/22 20:07

在ubuntu上经过gcc验证:



一、头文件:hangma@ubuntu:~/test/test/protest/stack_test$ cat my_stack.h

#ifndef _MY_STACK_#define _MY_STACK_struct stackrecord;typedef struct stackrecord* stack;typedef int ElementType;int IsEmpty(stack s);int IsFull(stack s);stack CreateStack(int MaxElements);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 EMPTY 0#define MinStackSize (5)struct stackrecord {    int capacity;    int size;    ElementType *array;};#endif



二、C文件:


#include <stdio.h>#include <stdlib.h>#include "my_stack.h"int IsEmpty(stack s){    return (s->size == EMPTY);}int IsFull(stack s){    return s->size == s->capacity;}stack CreateStack(int MaxElements){    if(MaxElements < MinStackSize)    {printf("the stack size is too small\n");exit(-1);    }    stack s;    s = (stack)malloc(sizeof(struct stackrecord));    if(s == NULL)    {printf("can't allocate the stack \n");exit(-2);    }    s->array = (ElementType *)malloc(MaxElements * sizeof(ElementType));    if(s->array == NULL)    {printf("allocate memory error\n");exit(-3);    }        s->capacity = MaxElements;    MakeEmpty(s);     return s;}void DisposeStack(stack s){    if(s != NULL)    {if(s->array != NULL)     free(s->array);free(s);    }}void MakeEmpty(stack s){    s->size = EMPTY;}void Push(ElementType x,stack s){    if(s == NULL)    {printf("the stack is not exsit\n");exit(-4);    }    if(IsFull(s))    {printf("the stack is full\n");exit(-5);    }    s->array[s->size] = x;    s->size++;}ElementType Top(stack s){    if(s == NULL)    {printf("the stack is not exsit\n");exit(-7);    }    if(IsEmpty(s))    {printf("the stack is empty\n");exit(-8);    }        return s->array[s->size-1];}void Pop(stack s){    if(s == NULL)    {printf("s is not exsit\n");exit(-6);    }    if(IsEmpty(s))    {printf("s is empty\n");exit(-7);    }    s->size--;}ElementType TopAndPop(stack s){        if(s == NULL)    {printf("s is not exsit\n");exit(-6);    }    if(IsEmpty(s))    {printf("s is empty\n");exit(-7);    }    s->size--;    return s->array[s->size];}int main(int argc,char *argv[]){    stack s;        s = CreateStack(10);    int i = 0;    while(++i <= 10)    {Push(i,s);printf("push the data:%d into the stack,the stack size is %d\n",i,s->size);    }    int x;    while(s->size)    {x = TopAndPop(s);printf("the top of stack is %d\n",x);   sleep(1);    }    DisposeStack(s);}



三、打印输出

hangma@ubuntu:~/test/test/protest/stack_test$ ./my_stack push the data:1 into the stack,the stack size is 1push the data:2 into the stack,the stack size is 2push the data:3 into the stack,the stack size is 3push the data:4 into the stack,the stack size is 4push the data:5 into the stack,the stack size is 5push the data:6 into the stack,the stack size is 6push the data:7 into the stack,the stack size is 7push the data:8 into the stack,the stack size is 8push the data:9 into the stack,the stack size is 9push the data:10 into the stack,the stack size is 10the top of stack is 10the top of stack is 9the top of stack is 8the top of stack is 7the top of stack is 6the top of stack is 5the top of stack is 4the top of stack is 3the top of stack is 2the top of stack is 1


原创粉丝点击