데이터 구조 - C 언어 판 - 순서 스 택 모든 기본 작업

C   -           
//              
#include 
#include 

#define MAXSIZE 5

typedef struct Stack{
	int data[MAXSIZE];
	int top;
}Stack;


Stack *Init_SeqStack()
{
    Stack *s;
    s=malloc(sizeof(Stack));
    s->top = -1;
    return s;
}

void Push_Stack(Stack * S)
{
	int i;

	for(i=0; itop== (MAXSIZE-1) )
		{
			printf("  ,      !
"); break; } S->top++; scanf("%d", &S->data[S->top] ); } } void Pop_Stack(Stack * S) { while(S->top != -1) { printf("%d ",S->data[S->top]); S->top--; } } int main(void) { Stack *S; S = Init_SeqStack(); Push_Stack(S); Pop_Stack(S); printf("
"); system("pause"); return 0; }
//                 
#include 
#define MAXSIZE 100
typedef int SElemType;
typedef struct
{
    SElemType *base;
	SElemType *top;
    int stacksize;
}SqStack;
SqStack S;
int  InitStack(SqStack *S)
{
    S->base= malloc(sizeof(SElemType[MAXSIZE]));
    if(!S->base) return 0;
    S->top = S->base;
    S->stacksize=MAXSIZE;
	return 1;
}

int Push(SqStack *S,SElemType e)
{
    if((S->top)-(S->base) == S->stacksize) 
		return 0;
    else
    {
        *S->top++=e;
       
        return 1;
    }
}
int Pop(SqStack *S,SElemType *e)
{
    if(S->top==S->base) return 0;
    else
    {
        *e = *--S->top;
       
        return 1;
    }
}

main()
{
    int a;
	InitStack(&S);
    Push(&S,1);
	Push(&S,2);
	Push(&S,3);
	Push(&S,4);
	Push(&S,5);
	Pop(&S,&a);
	printf("%d
",a); Pop(&S,&a); printf("%d
",a); Pop(&S,&a); printf("%d
",a); Pop(&S,&a); printf("%d
",a); Pop(&S,&a); printf("%d
",a); }

좋은 웹페이지 즐겨찾기