資料結構與演算法(C語言版) 鏈式棧

2021-07-25 15:24:24 字數 1065 閱讀 2810

用鍊錶做的堆疊,鍊錶可以動態的建立一些節點,在插入和刪除節點方面要快很多。

先進後出棧操作:

push入棧

pop出棧

top返回棧頂資料

在vs2013新建專案,在標頭檔案中加入linkedstack.h在原始檔中加入main.cpp

#ifndef linkedstack_h

#define linkedstack_h

template

class linkedstack;

template

class chainnode//建構函式

t data;

chainnode*link;

};template

class linkedstack

//~linkedstack()

bool isempty() const;

t& top() const;

void push(const t& e);

void pop();

void makeempty();

private:

chainnode*top;

};template

bool linkedstack::isempty() const

template

void linkedstack::push(const t &e)

template

t& linkedstack::top() const

template

void linkedstack::pop()

template

void linkedstack::makeempty()

#endif

#include

#include"linkedstack.h"

using

namespace

std;

int main()

總結:棧操作是比較常用的操作,鍊錶做的堆疊比動態陣列做的堆疊不太一樣,也有一些優勢。

資料結構與演算法(C語言版) 鏈式佇列

用鍊錶做的佇列 先進先出 使用c 模板類 動態陣列做可能會浪費空間,但是鍊錶的利用率就比較高,今天用鍊錶做乙個佇列。在vs2013中新建專案,在標頭檔案中加入queueli.h在原檔案中加入main.cpp ifndef queueli h define queueli h templateobje...

資料結構與演算法(C語言版) 棧

棧是一種常用的資料結構,棧常用在系統軟體和或者演算法中。棧使用陣列來做順序棧,鏈式站用鍊錶來做。今天使用動態陣列來設計棧。棧,後進先出 lifo 先進後出 filo push,進棧 pop,出棧 peek,看一下棧頂 我使用的是vs ultimate2013 新建乙個空專案,在標頭檔案裡面新增兩個標...

資料結構(C語言版) 棧

1 棧 僅在表尾進行插入和刪除操作的線性表。後進先出lifo。1 表尾端 允許插入和刪除的一端 為棧頂,表頭端 不允許插入和刪除的一端 為棧底。2 入棧 插入元素的操作。出棧 刪除棧頂元素 2 棧的兩種儲存表示方式 2 鏈棧 棧的鏈式儲存結構 優點是便於多個棧共享儲存空間和提高效率。3 括號匹配檢驗...