C 資料結構之棧 順序棧的實現

2021-08-10 13:40:21 字數 1315 閱讀 5846

停更說明:國慶陪女朋友旅遊去了,並且發生了許多許多有趣的事情,有空再分享哈

這裡該來一條華麗麗的分割線

現在來說說資料結構中的棧,英文中好像是stack,翻譯過來棧其實是很形象的(棧的出口和入口相同,且只有乙個進出口),我們平時通過變數宣告(非new方法)申請來的儲存變數的方法其實就是棧記憶體。

特點:

#pragma once

#include

using

namespace

std;

const

int maxsize = 100;

template

class stack ;

template

stack::stack()

template

stack::~stack()

template

bool stack::empty()

template

bool stack::full()

template

bool stack::enstack(t x)

top++;

data[top] = x;

return

true;

}template

bool stack::pop()

top--;

return

true;

}template

bool stack::gettop(t &element)

return

false;

}

小例子測試

#include

#include"stack.h"

using

namespace

std;

int main()

p->enstack(2); //進棧

if (p->gettop(element))

p->pop(); //出棧

if (p->gettop(element))

cout

<< p->full()

return

0;}

結果:

缺點:缺少了**的健壯性(其實可以裝逼的說魯棒性233)

總結:很像乙個**版的順序表(減少了很多方法),在實際應用的時候應該注意合不合適,陣列的maxsize應該取什麼值

C 資料結構之棧的實現(順序棧)

首先我們定義了乙個棧模板類,它有幾個純虛函式,分別是棧類最常用的幾個函式 push 將元素放入棧頂 top 返回棧頂元素值 pop 彈出棧頂元素 clear 清空棧 析構函式會用到 同樣我們定義了判斷棧是否為空函式isempty 以及保護成員 棧的高度 長度 height 模板類定義 如下 temp...

C 資料結構 棧之順序棧

棧 stack 是一種只允許在一端進行插入或刪除操作的線性表。棧頂 top 是允許進行插入刪除的一端。棧底 bottom 是固定不允許插入刪除的一端。棧的操作特性概括為後進先出 last in first out,lifo 相關知識 c 結構體 c 指標 c 引用 線性表之順序表 假定線性表的元素型...

資料結構之棧的實現 順序棧 C語言

學習參考 嚴蔚敏 資料結構 c語言版 要點 基本操作 入棧 出棧讀棧頂元素值 建棧棧滿 棧空清空棧 銷毀棧 實現 棧結構定義 typedef struct seqstack,pstack 建棧 int initstack pstack s,int size 棧滿 int stackfull psta...