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("栈为空\n");
		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\n",Pop(S));
	printf("%d\n",Pop(S));
	printf("%d\n",Pop(S));
	return 0;
}

  


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM