SlideShare a Scribd company logo
1 of 20
โครงสร้างข้อมูลสแตก (Stack)
วิชาโครงสร้างข้อมูลและอัลกอริทึม
ครูประจาวิชา
นางวราดา ขันตี
แผนกวิชาคอมพิวเตอร์ธุรกิจ
วิทยาลัยเทคนิคศรีสะเกษ
โครงสร้างข้อมูลสแตก (Stack)
ในการใช้งานโครงสร้างข้อมูลที่เปิด
ปลายเพียงด้านเดียว การดาเนินการกับ
ข้อมูลในโครงสร้างสามารถกระทาได้เพียง
ปลายข้างหนึ่งเท่านั้น ข้อมูลที่ถูกบรรจุอยู่ใน
โครงสร้างดังกล่าว ตัวแรกไม่สามารถถูกดึง
ข้อมูลออกจากโครงสร้างได้ถ้าข้อมูลตัวที่เข้า
มาในลาดับหลังยังไม่ถูกดึงออกไปก่อน
โครงสร้างข้อมูลที่มีลักษณะดังกล่าวนี้ไม่
สามารถใช้งานโครงสร้างอาร์เรย์ (Array)
ทั่วไปได้ จึงมีการใช้โครงสร้างข้อมูลที่เรียกว่า
โครงสร้างข้อมูลสแตก (Stack) มาแทน
2
ลักษณะของโครงสร้างข้อมูล
สแตก (Stack)
โครงสร้างข้อมูลแบบสแตก (Stack)
เป็นโครงสร้างข้อมูลแบบรายการเชิงเส้น (Linear
List) ที่มีลักษณะที่สาคัญคือ การนาข้อมูลเข้าสู่ส
แตก (Insertion : บางครั้งอาจเรียกว่า Pushing)
และการนาข้อมูลออกจากสแตก (Deletion
บางครั้งเรียกว่า Popping) สามารถกระทาได้
เพียงปลายด้านเดียวของโครงสร้างเท่านั้น โดย
ข้อมูลที่เข้าไปเก็บทีหลังจะถูกนาออกมาใช้งาน
ก่อน จะเรียกลักษณะแบบนี้ว่า เข้าหลังออกก่อน
(Last In First Out : LIFO)
3
4
ลักษณะของโครงสร้างข้อมูล
สแตก (Stack)
โครงสร้างของสแตก
5
ลักษณะของโครงสร้างข้อมูล
สแตก (Stack)
ลักษณะของสแตกในชีวิตประจาวัน
การแทนโครงสร้างข้อมูลสแตก (Stack)
โครงสร้างข้อมูลแบบสแตกสามารถ แทนได้ด้วยโครงสร้างข้อมูล
อาร์เรย์ (Array) แต่กาหนดวิธีการเข้าถึงข้อมูลในอาร์เรย์นั้นตามกฏเกณฑ์
ของสแตก คือ เข้าหลังออกก่อน (Last In First Out : LIFO) ดังนั้นการเข้าถึง
ข้อมูลในโครงสร้างสแตกจะต้องอาศัยพอยน์เตอร์ (Pointer) ซึ่งทาหน้าที่ชี้
ตาแหน่งของข้อมูลตัวสุดท้ายของสแตก
6Charter 3
7
A
B
C
D
E Pointer
การแทนโครงสร้างข้อมูล
สแตก (Stack)
โครงสร้างอาร์เรย์
(Array)
8
…
1 2 9
9
1
0
0
8 …
8 2
0
…
8 2
0
6 …
1
2
3
Poin
ter
Stac
k[10
0]
แสดงโครงสร้างข้อมูลสแตกด้วยอาร์เรย์
Stack[100]
การแทนโครงสร้างข้อมูล
สแตก (Stack)
การดาเนินการกับโครงสร้างข้อมูลสแตก (Stack)
มีอยู่ 3 กระบวนการ คือ
1. การสร้างสแตก
2. การเพิ่มข้อมูลเข้าสแตก (Insertion หรือ Pushing)
3. การลบข้อมูลในสแตก (Deletion หรือ Popping)
9Charter 3
ค่าที่เกี่ยวข้องกับโครงสร้างข้อมูล
สแตก
1. สแตกพอยน์เตอร์ (Stack Pinter : SP)
เป็นตัวชี้ข้อมูลค่าที่อยู่บนสุดของสแตก (Top of
Stack)
2. พุช (Push) เป็นการกระทาเพื่อนาข้อมูล
เข้าสู่สแตก เมื่อสแตกเต็มแล้ว หากมีการนาข้อมูลเข้า
สู่สแตกอีกจะเกิดข้อผิดพลาดที่เรียกว่า โอเวอร์โฟลว์
(Over flow) ขึ้น
3. ป๊ อบ (Pop) เป็นการกระทาเพื่อนาข้อมูล
ที่อยู่บนสุดออกมาจากโครงสร้างสแตก เมื่อนาข้อ
มูลค่าสุดท้ายออกจากสแตกแล้วจะทาให้สแตกว่าง
เปล่า (Empty) ได้ หากมีการ Pop ค่าข้อมูลอีกจะเกิด
ข้อผิดพลาดที่เรียกว่า อันเดอร์โฟลว์ (Under flow)
ขึ้น
1
0
ลักษณะการทางานของโครงสร้าง
ข้อมูลสแตก (Stack)
1
1
Q
A
Q A
Q
(Empty)
Push box Q
onto empty
stack
Push box A
onto stack
Pop box A
from stack
Pop box Q
from stack
Pushing and Popping a stack
1
2
R
D
M
D
M
Push box R
onto empty
stack
Push box D
onto stack
Push box M
onto stack
Pop box M
from stack
ลักษณะการทางานของ
โครงสร้างข้อมูลสแตก (Stack)
Pushing and Popping a stack (Continue)
1
3
Q
S
Push box Q onto stack
Push box S onto stack
ลักษณะการทางานของโครงสร้างข้อมูลสแตก (Stack)
Pushing and Popping a stack (Continue)
1
4ลักษณะการทางานของ
โครงสร้างข้อมูลสแตก (Stack)
Stack frames for subprogram calls
การสร้างสแต็ก
ด้วยโครงสร้างอาร์เรย์
 การเพิ่มข้อมูล เรียกว่า PUSH
เช่น PUSH 3
3
PUSH 8
8
3
วิธีการนาข้อมูลออก เรียกว่า POP
เช่น POP 8
8
3
POP 3
3
จบการนาเสนอ

More Related Content

Featured

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by HubspotMarius Sescu
 
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
 

Featured (20)

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot
 
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...
 

โครงสร้างข้อมูลแบบสแต็ก