棧的基本操作

2021-08-19 21:05:10 字數 1196 閱讀 1300

stack.h

#pragma once 

#include #include #include #include #include #define max_size 12

typedef int datatype;

typedef struct stack

stack;

//棧的初始化

void stackinit(stack* s);

//入棧

void stackpush(stack* s,datatype data);

//出棧

void stackpop(stack* s);

//獲取棧頂元素

datatype stacktop(stack* s);

//獲取棧中元素個數

int stacksize(stack* s);

//檢測棧是否為空

int stackempty(stack* s);

//列印棧中的元素

void stackprint(stack* s);

stack.c

#define _crt_secure_no_warning 1

#include "stack.h"

//棧的初始化

void stackinit(stack* s)

//入棧

void stackpush(stack* s,datatype data)

//出棧

void stackpop(stack* s)

s->_top--;

}//獲取棧中元素個數

int stacksize(stack* s)

return s->_top;

}//檢測棧是否為空

int stackempty(stack* s)

//獲取棧頂元素

datatype stacktop(stack* s)

return s->_array[s->_top-1];

}//列印棧中的元素

void stackprint(stack* s)

printf("null\n");

}

test.c

#include "stack.h"

int main()

棧 的基本操作。

include include typedef struct node node,pnode typedef struct stack stack,pstack void initialize pstack mystack 棧的初始化。int push stack pstack mystack,in...

棧的基本操作

描述 棧是一種重要的資料結構,它具有push k和pop操作。push k是將數字k加入到棧中,pop則是從棧中取乙個數出來。棧是後進先出的 把棧也看成橫向的乙個通道,則push k是將k放到棧的最右邊,而pop也是從棧的最右邊取出乙個數。假設棧當前從左至右含有1和2兩個數,則執行push 5和po...

棧的基本操作

下面先實現站的基本功能,最後通過乙個test來測試下方法是否實現 建個.c檔案 typedef struct seqstack 初始化 seqstack seqstackinit return null 判斷棧是否為空 int seqstackisempty seqstack s void seqs...