1. 程式人生 > 其它 >C語言順序棧(僅做記錄,無講解)

C語言順序棧(僅做記錄,無講解)

技術標籤:C語言筆記資料結構

#include <stdio.h>
#include <stdlib.h>

#define MAXSIZE  10

typedef struct {
	int  data[MAXSIZE];
	int top;
}SqStack;

// 初始化棧

int InitStack(SqStack* node) {
	node->top = -1;
	return 1;
}

// 判斷棧是否為空

int jugdeStack(SqStack* node) {
	if (node->top == -1)
		return 1;
//棧空 else return 0;//棧未空 } // 入棧 int inStack(SqStack *node,int data) { if (node->top==MAXSIZE-1) return -1;//棧滿,報錯 node->top++; node->data[node->top] = data; return 1; } //出棧 int outStack(SqStack* node,int popData) { if (node->top == -1) return -1;//棧空,報錯 popData = node->data[node->
top]; node->top--; return 1; } //列印棧中全部元素 void printfAll(SqStack* node) { for (int i = 0; i <= node->top; i++) { printf("%d\n", node->data[i]); } system("pause"); } //獲取棧頂元素 int getTop(SqStack* node,int data) { if (node->top == -1) return -1; data = node->
data[node->top]; return 1; } int main() { SqStack test; InitStack(&test); for (int i = 0; i < MAXSIZE; i++) { inStack(&test, i); } printfAll(&test); //system("pause"); return 0; }