체인 스 택 의 실현 (단일 포인터 노드 없 음) C 언어 버 전

2430 단어 cstructnull언어.
/*
	     、       
	  :S_hmily
	  :2011 8 31 
	    :VC++6.0
*/
/*********************************************************/
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
/*********************************************************/
#define OK      1         
#define ERROR   0         
#define TRUE    1         
#define FALSE   0       
typedef int     ElemType;    
typedef int     Status; 

typedef struct node {
	ElemType	data;
	struct node	*next;
}StackNode, *pStackNode;

typedef struct {
	pStackNode	top;
	int			count;
}LinkStack;
/*********************************************************/
//   
Status InitLinkStack(LinkStack *S)
{
	S->top = NULL;
	S->count = 0;
	return OK;
}
/*********************************************************/
//  
Status PushStack(LinkStack *S, ElemType e)
{
	pStackNode pNew = (pStackNode)malloc(sizeof(StackNode));
	pNew->data = e;
	pNew->next = S->top;
	S->top = pNew;
	++S->count;
	return OK;
}
/*********************************************************/
//  
Status PopStack(LinkStack *S, ElemType *e)
{
	pStackNode p = S->top;
	if (NULL == S->top)
		return ERROR;

	*e = p->data;
	S->top = S->top->next;
	free(p);
	--S->count;
	return OK;
}
/*********************************************************/
//      
void DispLinkStack(LinkStack *S)
{
	pStackNode p = S->top;

	while (p)
	{
		printf("%d ", p->data);
		p = p->next;
	}
}
/*********************************************************/
int main(void)
{
	LinkStack S;
	ElemType e;
	InitLinkStack(&S);
	PushStack(&S, 1);
	PushStack(&S, 2);
	PushStack(&S, 3);
	PushStack(&S, 4);
	PushStack(&S, 5);
	DispLinkStack(&S);
	PopStack(&S, &e);
	PopStack(&S, &e);
	PopStack(&S, &e);
	PopStack(&S, &e);
	PopStack(&S, &e);
	printf("

%d ", e); DispLinkStack(&S); return 0; }

좋은 웹페이지 즐겨찾기