棧和佇列的基本概念和操作

2021-08-01 07:48:51 字數 4133 閱讀 1221

棧和佇列

1.棧的定義

棧是限定只能在一端進行插入和刪除的線性表。允許進行插入和刪除操作的一端稱為棧頂,另一端為棧底。當棧中沒有元素時,稱為空棧

棧的特點是「後進先出」(last in first out,lifo),即後入棧的元素先出棧。

2.佇列定義

佇列是限定在一端進行插入,在另一端進行刪除的線性表。佇列中允許插入一端稱為隊尾,允許刪除的一端稱為隊頭

在隊尾插入元素的操作稱為入隊。在隊頭刪除元素的操作稱為出隊。入隊時只涉及隊尾指標的變化;出隊時,只涉及隊頭指標的變化。當佇列中沒有元素時,稱為空隊。

佇列的特點是「先進先出」(first in first out,fifo),即先入隊的元素先出隊。

3.棧的基本操作

#include 

using namespace std;

class stacknode

private:

int data;

stacknode *next;

};class liststack

liststack(const liststack&); //拷貝建構函式

~liststack(); //析構函式

liststack& operator=(const liststack&); //賦值運算子過載

bool isempty()const; //判空

void push(int); //入棧

bool pop(); //出棧

bool get_top(int&)const; //取棧頂元素

protected:

private:

node *top;

void copy(const liststack&); //拷貝功能函式

void clear(); //清空函式,實現析構

};void liststack::copy(const liststack& other) //拷貝構造

else

prenode = newnode;

tmp = tmp->next;

}}void liststack::clear() //清空棧

}liststack::liststack(const liststack& other) //拷貝建構函式

liststack::~liststack() //析構函式

liststack& liststack::operator=(const liststack& other) //賦值運算子過載

return *this;

}bool liststack::isempty() const

//判棧空

void liststack::push(int

value) //入棧

bool liststack::pop()//出棧

node *delnode = top;

top = top->next;

delete delnode;

return

true;

}bool liststack::get_top(int &value)const

//取棧頂元素

value = top->data;

return

true;

}void test()

liststack s2(s1);

liststack s3;

s3 = s1;

intvalue;

while (s1.get_top(value))

cout << endl << "s1 已經清空"

<< endl;

while (s2.get_top(value))

cout << endl << "s2已經清空"

<< endl;

while (s3.get_top(value))

cout << endl << "s3已經清空"

<< endl;

}int main()

4.佇列的基本操作

#include

#include

#include

using namespace std;

struct qnode //結點的結構

};class queuet //佇列的結構

queuet(const queuet& other) //拷貝建構函式

queuet& operator=(const queuet& other) //賦值運算子過載

return

*this;

}~queuet() //析構函式

{}void copy(const queuet &other)

tmp = tmp->next;

if (front ==

null)}}

queuet *insertqueue(queuet *q,int x)//佇列的入隊操作

s->

data

= x;

s->next =

null;

if (q->front ==

null)

else

return q;

}int lengthqueue(queuet *q)

return len;

}queuet *delqueue(queuet *q) //佇列的出隊操作

x = q->front->

data;

p = q->front;

q->front = q->front->next;

if (q->front == q->rear) //佇列中就乙個元素,將佇列置空

free(p);

return q;

}int readheadqueue(queuet *q) //讀取隊頭元素

else

return q->front->

data;

}bool is_emptyqueue(queuet *q) //判斷佇列是否為空

queuet *clearqueue(queuet *q) //清空佇列

q->rear =

null;

return q;

}void print(queuet *q) //列印佇列

else

cout <<

"隊列為空!"

<< endl;

cout << endl;

}protected:

node *front;

node *rear;

};void test()

; int i;

for (i = 0; i<8; i++)

q.print(&q);

cout << q.lengthqueue(&q) << endl;

cout << q.readheadqueue(&q) << endl;

cout << q.is_emptyqueue(&q) << endl;

q.delqueue(&q);

q.print(&q);

queuet q1(q);

q1.print(&q1);

queuet q3;

q3 = q1;

q3.print(&q3);

q.clearqueue(&q);

q.print(&q);

}int main()

棧和佇列的基本概念

1.棧的定義 棧是一種只能在一端進行插入或刪除操作的線性表。其中允許進行插入或刪除操作的一端稱為棧頂 top 棧項由乙個稱為棧頂指標的位置指示器 其實就是乙個變數,對於順序棧,就是記錄棧項元素所在陣列位置標號的乙個整型變數 對於鏈式棧,就是記錄棧頂元素所在結點位址的指標 來指示,它是 動態變化的。表...

堆和棧的基本概念

堆 heap 和棧 stack 是c c 程式設計不可避免會碰到的兩個基本概念。首先,這兩個概念都可以在講資料結構的書中找到,他們都是基本的資料結構,雖然棧更為簡單一些。在具體的c c 程式設計框架中,這兩個概念並不是並行的。對底層機器 的研究可以揭示,棧是機器系統提供的資料結構,而堆則是c c 函...

Python基本概念和基本操作

for迴圈 python中需要注意的問題 參考文獻 變長,可接收任意物件 tuple value 有序可重複 value內容個數均不可變 list value 有序可重複 value內容個數均可變 1.查詢和插入的時間隨著元素的增加而增加 2.占用空間小,浪費記憶體很少 dict map 無序key...