C语言链栈完整实现

#include <stdio.h>
#include <stdlib.h>
#define ElementType int

//数据结构部分定义 
typedef struct SNode *Stack;
typedef struct SNode{
	ElementType Data;
	Stack Next;
};

Stack InitStack(){
	Stack S = (Stack)malloc(sizeof(struct SNode));
	S->Next=NULL;
	return S;
}
bool Push(Stack &S, ElementType item){
	Stack T = (Stack)malloc(sizeof(struct SNode));
	T->Next = S->Next;
	T->Data=item;
	S->Next=T;
	return true;
}
ElementType Pop(Stack &S){
	if(S->Next==NULL){
		printf("栈为空
");
		return NULL; 
	}
	Stack Tmp = S->Next;
	ElementType X = Tmp->Data;
	S->Next = Tmp->Next;
	free(Tmp);
	return X;
}

int main(){
	Stack S = InitStack();
	for(int i = 0; i<11;i++){
		Push(S,i);
	}	
	printf("%d
",Pop(S));
	printf("%d
",Pop(S));
	printf("%d
",Pop(S));
	return 0;
}

  

原文地址:https://www.cnblogs.com/zangkuo/p/6142401.html