SlideShare a Scribd company logo
1 of 27
Download to read offline
คือ
คือ ชุดข้อมูลที่เรียงต่อกันและจัดลำดับกำร
เก็บข้อมูลแบบเข้ำที่หลังและออกก่อน
เรียกได้ว่ำเป็นกำรใส่ของแบบไลโฟLIFO =
Last in First out
กำรใช้สแตกนั้นง่ำยสำหรับระบบ
คอมพิวเตอร ์
การนาข้อมูลเข้า Stack
Push คือ กำรนำเข้ำข้อมูลใส่ลงไปในสแตกเช่น
สแตก s ต้องกำรใส่ข้อมูล i ในสแตกจะได้ push
(s,i) คือใส่ข้อมูล i ลงไปที่ท็อปของสแตก s ในกำร
เพิ่มข้อมูลลงในสแตก จะต้องทำกำรตรวจสอบว่ำส
แตก เต็มหรือไม่ ถ้ำไม่เต็มก็สำมำรถเพิ่มข้อมูลลงไป
ในสแตกได้แล้วปรับตัวชี้ตำแหน่งให้ไปชี้ที่ตำแหน่ง
ใหม่ ถ้ำสแตกเต็ม (Stack Overflow) ก็จะไม่
สำมำรถเพิ่มข้อมูลเข้ำไปในสแตกได้อีก
การนาข้อมูลออกจาก Stack
Pop คือกำรนำข้อมูลออกจำกส่วนบนสุดของสแตก
เช่น ต้องกำรนำข้อมูลออกจำกสแตก s ไปไว้ที่ตัวแปร
i จะได้ i = pop (s) กำรนำข้อมูลออกจำกสแตก
ถ้ำสแตกมีสมำชิกเพียง 1 ตัวแล้วนำสมำชิกออกมำ
จำกสแตก จะเกิดภำวะสแตกว่ำง (Stack) คือไม่มี
สมำชิกอยู่ในสแตก แต่ถ้ำไม่มีสมำชิกในสแตกแล้วทำ
กำร pop สแตกจะทำให้เกิดควำมผิดพลำดที่เรียกว่ำ
Stack Underflow ก่อนนำข้อมูลออกจำกสแตก
จะต้องตรวจสอบดูก่อนว่ำสแตกว่ำงหรือเปล่ำ จึงจะนำ
ข้อมูลออกจำกสแตกได้
Queue
เป็นโครงสร ้ำงข้อมูลแบบเชิงเส้นหรือลิเนียร ์ลิสตซึ่ง
กำรเพิ่มข้อมูลจะกระทำที่ปลำยข้ำงหนึ่งซึ่งเรียกว่ำ
ส่วนท้ำยหรือเรียร ์(rear)และกำรนำข้อมูลออกจะ
กระทำที่ปลำยอีกข้ำงหนึ่งซึ่งเรียกว่ำ ส่วนหน้ำ หรือฟ
รอนต์(front)
กำรใส่สมำชิกตัวใหม่ลงในคิวเรียกว่ำ Enqueue
ซึ่งมีรูปแบบคือ enqueue (queue, newElement)
หมำยถึง กำรใส่ข้อมูล newElement ลงไปที่ส่วนเรียร ์
ข้อมูลใส่ด้ำนท้ำยแถวเสมอ
การนาข้อมูลเข้า Queue
เรียกว่ำ Dequeue ซึ่งมีรูปแบบคือ dequeue
(queue, element) หมำยถึง กำรนำออกจำกส่วน
หน้ำ ของคิวและให้ ข้อมูลนั้นกับ element นำข้อมูล
หัวแถวออกก่อนเสมอ
การนาข้อมูลออกจาก Queue
Circular Queue
คิววงกลม หมำยถึง คิวที่ถูกออกแบบมำให้มีลักษณะเป็น
วงกลมเพื่อให้สำมำรถนำข้อมูลใหม่ไปเก็บไว้ที่ช่องว่ำงด้ำน
หน้ำคิวได้ คิววงกลมออกแบบมำเพื่อแก้ปัญหำคิวธรรมดำ
คิววงกลมจะสามารถเพิ่มข้อมูลเข้าไปในคิวได้จนกว่าคิวจะเต็มจริง ๆ
เหมือนคิวธรรมดาคือมีตัวชี้2 ตัวคือ Front และ
Rear สาหรับแสดงตาแหน่งตัวและท้ายคิวตามลาดับ
แตกต่างจากคิวธรรมดา คือ คิวธรรมดาเมื่อ Rear
ชี้ตาแหน่งสุดท้าย จะไม่สามารถเพิ่มข้อมูลเข้าไปได้
อีก ทั้งที่ยังมีคิวว่างอยู่ก็ตาม
คิววงกลมจัดการปัญหานี้โดย Rear ชี้อยู่ตาแหน่ง
สุดท้ายของคิว หากมีการเพิ่มข้อมูล ค่าของ Rear จะ
สามารถวนมาชี้ยังตาแหน่งแรกสุดของคิวได้
ดังนั้นคิววงกลมจะสามารถเพิ่มข้อมูลเข้าไปได้
จนกว่าจะเต็มจริงๆ
คิววงกลม
การนาสมาชิก
ออกจากคิววงกลม
การนาสมาชิก
เข้าสู่วงกลม
Priority Queue
ในกำรทำงำนกับคิวแบบนี้ ต้องมีค่ำอภิสิทธิ์ของแต่ละ
สมำชิกเก็บไว้ด้วย เพื่อใช ้หำตำแหน่งที่อยู่ก่อนหน้ำ
สมำชิกที่มีอภิสิทธิ์ต่ำกว่ำและตำมหลังสมำชิกที่มีอภิสิทธิ์
เท่ำกันหรือสูงกว่ำ
typedef struct { int priority;
char data;
} Queue;
Queue priority_queue[15];
คิวลำดับควำมสำคัญ หรือ แถวคอยเชิงบุริมภำพ
(Priority Queue)
ตัวอย่ำงกำรประยุกต์คิวมำใช้งำนบนคอมพิวเตอร ์
-กำรเข้ำคิวของโปรเซสหรืองำนต่ำง ๆ เพื่อรอกำร
ประมวลผลจำกซีพียูตำมลำดับ
-กำรแบ่งเวลำ (Time Sharing) ด้วยกำรจำกัด
ตำรำงเวลำกำรประมวลผลของแต่ละโปรเซส ส่งผลให้
สำมำรถหมุนเวียนกำรประมวลผลในแต่ละโปรเซสสลับ
ไปมำได้ ทำให้ดูเหมือนกับกำรประมวลผลงำนหลำย ๆ
อย่ำงได้ในเวลำเดียวกัน (Multitasking)
-กำรเข้ำคิวเพื่อรอผลลัพธ์จำกเครื่องพิมพ์
การประยุกต์ใช ้Queue
Recursion
• recursion ชื่อภำษำไทยคือ กำรเวียนเกิด
หมำยถึง กำรเขียนโปรแกรมแบบวนซ้ำเพื่อ
เรียกฟังก์ชันตัวเอง
• ฟังก์ชันที่เป็น recursive ก็ต่อเมื่อตัว
ฟังก์ชันนั้นมีกำรเรียกใช ้ฟังก์ชันตัวเอง
• ฟังก์ชัน recursive ใช ้สำหรับกำรเขียน
ฟังก์ชันที่ใช ้เรียกตัวองซ้ำๆ เช่น กำรหำ
ค่ำ Power(x,n), Factorial , Fibonacci
number , Tower of Hanoi, List Files
and Folders
• ในกำรแก้ปัญหำบำงกรณี วิธีคิดแบบวนซ้ำ
(for loop) อำจยำกกว่ำกำรคิดแบบ เรียก
ตัวเอง
Recursion
ประเภทของ recursion
1. Linear Recursion
2. Binary Recursion
3. Multiple Recursion
Linear Recursion
เป็นกำรเขียน recursive แบบง่ำย
สำมำรถเขียนเป็นแบบลูปธรรมดำได้เช่น
โปรแกรมหำผลรวม ผลคูณ Factorial
Fibonacci number
ตัวอย่ำง
factorial
Linear Recursion
for loop recursive
Binary Recursion
เป็นกำรเขียนฟังก์ชันเพื่อเรียกตัวเอง โดยทำกำร
เรียกทีละ 2 recursive
ตัวอย่ำง
ลำดับเลข fibonacci หมำยถึงลำดับเลขต่อไปนี้
0,1,1,2,3,5,8,13,21,34,…, fib(n-1) + fib(n-2)
Binary Recursion
for loop recursive
Multiple Recursion
เป็นกำรเขียนฟังก์ชันเพื่อเรียกตัวเอง โดย
ทำ กำรเรียกหลำยๆ recursive
ตัวอย่ำงให้แสดงผลควำมน่ำจะเป็นในกำร
ผสมตัวอักษรทั้งหมด จำกตัวอักษรชุด
เช่น ABC คำตอบที่ได้ คือ ABC ACB
BAC BCA CAB CBA
Multiple Recursion
Multiple Recursion
จัดทำโดย
นำงสำวธนพร เพ็งจันทร ์รหัสนิสิต
59010230
นำงสำวกัญญำรัตน์ สนั่นนำรี รหัสนิสิต
60018500

More Related Content

Featured

Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTExpeed Software
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsPixeldarts
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthThinkNow
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfmarketingartwork
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsKurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summarySpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project managementMindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...RachelPearson36
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Applitools
 

Featured (20)

Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
 

Present Unit 5-6 (BC0212)