Upload
suebloei
View
592
Download
5
Tags:
Embed Size (px)
DESCRIPTION
S
Citation preview
LOGO
StackStack
Data structure & Algorithms
www.themegallery.com
Company Logo
Stack
Stack เป็�นโครงสรางข้อมู�ลแบบ LIFO (Last-In, First-Out)
Operations พื้��นฐานข้อง Stack ได้แก่� - ก่ารน�าข้อมู�ลเข้าส�� Stack เร�ยก่ว่�า Push - ก่ารน�าข้อมู�ลออก่จาก่ Stack เร�ยก่ว่�า Pop
- ก่ารเร�ยก่ใช้ข้อมู�ลจาก่ Stack เร�ยก่ว่�า Top ก่ารสราง Stack
- ใช้ Array แทน Stack - ใช้ Linked list แทน Stack
www.themegallery.com
Company Logo
Stack
www.themegallery.com
Company Logo
เพื้#$มูข้อมู�ลใน Stack: Push
www.themegallery.com
Company Logo
น�าข้อมู�ลออก่จาก่ Stack : Pop
www.themegallery.com
Company Logo
เร�ยก่ใช้ข้อมู�ลใน Stack: Top
www.themegallery.com
Company Logo
www.themegallery.com
Company Logo
www.themegallery.com
Company Logo
Linked list แทน Stack
www.themegallery.com
Company Logo
Linked list แทน Stack
www.themegallery.com
Company Logo
www.themegallery.com
Company Logo
www.themegallery.com
Company Logo
Operations พื้��นฐานข้อง Stack ท�$สรางด้ว่ย Linked list
1. Create stack: สราง stack head node2. Push stack: เพื้#$มูรายก่ารใน stack3. Pop stack: ลบรายก่ารใน stack4. Stack top: เร�ยก่ใช้รายก่ารข้อมู�ลท�$อย��บนส&ด้ข้อง
stack5. Empty stack: ตรว่จสอบว่�า stack ว่�างเป็ล�าหร�อไมู�6. Full stack: ตรว่จสอบว่�า stack เต)มูหร�อไมู�7. Stack count: ส�งค�าจ�านว่นรายก่ารใน stack 8. Destroy stack: ค�นหน�ว่ยคว่ามูจ�าข้องท&ก่ node ใน
stack ใหระบบ
www.themegallery.com
Company Logo
Stack Applications: Balancing Symbols
www.themegallery.com
Company Logo
Stack Applications: Infix to Postfix conversion
The conversion time is O(n)
www.themegallery.com
Company Logo
Postfix expression evaluation
The evaluation time is O(n)
www.themegallery.com
Company Logo
Backtracking
Backtracking ค�อว่#ธี�ก่ารหาค�าตอบโด้ยเด้#นหนาไป็ย,งเป็-าหมูาย เมู�$อถึ/งทางแยก่ก่)จะตองต,ด้ส#นใจ
เล�อก่เสนทางใด้เสนทางหน/$งเด้#นหนาต�อไป็เพื้�$อหาเป็-าหมูาย หาก่เด้#นไป็จนส&ด้เสนทางแลว่ย,งไมู�พื้บเป็-า ก่)จะเด้#นยอนก่ล,บมูาย,งจ&ด้แยก่คร,�งส&ด้ทายแลว่เล�อก่เสนทางใหมู�ท�$ย,งไมู�เคยไป็ ท�าเช้�นน��ไป็เร�$อยๆจนก่ว่�าจะพื้บ
เป็-าหมูาย หร�อจนครบท&ก่เสนทาง
Backtracking เป็�นก่ารป็ระย&ก่ต1ใช้โครงสรางข้อมู�ลแบบ Stack ส�าหร,บก่ารเข้�ยนโป็รแก่รมู
ป็ระเภทเก่มูส1คอมูพื้#ว่เตอร1 (computer gaming) ก่ารว่#เคราะห1ก่ารต,ด้ส#นใจ(decision
analysis) และระบบผู้�เช้�$ยว่ช้าญ(expert system) ต,ว่อย�างป็5ญหาท�$ใช้ว่#ธี� Backtracking เช้�นป็5ญหาก่ารคนหาเป็-าหมูาย (goal seeking)
และ ป็5ญหา 8 ราช้#น� (eight queens problem)
www.themegallery.com
Company Logo
Stack Applications: Backtracking
www.themegallery.com
Company Logo
Stack Applications: Backtracking
www.themegallery.com
Company Logo
Stack Applications: Backtracking
www.themegallery.com
Company Logo
Stack Applications: Backtracking
LOGO
QueueQueue
Data structure & Algorithms
www.themegallery.com
Company Logo
Queue
Queue เป็�นโครงสรางข้อมู�ลแบบ FIFO (First-In, First-Out)
Operations พื้��นฐานข้อง Queue ได้แก่�- ก่ารน�าข้อมู�ลเข้าส�� Queue เร�ยก่ว่�า Enqueue- ก่ารน�าข้อมู�ลออก่จาก่ Queue เร�ยก่ว่�า Dequeue- ก่ารเร�ยก่ใช้ข้อมู�ลจาก่ห,ว่แถึว่ข้อง Queue เร�ยก่ว่�า Front- ก่ารเร�ยก่ใช้ข้อมู�ลจาก่ทายแถึว่ข้อง Queue เร�ยก่ว่�า Rear
ก่ารสราง Queue - ใช้ Array แทน queue
- ใช้ Linked list แทน queue
www.themegallery.com
Company Logo
The Queue concept
www.themegallery.com
Company Logo
Operation Enqueue
www.themegallery.com
Company Logo
Operation Dequeue
www.themegallery.com
Company Logo
Operation QueueFront
www.themegallery.com
Company Logo
Figure 5-5
Operation QueueRear
www.themegallery.com
Company Logo
Figure 5-6
Queue Operations
www.themegallery.com
Company Logo
Queue Operations
www.themegallery.com
Company Logo
โครงสรางข้อง Queue แบบ Array
www.themegallery.com
Company Logo
Is queue full?
www.themegallery.com
Company Logo
Circular Queue
www.themegallery.com
Company Logo
โครงสรางข้อง Queue แบบ Linked list
www.themegallery.com
Company Logo
Queue data structure
www.themegallery.com
Company Logo
Algorithm พื้��นฐานข้อง Queue
1. Create queue: สราง queue head จาก่ dynamic memory
2. Enqueue: เพื้#$มูรายก่ารเข้าไป็ใน queue3. Dequeue: ลบรายก่ารออก่จาก่ queue4. Queue front: เร�ยก่ใช้ข้อมู�ลท�$ด้านหนาข้อง
queue 5. Queue rear: เร�ยก่ใช้ข้อมู�ลท�$ด้านหนาข้อง
queue6. Empty queue: ตรว่จสอบว่�า queue ว่�างหร�อไมู�7. Full queue: ตรว่จสอบว่�า queue เต)มู
หร�อไมู� (มู�หน�ว่ยคว่ามูจ�า จ,ด้ใหได้หร�อไมู�)8. Queue count: บอก่จ�านว่นรายก่ารใน
queue9. Destroy queue: ลบข้อมู�ลท,�งหมูด้ใน queue
และค�นหน�ว่ยคว่ามูจ�า ใหระบบแลว่ลบและค�นหน�ว่ยคว่ามูจ�าข้อง
head node
www.themegallery.com
Company Logo
Create and enqueue
www.themegallery.com
Company Logo
www.themegallery.com
Company Logo
เพื้#$มูข้อมู�ลเข้า Queue
www.themegallery.com
Company Logo
ลบข้อมู�ลออก่จาก่เข้า Queue
www.themegallery.com
Company Logo
Queue Application
ก่ารเข้�ยนฟั5งก่1ช้,นช้�$อ matching เพื้�$ออ�านอ,ก่ข้ระจาก่แป็-นพื้#มูพื้1 แลว่ท�าก่ารตรว่จสอบด้�ว่�า อ,ก่ข้ระท�$อ�านมูาน,�น อ,ก่ข้ระท&ก่ต,ว่ก่�อนหนาจ&ด้และอ,ก่ข้ระท&ก่ต,ว่ท�$อย��หล,งจ&ด้ เหมู�อนก่,นหร�อไมู� ถึาเหมู�อนก่,นฟั5งก่1ช้,นจะส�งค�นค�า 1 มู#ฉะน,�นจะค�นค�า 0 โด้ยล,ก่ษณะข้องอ,ก่ข้ระท�$อ�านจาก่แป็-นพื้#มูพื้1 มู�ด้,งน��อ,ก่ข้ระย�อย1.อ,ก่ข้ระย�อย2
โด้ยท�$อ,ก่ข้ระย�อยก่�อนหนาจ&ด้และอ,ก่ข้ระย�อยหล,งจ&ด้มู�ข้นาด้คว่ามูยาว่เท�าก่,น
www.themegallery.com
Company Logo
int matching(){ char c1, c2; QueueType queue; int match; clearQ(queue); while((c1=getchar()) != '.') enq(queue, c1); match = 1; while(!emptyQ(queue)&& match) { c1 = deq(queue); c2 = getchar(); if (c2 != c1) match = 0; } return (match);}