Stack
-
Upload
hamish-mcdaniel -
Category
Documents
-
view
29 -
download
0
description
Transcript of Stack
![Page 1: Stack](https://reader035.fdocument.pub/reader035/viewer/2022062517/568135ca550346895d9d2c1c/html5/thumbnails/1.jpg)
Stack
![Page 2: Stack](https://reader035.fdocument.pub/reader035/viewer/2022062517/568135ca550346895d9d2c1c/html5/thumbnails/2.jpg)
First In First Out : FIFO หมายถึ�งข้อม�ลที่��เข้�ามาในล�สต์�ก่�อน จะถึ�กนาออก่จากล�สต์�เป็�นลำาดั บแรก่ ต์�วอย�างได้แก�การย$นรอคิ�วเพื่$�อซื้$(อต์�)ว
Last In First Out : LIFO หมายถึ�งข้อม�ลที่��เข้�ามาในล�สต์�เป็�นลำาดั บสุ�ดัท้�าย จะถึ�กนาออก่จากล�สต์�เป็�นอ นดั บแรก่ ต์�วอย�างได้แก�การน*าชั้�(นข้องป็,� นโต์เข้าและออกจากเถึาป็,� นโต์
FIFO แลำะ LIFO
![Page 3: Stack](https://reader035.fdocument.pub/reader035/viewer/2022062517/568135ca550346895d9d2c1c/html5/thumbnails/3.jpg)
เป็�นโคิรงสรางข้อม�ลแบบเชั้�งเสน ท้��ม�ก่ารใสุ� ข้�อม�ลำเข้�า แลำะนาข้�อม�ลำออก่เพี�ยงดั�าน
เดั�ยว ด้�งน�(น ข้อม�ลที่��เข้าไป็อย��ใน stack ก�อนจะออกจาก stack หล�งข้อม�ลที่��เข้าไป็ใน stack ที่�หล�ง น��นคิ$อ การ "เข้�าท้�หลำ งแต่�ออก่ก่�อน" (Last In First Out : LIFO)
Stack
![Page 4: Stack](https://reader035.fdocument.pub/reader035/viewer/2022062517/568135ca550346895d9d2c1c/html5/thumbnails/4.jpg)
ป็ฏิ�บ�ต์�การพื่$(นฐานข้องStack ได้แก� push คื!อก่ารนาข้�อม�ลำเก่"บในStack และ pop คื!อก่ารนาข้�อม�ลำออก่จาก่Stack ซื้��งที่�(งสองกระบวนการ จะกระที่*าที่��ส�วนบนส1ด้ข้องStackเสมอ โด้ยป็กต์�แลวม�กก*าหนด้ใหม�ต่ วชี้�%สุ�วนบนสุ�ดัข้องStack เร�ยก่ว�า top ส�วนป็ฏิ�บ�ต์�การอ$�น ๆ เป็�นป็ฏิ�บ�ต์�การที่��เก��ยวเน$�องก�บการ push และ pop ม�ด้�งน�(
การสุร�างStack (CREATE)การที่ด้สอบว�า stack ว�างหร!อไม�(EMPTY)การที่ด้สอบว�า stack เต่"มหร!อไม�(FULL)การท้าให� stack เป็(น stack ว�าง(CLEAR)
ก่ารก่ระท้า(Operation) ท้��เก่��ยวข้�องก่ บ โคืรงสุร�างข้�อม�ลำแบบ Stack
![Page 5: Stack](https://reader035.fdocument.pub/reader035/viewer/2022062517/568135ca550346895d9d2c1c/html5/thumbnails/5.jpg)
ก่ารนาข้�อม�ลำเข้�าสุ��Stack (Push) กระที่*าที่��ส�วนบนข้องStack (Top) ซื้��งต่�องม�ก่ารต่รวจสุอบก่�อนว�าStackเต่"มหร!อไม�
และการน*าข้อม�ลออก่จาก่Stack (Pop) กระที่*าที่��ส�วนบนข้องStackเชั้�นก�น โด้ยต์รวจสอบว�าม�สมาชั้�กอย��ในStackหร$อไม� (ต่รวจสุอบว�าStackว�างเป็ลำ�าหร!อไม�)
![Page 6: Stack](https://reader035.fdocument.pub/reader035/viewer/2022062517/568135ca550346895d9d2c1c/html5/thumbnails/6.jpg)
เป็�นการด้*าเน�นการที่��น*าข้อม�ลเข้าไป็เก3บไว ด้านบนส1ด้ข้องกองซื้อน (Top of the
Stack) เร$�อย ๆ จนกว�ากองซื้อนไม�สามารถึน*าข้อม�ลเข้าไป็เก3บได้
จะเร�ยกว�า กองซื้อนเต์3ม (Stack Full)
การน*าข้อม�ลเข้าไป็ในกองซื้อน (Push)
![Page 7: Stack](https://reader035.fdocument.pub/reader035/viewer/2022062517/568135ca550346895d9d2c1c/html5/thumbnails/7.jpg)
ก่ารท้างานจะต่รงข้�ามก่ บ Pushจะด้�งเอาข้อม�ลที่��อย��บนส1ด้ออกมาก�อน แต์�ก�อนที่��จะด้�งจะม�การ
ต์รวจสอบว�ากองซื้อนว�างหร$อไม� ถ้�าว�างจะไม�สุามารถ้นาข้�อม�ลำออก่ไดั� แสด้งว�า
กองซื้อนว�าง (Stack Empty)ถึาไม�ว�างจะน*าเอาข้อม�ลออกแลวเลำ!�อนต่ วชี้�%ไป็
ย งต่าแหน�งถ้ ดัลำงไป็
การน*าข้อม�ลออกจากกองซื้อน (Pop)
![Page 8: Stack](https://reader035.fdocument.pub/reader035/viewer/2022062517/568135ca550346895d9d2c1c/html5/thumbnails/8.jpg)
ให Y เป็�นสแต์กเก3บคิ�าต์�วเลข้ได้ไม� เก�น 6 ต์�ว
Push(‘2’),Push(‘5’),Push(‘3’)Pop(),Push(‘9’), Push(‘0’)Push(‘4’) Push(‘6’),Pop()
![Page 9: Stack](https://reader035.fdocument.pub/reader035/viewer/2022062517/568135ca550346895d9d2c1c/html5/thumbnails/9.jpg)
เร��มต์นจากกา รสรางสแต์ก
Yข้�(นมาที่*างานจ*าได้ส
แต์กว�าง ไม�ม�สมาชั้�กโด้ยต์�ว
ชั้�(สแต์ก Top ย�งไม�ม�คิ�า
Top
![Page 10: Stack](https://reader035.fdocument.pub/reader035/viewer/2022062517/568135ca550346895d9d2c1c/html5/thumbnails/10.jpg)
2
น*าคิ�า 2 เข้ามาเก3บเป็�นต์�วแรกโด้ยใชั้ Push(‘2’)
สแต์กY=[2] ต์�วชั้�(ส
แต์ก Top = 2 Top
![Page 11: Stack](https://reader035.fdocument.pub/reader035/viewer/2022062517/568135ca550346895d9d2c1c/html5/thumbnails/11.jpg)
2
5
น*าคิ�า 5 เข้ามาเก3บเป็�นต์�วแรกโด้ย
ใชั้Push(‘5’)
สแต์กY=[5] ต์�ว
ชั้�(สแต์กTop = 5
Top
![Page 12: Stack](https://reader035.fdocument.pub/reader035/viewer/2022062517/568135ca550346895d9d2c1c/html5/thumbnails/12.jpg)
2
5
3 Top
น*าคิ�า 3 เข้ามาเก3บเป็�นต์�วแรก
โด้ยใชั้Push(‘3’) สแต์กY=[3] ต์�ว
ชั้�(สแต์กTop = 3
![Page 13: Stack](https://reader035.fdocument.pub/reader035/viewer/2022062517/568135ca550346895d9d2c1c/html5/thumbnails/13.jpg)
5
2
ต์องการด้�งคิ�าออกมาโด้ยใชั้Pop()
สแต์กY=[2,5,3]
ต์�วชั้�(สแต์กTop = 3 Top
![Page 14: Stack](https://reader035.fdocument.pub/reader035/viewer/2022062517/568135ca550346895d9d2c1c/html5/thumbnails/14.jpg)
2
5
9
น*าคิ�า 9 เข้ามาเก3บเป็�นต์�ว
แรกโด้ยใชั้Push(‘9’) สแต์กY=[9]
ต์�วชั้�(สแต์กTop = 9
Top
![Page 15: Stack](https://reader035.fdocument.pub/reader035/viewer/2022062517/568135ca550346895d9d2c1c/html5/thumbnails/15.jpg)
2
5
9
0
น*าคิ�า 0 เข้ามาเก3บเป็�นต์�วแรก
โด้ยใชั้Push(‘0’) สแต์กY=[0]
ต์�วชั้�(สแต์กTop = 0
Top
![Page 16: Stack](https://reader035.fdocument.pub/reader035/viewer/2022062517/568135ca550346895d9d2c1c/html5/thumbnails/16.jpg)
2
5
9
0
4
น*าคิ�า 4 เข้ามาเก3บเป็�นต์�วแรก
โด้ยใชั้Push(‘4’) สแต์กY=[4]
ต์�วชั้�(สแต์กTop = 4
Top
![Page 17: Stack](https://reader035.fdocument.pub/reader035/viewer/2022062517/568135ca550346895d9d2c1c/html5/thumbnails/17.jpg)
2
5
9
0
4
6 น*าคิ�า 6 เข้ามาเก3บเป็�นต์�วแรก
โด้ยใชั้Push(‘6’) สแต์กY=[6]
ต์�วชั้�(สแต์กTop = 6
Top
![Page 18: Stack](https://reader035.fdocument.pub/reader035/viewer/2022062517/568135ca550346895d9d2c1c/html5/thumbnails/18.jpg)
Any list implementation could be used to implement a stackArrays (static: the size of stack is given
initially)Linked lists (dynamic: never become full)
We will explore implementations based on array and linked list
Let’s see how to use an array to implement a stack first
Implementation of Stacks
![Page 19: Stack](https://reader035.fdocument.pub/reader035/viewer/2022062517/568135ca550346895d9d2c1c/html5/thumbnails/19.jpg)
Need to declare an array size ahead of time ม�ที่างใหข้อม�ลเข้า และ ข้อม�ลออก เพื่�ยงด้านเด้�ยวAssociated with each stack is TopOfStack
for an empty stack, set TopOfStack to -1Push
(1) Increment TopOfStack by 1. (2) Set Stack[TopOfStack] = X
Pop (1) Set return value to Stack[TopOfStack] (2) Decrement TopOfStack by 1
These operations are performed in very fast constant time
Array Implementation
![Page 20: Stack](https://reader035.fdocument.pub/reader035/viewer/2022062517/568135ca550346895d9d2c1c/html5/thumbnails/20.jpg)
เป็�นการเต์ร�ยมเน$(อที่��ในหน�วยคิวามจ*าไวส*าหร�บเก3บข้อม�ล ต์�วอย�างในภาษาซื้� คิ$อ
int Stack[4];
การน*าข้อม�ลเข้าและออกจากหน�วยคิวามจ*าด้วยแถึวล*าด้�บ ก3เหม$อนก�บที่��ยกต์�วอย�างไป็แลว
การสรางกองซื้อนด้วยแถึวล*าด้�บ
Stack
![Page 21: Stack](https://reader035.fdocument.pub/reader035/viewer/2022062517/568135ca550346895d9d2c1c/html5/thumbnails/21.jpg)
Stack classclass Stack {public:
Stack(int size = 10); // constructor~Stack() { delete [] values; } // destructorbool IsEmpty() { return top == -1; }bool IsFull() { return top == maxTop; }double Top();void Push(const double x);double Pop();void DisplayStack();
private:int maxTop; // max stack size = size - 1int top; // current top of stackdouble* values; // element array
};
![Page 22: Stack](https://reader035.fdocument.pub/reader035/viewer/2022062517/568135ca550346895d9d2c1c/html5/thumbnails/22.jpg)
Attributes of StackmaxTop: the max size of stacktop: the index of the top element of stackvalues: point to an array which stores elements of
stackOperations of Stack
IsEmpty: return true if stack is empty, return false otherwise
IsFull: return true if stack is full, return false otherwise
Top: return the element at the top of stackPush: add an element to the top of stackPop: delete the element at the top of stackDisplayStack: print all the data in the stack
Stack class
![Page 23: Stack](https://reader035.fdocument.pub/reader035/viewer/2022062517/568135ca550346895d9d2c1c/html5/thumbnails/23.jpg)
The constructor of StackAllocate a stack array of size. By default, size = 10.
When the stack is full, top will have its maximum value, i.e. size – 1.
Initially top is set to -1. It means the stack is empty.
Create Stack
Stack::Stack(int size /*= 10*/) {maxTop = size - 1;values = new double[size];top = -1;
}
Although the constructor dynamically allocates the stack array, the stack is still static. The size is fixed after the initialization.
![Page 24: Stack](https://reader035.fdocument.pub/reader035/viewer/2022062517/568135ca550346895d9d2c1c/html5/thumbnails/24.jpg)
void Push(const double x);Push an element onto the stackIf the stack is full, print the error information.Note top always represents the index of the top
element. After pushing an element, increment top.
Push Stack
void Stack::Push(const double x) {if (IsFull())
cout << "Error: the stack is full." << endl;else
values[++top] = x;}
![Page 25: Stack](https://reader035.fdocument.pub/reader035/viewer/2022062517/568135ca550346895d9d2c1c/html5/thumbnails/25.jpg)
double Pop()Pop and return the element at the top of the
stackIf the stack is empty, print the error information.
(In this case, the return value is useless.)Don’t forgot to decrement top
Pop Stack
double Stack::Pop() {if (IsEmpty()) {
cout << "Error: the stack is empty." << endl;return -1;
}else {
return values[top--];}
}
![Page 26: Stack](https://reader035.fdocument.pub/reader035/viewer/2022062517/568135ca550346895d9d2c1c/html5/thumbnails/26.jpg)
double Top()Return the top element of the stackUnlike Pop, this function does not remove
the top element
Stack Top
double Stack::Top() {if (IsEmpty()) {
cout << "Error: the stack is empty." << endl;return -1;
}else
return values[top];}
![Page 27: Stack](https://reader035.fdocument.pub/reader035/viewer/2022062517/568135ca550346895d9d2c1c/html5/thumbnails/27.jpg)
void DisplayStack()Print all the elements
Printing all the elements
void Stack::DisplayStack() {cout << "top -->";for (int i = top; i >= 0; i--)
cout << "\t|\t" << values[i] << "\t|" << endl;cout << "\t|---------------|" << endl;
}
![Page 28: Stack](https://reader035.fdocument.pub/reader035/viewer/2022062517/568135ca550346895d9d2c1c/html5/thumbnails/28.jpg)
Using Stack
int main(void) {Stack stack(5);stack.Push(5.0);stack.Push(6.5);stack.Push(-3.0);stack.Push(-8.0);stack.DisplayStack();cout << "Top: " << stack.Top() << endl;
stack.Pop();cout << "Top: " << stack.Top() << endl;while (!stack.IsEmpty()) stack.Pop();stack.DisplayStack();return 0;
}
result
![Page 29: Stack](https://reader035.fdocument.pub/reader035/viewer/2022062517/568135ca550346895d9d2c1c/html5/thumbnails/29.jpg)
โคิรงสรางข้อม�ลแบบ stack ม�การป็ระย1กต์�ใชั้มากในการเข้�ยนโป็รแกรมข้องสาข้าว�ที่ยาการคิอมพื่�วเต์อร� เชั้�น การจ�ด้สรรหน�วยคิวามจ*าในการป็ระมวลผลโป็รแกรม
(Function Call)รวมที่�(ง โป็รแกรมเร�ยกใชั้ต์�วเอง (Recursive)
การต์รวจสอบอ�กข้ระสมด้1ล(Balancing Symbol) การคิ*านวณน�พื่จน�คิณ�ต์ศาสต์ร�
ก่ารป็ระย�ก่ต่+ใชี้� stack
![Page 30: Stack](https://reader035.fdocument.pub/reader035/viewer/2022062517/568135ca550346895d9d2c1c/html5/thumbnails/30.jpg)
การเร�ยกใชั้ Function หร$อ Procedure หร$อโป็รแกรมย�อยในภาษาที่��ไม�ม�การ
Recursive เม$�อม�การเร�ยกใชั้ Function ก3จะที่*าการ
Push Function to Stack และเม$�อม�การ Return หร$อจบการที่*างานข้อง
Function แลวจะต์อง Pop Function from Stack
Function Call
![Page 31: Stack](https://reader035.fdocument.pub/reader035/viewer/2022062517/568135ca550346895d9d2c1c/html5/thumbnails/31.jpg)
การเร�ยกโป็รแกรมย�อยม�คิวามแต์กต์�างก�บการกระโด้ด้ที่��วไป็ เน$�องจากภายหล�งที่��โป็รแกรมย�อยที่*างานเสร3จ หน�วยป็ระมวลผลจะต์องสามารถึกระโด้ด้กล�บมาที่*างานในโป็รแกรมหล�กต์�อไป็ได้ ดั งน %นก่ารเร�ยก่ใชี้�โป็รแก่รมย�อยน %นจะต่�องม�ก่ารเก่"บต่าแหน�งข้องคืาสุ �งท้��ท้างานอย��เด้�มด้วย และเม!�อจบโป็รแก่รมย�อยโป็รแก่รมจะต่�องก่ระโดัดัก่ลำ บมาท้างานท้��เดั,ม โด้ยใชั้ข้อม�ลที่��เก3บไว ภาพื่และอ�ลกอร�ที่�มแสด้งต์�วอย�างการเร�ยกใชั้โป็รแกรมย�อย
ก่ารเร�ยก่ใชี้�โป็รแก่รมย�อย
![Page 32: Stack](https://reader035.fdocument.pub/reader035/viewer/2022062517/568135ca550346895d9d2c1c/html5/thumbnails/32.jpg)
PROGRAM MAIN......CALL Sub1PRINT Q....END MAINPROCEDURE Sub1....CALL Sub2A:=A+B...END Sub1PROCEDURE Sub2...END Sub2
![Page 33: Stack](https://reader035.fdocument.pub/reader035/viewer/2022062517/568135ca550346895d9d2c1c/html5/thumbnails/33.jpg)
Example :
{x+(y-[a+b])*c-[(d+e)]}/(h-(j-(k-[l-n])))
Balancing Symbols
![Page 34: Stack](https://reader035.fdocument.pub/reader035/viewer/2022062517/568135ca550346895d9d2c1c/html5/thumbnails/34.jpg)
ในก่ารต่รวจสุอบอ ก่ข้ระสุมดั�ลำน %น คือมไพีเลำอร+ ไดั�นาแนวคื,ดัโคืรงสุร�างข้�อม�ลำแบบ Stack มา
ป็ระย�ก่ต่+ โดัยม�ว,ธี�ก่ารดั งน�% 1. ให�อ�านอ ก่ข้ระท้�ลำะต่ ว - ถ้�าอ ก่ข้ระเป็(นอ ก่ข้ระเป็.ดั เชี้�น {,(,
[ เป็(นต่�น ให� PUSH ลำง stack - ถ้�าอ ก่ข้ระเป็(นอ ก่ข้ระป็.ดั เชี้�น },),]
เป็(นต่�น ให�ต่รวจสุอบว�าอ ก่ข้ระบน TOP ข้อง stack เป็(นอ ก่ข้ระเป็.ดัท้��คื��ก่ นหร!อไม�
- ถ้�าใชี้� ให� POP อ ก่ข้ระน %นออก่จาก่ stack
- ถ้�าไม�ใชี้� ให�แสุดังผลำ error 2.เม!�ออ�านอ ก่ข้ระหมดัแลำ�ว แต่� stack ไม�เป็(น
stack ว�าง ให�แสุดังผลำ error
ก่ารต่รวจสุอบอ ก่ข้ระสุมดั�ลำ(Balancing Symbol)