数据结构_堆栈的链式实现

typedef struct SNode *Stack;
struct SNode{
	ElementType Data;
	struct SNode *Next;
}; 

Stack CreateCrack(){
	Stack S;
	S = (Stack)malloc(sizeof(struct SNode));
	S->Next = NULL;
	return S;
}

int IsEmpty(Stack S){
	return (S->Next == NULL);
}

/*将元素item插入堆栈S*/
void Push(ElementType item, Stack S){
	Struct SNode *TmpCell;
	TmpCell = (struct SNode *)malloc(sizeof(struct SNode));
	TmpCell->Data = item;
	TmpCell->Next = S->Next;
	S->Next = TmpCell;
}

/*删除并返回S的栈顶元素*/
ElementType Pop(Stack S){
	struct SNode *FirstCell;
	ElementType TopElem;
	if(IsEmpty(S)){
		printf("堆栈空");
		return NULL; 
	}else{
		FirstCell = S->Next;
		S->Next = FirstCell->Next;
		TopElem = FirstCell->Data;
		free(FirstCell);
		return TopElem;
	}
} 

猜你喜欢

转载自blog.csdn.net/birdunderastarrysky/article/details/81127381