鏈棧的基本操作實現

注:截圖來自青島大學王卓老師的《數據結構》視頻教學,視頻地址爲: https://space.bilibili.com/40323036?spm_id_from=333.788.b_765f7570696e666f.2 

以下是具體代碼實現:

//鏈棧相關操作 
#include <stdio.h>
#include <stdlib.h>
#include <iostream>

using namespace std;

typedef int SElemType;
typedef int Status;

#define OK 1
#define ERROR 0
#define TRUE 1
#define FLASE 0

//鏈棧表示(重點掌握)
typedef struct StackNode{
	SElemType data;
	struct StackNode *next;
}StackNode,*LinkStack;

//鏈棧初始化
void InitStack(LinkStack &S){
	S=NULL;
}

//判斷鏈棧是否爲空 
Status EmptyStack(LinkStack S){
	if(S==NULL) return TRUE;
	else return FLASE;
}

//入棧
Status Push(LinkStack &S,SElemType e){
	StackNode *p=new StackNode;
	if(!p) return ERROR;
	p.data=e;
	p->next=S;
	S=p;
	return OK;
} 

//出棧
Status Pop(LinkStack &S,SElemType &e){
	if(S==NULL) return ERROR;
	e=S.data;
	StackNode *temp=S;
	S=S.next;
	delete temp;
	return OK;
} 

//取棧頂元素
SElemType GetTop(LinkStack S){
	if(S!=NULL) return S.data;
}

int main(){
    /*測試代碼省略*/
} 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章