关于进栈与出栈(作为备忘)

#ifndef _1_H

#define _1_H


#ifdef __cplusplus

extern "C" {

#endif



#define TRUE 1

#define FALSE 0


//≤ª¥¯ø’Õ∑


typedef struct stack

{

int score;

struct stack *next;

}STACK;


STACK *initStack(STACK *t);


int EMPTY(STACK *t);


STACK *PUSH(STACK *t,int x);


STACK *POP(STACK *t,int *x);


#ifdef __cplusplus

}

#endif




#include <stdio.h>

#include <stdlib.h>

#include "1.h"


STACK *initStack(STACK *t)

{

t =NULL;

return t;

}


int EMPTY(STACK *t)

{

return ((NULL == t) ?TRUE : FALSE);

}


STACK *PUSH(STACK *t,int x)

{

STACK *p;

p = (STACK *)malloc(sizeof(STACK));

if(NULL == p)

{

printf("ram error\n");

returnNULL;

}

p->score = x;

p->next = t;

return p;

}


STACK *POP(STACK *t,int *x)

{

STACK *p;

if(NULL == t)

{

printf("overflow\n");

returnNULL;

}

else

{

*x = t->score;

p = t;

t = t->next;

free(p);

return t;

}

}


int main()

{

int a[]={50,80,70,90},b[4]={0};

int i,score,flag;

STACK *top;

top = initStack(top);

flag = EMPTY(top);

printf("-----------------------------\n");

printf("this stack's empty flag is %d\n",flag);

printf("-----------------------------\n");


printf("PUSH:\n");

for(i =0;i < 4;i++)

{

printf("%d\n",a[i]);

top = PUSH(top, a[i]);

}

flag = EMPTY(top);

printf("-----------------------------\n");

printf("this stack's empty flag is %d\n",flag);

printf("-----------------------------\n");

printf("POP:\n");

for(i =0;i < 4;i++)

{

top = POP(top, &b[i]);

printf("%d\n",b[i]);

}

flag = EMPTY(top);

printf("-----------------------------\n");

printf("this stack's empty flag is %d\n",flag);

printf("-----------------------------\n");

top =NULL;

return0;

}



你可能感兴趣的:(C语言,出栈,进栈)