栈的实现及基本操作

来源:互联网 发布:gradle java home 编辑:程序博客网 时间:2024/05/23 18:53

转载修改:

VC6.0 测试通过。

#include "stdafx.h"

#include 
<stdlib.h>
#include 
<stdio.h>
#include 
<malloc.h>
#include 
<conio.h>
#include 
<string.h>

#define ERROR 0
#define TRUE 1
#define FALSE 0
#define OK 1
#define EQUAL 1
#define OVERFLOW -1
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10

typedef 
int Status ;

struct STU{
    
char name[20];
    
char stuno[10];
    
int age;
    
int score;
}
;

struct STACK
{
    STU 
*base;
    STU 
*top;
    
int stacksize;
}
;

int InitStack(STACK  **S);
int DestroyStack(STACK *S);
int ClearStack(STACK *S);
int StackEmpty(STACK S);
int StackLength(STACK S);
int GetTop(STACK S,STU *e);
int Push(STACK *S,STU e);
int Pop(STACK *S,STU *e);
int StackTraverse(STACK S,Status (*visit)());

/*
 * 初始化栈
 
*/

int InitStack(STACK **S)
{
    (
*S)=(STACK *) malloc(sizeof(STACK));
    (
*S)->base=(STU *)malloc(STACK_INIT_SIZE *sizeof(STU));
    
if(!(*S)->base)
      exit(
-1);
    (
*S)->top=(*S)->base;
    (
*S)->stacksize=STACK_INIT_SIZE;
    
return OK;
}


int DestroyStack(STACK *S)
{
    free(S
->base);
    free(S);
    
return 1;
}


int ClearStack(STACK *S)
{
    S
->top=S->base;
    
return 1;
}


int StackEmpty(STACK S)
{
    
if(S.top==S.basereturn TRUE;
    
else
    
return FALSE;
}


int StackLength(STACK S)
{
    
int i;
    STU 
*p;
    i
=0;
    p
=S.top;
    
while(p!=S.base)
    
{
        p
--;
        i
++;
    }

    
return i;
}


int GetTop(STACK S,STU *e)
{
    
if(S.top==S.basereturn ERROR;
    
*e=*(S.top-1);
    
return OK;
}


int Push(STACK *S,STU e)
{
    
// 栈空间的增加
    if(S->top - S->base>=S->stacksize)
    
{

        S
->base=(STU *) realloc(S->base,
        (S
->stacksize + STACKINCREMENT) * sizeof(STU));
        
if(!S->base)exit(OVERFLOW);
        S
->top=S->base+S->stacksize;
        S
->stacksize += STACKINCREMENT;
    }



    
*(S->top++)=e;
    
return OK;
}


int Pop(STACK *S)
{

    
if(S->top==S->basereturn ERROR;
    
*--S->top;
    printf(
"%s  %s  %d  %d ",(*S).top->name, (*S).top->stuno,(*S).top->age,(*S).top->score);
    
// 未释放空间
    return OK;
}


int StackPrintElem(STU * e)
{
    printf(
"%s  %s  %d  %d ",e->name,e->stuno,e->age,e->score);
    
return 1;
}


int StackTraverse(STACK S)
{
    
while(S.top!=S.base)
    
{
        
--S.top;
        printf(
"%s  %s  %d  %d ",S.top->name, S.top->stuno,S.top->age,S.top->score);
    }

    
return 1;
}


main()
{
    STU e;
    STACK 
*Sa;

    printf(
" -------------------STACK Demo is running...---------------- ");
    printf(
"First is Push function. ");

    InitStack(
&Sa);
    printf(
"Now Stack is Empty. ");

    strcpy(e.name,
"stu1");
    strcpy(e.stuno,
"100001");
    e.age
=80;
    e.score
=1000;
    Push(Sa,e);
    printf(
"Now Stack has %d element. ",StackLength(*Sa));

    strcpy(e.name,
"stu2");
    strcpy(e.stuno,
"100002");
    e.age
=80;
    e.score
=1000;
    Push(Sa,e);
    printf(
"Now Stack has another element. ");
    printf(
"Now Stack has %d element. ",StackLength(*Sa));

    printf(
"Now Pop Stack,the top elem put into variable e. ");
    Pop(Sa);
    printf(
"Now Stack has %d element. ",StackLength(*Sa));

    strcpy(e.name,
"stu3");
    strcpy(e.stuno,
"100003");
    e.age
=30;
    e.score
=3000;
    Push(Sa,e);

    printf(
"Let's see the left of Stack's elem: ");
    StackTraverse(
*Sa);

    
return 0;
}


 

原创粉丝点击