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