1. 程式人生 > >用陣列實現棧

用陣列實現棧

在ubuntu上經過gcc驗證:

一、標頭檔案:[email protected]:~/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);
}


三、列印輸出

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