1 / 51

โครงสร้างข้อมูลแบบ Stack

โครงสร้างข้อมูลแบบ Stack. Data Structure. Department of Computer Science. ลักษณะของโครงสร้างข้อมูลแบบ Stack.

Télécharger la présentation

โครงสร้างข้อมูลแบบ Stack

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. โครงสร้างข้อมูลแบบ Stack Data Structure Department of Computer Science

  2. ลักษณะของโครงสร้างข้อมูลแบบ Stack • ข้อมูลที่เก็บใน Stack จะเก็บในลักษณะวางทับกัน เช่นเดียวกับการวางจานเรียงซ้อนกัน ข้อมูลตัวแรกจะเป็นข้อมูลที่อยู่ล่างสุดของ Stack และ ข้อมูลสุดท้ายจะเป็นข้อมูลที่อยู่บนสุด ของ Stack เมื่อมีการนำข้อมูลออกจาก Stack ข้อมูลที่อยู่บนสุด นั่นคือข้อมูลที่นำลงสู่Stack เป็นข้อมูลสุดท้าย จะเป็นข้อมูลที่จะต้องนำออกจาก Stack ก่อน การทำงานลักษณะนี้เรียกว่า LIFO (last-in-first-out) Data Structure Department of Computer Science

  3. Push Stack • Algorithm 1. ตรวจสอบว่า Stack เต็ม ? (โดยการตรวจสอบว่า Top เท่ากับ N) - ถ้า Stack เต็ม ให้แสดงข้อความว่า "Stack Overflow" แล้วเลิกงาน - ถ้า Stack ไม่เต็ม ให้ทำงานข้อที่ 2 และ 3 2. ให้เพิ่มค่าของ Top อีก 1 3. ใส่ข้อมูลลงใน Stack ในตำแหน่งของตัวแปร Top Data Structure Department of Computer Science

  4. Operation ของ Stack • Push Stack เป็น operation สำหรับนำข้อมูลลงใน Stack • Pop Stack เป็น operation สำหรับนำข้อมูลออกจาก Stack Data Structure Department of Computer Science

  5. ตัวอย่างการทำงานของ Operation Push และ Pop Data Structure Department of Computer Science

  6. Implementation Stack • การ implement Stack ทำได้ 2 วิธี คือ1. Array Implementation2. Linked List Implementation • Array Implementationข้อตกลง 1) กำหนดให้จำนวนข้อมูลสูงสุดคือ N ทั้งนี้เพราะว่าการกำหนด Array จะต้องระบุจำนวนข้อมูลสูงสุดที่จะเก็บลงใน Array 2) กำหนดให้ตัวแปร Top แทนจำนวนข้อมูลที่มีอยู่ใน Stack Data Structure Department of Computer Science

  7. Flowchart Department of Computer Science

  8. Programตัวอย่างโปรแกรมนี้เป็น Stack ของ Integer • Pascal Procedure Push ( X ) ;BeginIf Top=N Then Writeln ( " Stack Overflow " ) ElseBeginTop := Top + 1 ;Stack [ Top ] := X ;End;End; Data Structure Department of Computer Science

  9. Programตัวอย่างโปรแกรมนี้เป็น Stack ของ Integer • C,C++ Push ( int X ) {if (Top = = N) printf ( " Stack Oerflow " );else{Top = Top + 1 ;Stack [ Top ] = X ;}} Data Structure Department of Computer Science

  10. Programตัวอย่างโปรแกรมนี้เป็น Stack ของ Integer • C,C++ ฟังก์ชั่นการ Push int Push ( int what) { if (Top<MAX-1) { top++; stackdata[top]=what; return 1; } return -1; } Data Structure Department of Computer Science

  11. ฟังก์ชั่นการ Push • ฟังก์ชันนี้เป็นการนำข้อมูลเข้าสู่ Stack คือ ถ้า top น้อยกว่า MAX-1 ซึ่งก็คือ ถ้า Stack ยังไม่เต็ม ก็ให้ top++ และเก็บข้อมูลที่ส่งมาทางพารามิเตอร์ what ลงไปในตัวแปร stackdata ช่องที่ top ชี้อยู่นั้นเอง จากนั้น return 1 กลับไปยังจุดที่เรียกใช้เป็นการบอกสถานะว่าตอนนี้ได้ push เรียบร้อยแล้วนั่นเอง แต่ถ้าเงื่อนไข if ไม่เป็นจริง มันก็จะลงมาทำที่ return-1 ดังนั้น ฟังก์ชันนี้คืนค่า -1 แสดงว่า stack เต็มแล้วนั่นเอง Data Structure Department of Computer Science

  12. ..ตัวอย่างการทำงานของ Push Stack.. • ทำการ Push ข้อมูลต่อไปนี้ 9 5 7 2 8 เข้าเก็บใน Stack • ดูตัวอย่าง Data Structure Department of Computer Science

  13. Pop Stack • Algorithm 1. ตรวจสอบว่า Stack ว่าง ? (โดยการตรวจสอบว่า Top เท่ากับ 0)- ถ้า Stack ว่าง ให้แสดงข้อความว่า "Stack Empty" แล้วเลิกงาน- ถ้า Stack ยังมีข้อมูล ให้ทำงานข้อที่ 2 และ 32. ให้นำข้อมูลออกจาก Stack3. ให้ลดค่าของ Top ลงอีก 1 Data Structure Department of Computer Science

  14. Flowchart

  15. Programตัวอย่างโปรแกรมนี้เป็น Stack ของ Integer • Pascal Procedure Pop ( Var X : Integer ) ;BeginIf Top=0 Then Writeln ( " Stack Empty " ) ElseBeginX := Stack [ Top ] ;Top := Top - 1 ;End;End; Data Structure Department of Computer Science

  16. Programตัวอย่างโปรแกรมนี้เป็น Stack ของ Integer • C,C++ Pop ( int X ){if (Top = = 0) printf ( " Stack Empty " );else{X = Stack [ Top ] ;Top = Top - 1 ;}} Data Structure Department of Computer Science

  17. Programตัวอย่างโปรแกรมนี้เป็น Stack ของ Integer • C,C++ ฟังก์ชั่นการ Pop int pop() { int r; if (top>-1) { r=stack[top]; stack[top]=0; top--; return r; } return -1; } Data Structure Department of Computer Science

  18. ฟังก์ชั่นการ Pop การเอาข้อมูลออกจาก Stack ได้นั้น ก็เพียงแค่คืนค่าข้อมูลตัวที่ top นั้นชี้อยู่กลับไป แต่ก่อนที่จะ Pop ได้นั้นจะต้องดูด้วยว่า top นั้นเป็น -1 หรือไม่ ถ้าเป็น -1 ก็แสดงว่าตอนนั้น Stack ว่าง ให้คืนค่า -1 กลับไป แต่ถ้า top >-1 แสดงว่ามัมมีข้อมูลอยู่ภายใน Stack ดังนั้น ให้คืนค่าตัวที่ top ชี้อยู่กลับไป และลดค่า top ลงอีก 1 Data Structure Department of Computer Science

  19. ..ตัวอย่างการทำงานของ Pop Stack.. • ทำการ Pop ข้อมูลต่อไปนี้ 8 2 7 5 9 ออกจาก Stack • ดูตัวอย่าง Data Structure Department of Computer Science

  20. ตัวอย่างโปรแกรมแสดงการทำงานของ Stack ตัวอย่างที่ 1 ตั้งชื่อไฟล์ว่า Stack1.c #include <stdio.h> #include <stdlib.h> #include <conio.h> #define MAX 10 int stackdata[MAX]; int top =-1; int Push(int what) { if (top<MAX-1) { top++; stackdata[top] = what; return 1; } return -1; } Department of Computer Science Data Structure

  21. ตัวอย่างโปรแกรมแสดงการทำงานของ Stack ตัวอย่างที่ 1 (ต่อ) int Pop() { int r; if (top>-1) { r=stackdata[top]; stackdata[top]=0; top--; return r; } return -1; } Department of Computer Science Data Structure

  22. ตัวอย่างโปรแกรมแสดงการทำงานของ Stack ตัวอย่างที่ 1 (ต่อ) void main() { clrscr(); Push(7); Push(12); Push(23); Push(2); Push(33); Push(10); printf("Pop=%d\n",Pop()); printf("Pop=%d\n",Pop()); printf("Pop=%d\n",Pop()); printf("Pop=%d\n",Pop()); getch(); } Run Pop = 10 Pop = 33 Pop = 2 Pop = 23 Department of Computer Science Data Structure

  23. ตัวอย่างโปรแกรมแสดงการทำงานของ Stack ตัวอย่างที่ 2 (ต่อ) ตั้งชื่อไฟล์ว่า Stack2.c #include <stdio.h> #include <stdlib.h> #include <conio.h> #define MAX 10 int stackdata[MAX]; int top =-1; int Push(int what) { if (top<MAX-1) { top++; stackdata[top] = what; return 1; } return -1; } Department of Computer Science Data Structure

  24. ตัวอย่างโปรแกรมแสดงการทำงานของ Stack ตัวอย่างที่ 2 (ต่อ) int Pop() { int r; if (top>-1) { r=stackdata[top]; stackdata[top]=0; top--; return r; } return -1; } Department of Computer Science Data Structure

  25. ตัวอย่างโปรแกรมแสดงการทำงานของ Stack ตัวอย่างที่ 2 (ต่อ) Run Pop = 10Pop = 33 Pop = 2 Pop = 23 Pop = 12 Pop = 7 Pop = -1 Pop = -1 void main() { clrscr(); Push(7); Push(12); Push(23); Push(2); Push(33); Push(10); for (int i=0;i<8;i++) { printf("Pop=%d\n",Pop()); } getch(); } Department of Computer Science Data Structure

  26. ตัวอย่างโปรแกรมแสดงการทำงานของ Stack ตัวอย่างที่ 3 (ต่อ) ตั้งชื่อไฟล์ว่า Stack3.c #include <stdio.h> #include <stdlib.h> #include <conio.h> #define MAX 10 int stackdata[MAX]; int top =-1; int Push(int what) { if (top<MAX-1) { top++; stackdata[top] = what; return 1; } return -1; } Department of Computer Science Data Structure

  27. ตัวอย่างโปรแกรมแสดงการทำงานของ Stack ตัวอย่างที่ 3 (ต่อ) int Pop() { int r; if (top>-1) { r=stackdata[top]; stackdata[top]=0; top--; return r; } return -1; } Department of Computer Science Data Structure

  28. ตัวอย่างโปรแกรมแสดงการทำงานของ Stack ตัวอย่างที่ 3 (ต่อ) Run Pop = 10Pop = 33 Pop = 2 Pop = 23 Pop = 12 Pop = 7 Stack is empty void main() { clrscr(); Push(7); Push(12); Push(23); Push(2); Push(33); Push(10); for (int i=0;i<50;i++) { int result=Pop(); if (result==-1) { printf("Stack is empty\n"); getch(); break; } printf("Pop=%d\n", result); getch(); } } Department of Computer Science Data Structure

  29. การใช้ สแตค เพื่อแปลรูปนิพจน์ทางคณิตศาสตร์ Data Structure Department of Computer Science

  30. ตัวอย่างนิพจน์คณิตศาสตร์ในรูปแบบต่าง ๆ • นิพจน์ Infix นิพจน์ Postfix นิพจน์ Prefix • A+B-C AB+C- - +ABC • A+B*C-D/E ABC*+DE/- - +A*BC/DE • A*B+C-D/E AB*C+DE/- - +*ABC/DE Data Structure Department of Computer Science

  31. การแปลงนิพจน์ Infix ให้เป็น Postfix • เขียนโปรแกรมสั่งให้เครื่องคำนวณต้องเขียนนิพจน์ที่ต้องการไปในตัวโปรแกรม ซึ่งนิพจน์เหล่านี้เรียกว่า นิพจน์ Infix คือนิพจน์ที่มีโอเปอร์เรเตอร์ (Operator) อยู่ระหว่างโอเปอร์แรนด์ (Operand) ทั้งสอง เช่น A+B เครื่องหมาย + เป็นโอเปอร์เรเตอร์ระหว่างโอเปอร์แรนด์ A และ B ซึ่งเห็นว่าเป็นนิพจน์ที่มนุษย์คุ้นเคย ข้อเสียของนิพจน์ infix ทีททำให้คอมไพเลอร์ยุ่งยาก คือลำดับความสำคัญของโอเปอร์เรเตอร์ (Precedence) ที่ต่างกัน เช่นเครื่องหมายยกกำลัง (ใช้ ^ ในการเขียนโปรแกรม) มีความสำคัญมากกว่าเครื่องหมายคูณ และหาร และเครื่องหมายคูณและหารมีความสำคัญมากกว่าเครื่องหมายบวกและลบ Data Structure Department of Computer Science

  32. การแปลงนิพจน์ Infix ให้เป็น Postfix • เมื่อการประมวลนิพจน์ infix เป็นไปด้วยความยากที่การคำนวณไม่เป็นไปตามลำดับของเครื่องหมายโอเปอร์เรเตอร์ที่มีก่อนหลัง คอมไพเลอร์จึงแปลงนิพจน์ infix ให้เป็น postfix เสียก่อน • นิพจน์ Postfix ก็คือนิพจน์ที่มีโอเปอเรเตอร์อยู่หลังโอเปอร์แรนด์ทั้งสองของมัน เช่น AB+ หมายถึง A+B AB- หมายถึง A-B AB* หมายถึง A*B Data Structure Department of Computer Science

  33. การแปลงนิพจน์ Infix ให้เป็น Postfix • Operator คือเครื่องหมายกระทำ + - * / ^ • Operand คือตัวแปรต่าง ๆ A,B,C,D,E เช่น A+B*C,(A*B)-C Data Structure Department of Computer Science

  34. ลำดับความสำคัญ Operator 1. เครื่องหมายยกกำลัง ^ 2. เครื่องหมายคูณกับหาร *,/ 3. เครื่องหมายบวกกับลบ +,- 4. เครื่องหมายวงเล็บ () กระทำก่อนเช่น A+(B*C) 5. เครื่องหมายระดับเดียวกันไม่มีวงเล็บให้ทำจากซ้ายไปขวา เช่น A+B+C Data Structure Department of Computer Science

  35. ตัวอย่างนิพจน์ทางคณิตศาสตร์และลำดับการคำนวณตัวอย่างนิพจน์ทางคณิตศาสตร์และลำดับการคำนวณ Department of Computer Science Data Structure

  36. อัลกอริทึมการแปลงนิพจน์ Infix เป็น นิพจน์ Postfix • เราสามารถแปลงนิพจน์ Infix ให้เป็น Postfix ได้โดยอาศัยสแตคที่มีคุณสมบัติการเข้าหลังออกก่อนหรือ LIFO โดยมีอัลกอริทึมในการแปลงนิพจน์ ดังนี้1. ถ้าข้อมูลเข้า (input) เป็นตัวถูกดำเนินการ (operand) ให้นำออกไปเป็นผลลัพธ์ (output)2. ถ้าข้อมูลเข้าเป็นตัวดำเนินการ (operator) ให้ดำเนินการดังนี้2.1 ถ้าสแตคว่าง ให้ push operator ลงในสแตค Data Structure Department of Computer Science

  37. อัลกอริทึมการแปลงนิพจน์ Infix เป็น นิพจน์ Postfix • 2.2 ถ้าสแตคไม่ว่าง ให้เปรียบเทียบ operator ที่เข้ามากับ operator ที่อยู่ในตำแหน่ง TOP ของสแตค2.2.1 ถ้า operator ที่เข้ามามีความสำคัญมากกว่า operator ที่ตำแหน่ง TOP ของสแตคให้ push ลงสแตค2.2.2 ถ้า operator ที่เข้ามามีความสำคัญน้อยกว่าหรือเท่ากับ operator ที่อยู่ในตำแหน่ง TOP ของสแตค ให้ pop สแตคออกไปเป็นผลลัพธ์ แล้วทำการเปรียบเทียบ operator ที่เข้ามากับ operator ที่ตำแหน่ง TOP ต่อไป จะหยุดจนกว่า operator ที่เข้ามาจะมีความสำคัญมากกว่า operator ที่ตำแหน่ง TOP ของสแตค แล้วจึง push operator ที่เข้ามานั้นลงสแตค

  38. อัลกอริทึมการแปลงนิพจน์ Infix เป็น นิพจน์ Postfix • 3. ถ้าข้อมูลเข้าเป็นวงเล็บเปิด ให้ push ลงสแตค4. ถ้าข้อมูลเข้าเป็นวงเล็บปิด ให้ pop ข้อมูลออกจากสแตคไปเป็นผลลัพธ์จนกว่าจะถึงวงเล็บ เปิด จากนั้นทิ้งวงเล็บเปิดและปิดทิ้งไป5. ถ้าข้อมูลเข้าหมด ให้ pop ข้อมูลออกจากสแตคไปเป็นผลลัพธ์จนกว่าสแตคจะว่าง Data Structure Department of Computer Science

  39. ตัวอย่างการแปลงนิพจน์ Infix เป็นนิพจน์ Postfix • นิพจน์ A + B * C

  40. นิพจน์ (A * B) + (C / D)

  41. นิพจน์ A / B + (C – D)

  42. นิพจน์ (A + B) * ( C ^ D – E ) * F

  43. ขั้นตอนการคำนวณจากนิพจน์ Postfix ในการคำนวณค่า Postfix ที่แปลงมาแล้ว ตัวแปลภาษาจะทำ การคำนวณโดยใช้โครงสร้างสแตกช่วยอีกเช่นกัน ขั้นตอนในการคำนวณ 1. อ่านตัวอักษรในนิพจน์ Postfix จากซ้ายไปขวาทีละ ตัวอักษร 2. ถ้าเป็นตัวถูกดำเนินการ ให้ทำการ push ตัวถูกดำเนินการ นั้นลงในสแตก แล้วกลับไปอ่านอักษรตัวใหม่เข้ามา 3. ถ้าเป็นตัวดำเนินการ ให้ทำการ pop ค่าจากสแตก 2 ค่า โดย ตัวแรกเป็นตัวถูกดำเนินการตัวที่ 2 และตัวที่ 1 ตามลำดับ Department of Computer Science Data Structure

  44. ขั้นตอนการคำนวณจากนิพจน์ Postfix 4. ทำการคำนวณ ตัวถูกดำเนินการตัวที่ 1 ด้วยตัวถูก ดำเนินการตัวที่ 2 โดยใช้ตัวดำเนินการในข้อ 3 5. ทำการ push ผลลัพธ์ที่ได้จากการคำนวณในข้อ 4 ลงสแตก 6. ถ้าตัวอักษรในนิพจน์ Postfix ยังอ่านไม่หมดให้กลับไปทำ ข้อ 1 ใหม่ Data Structure Department of Computer Science

  45. ขั้นตอนการคำนวณจากนิพจน์ Postfix ตัวอย่าง ขั้นตอนการคำนวณจากนิพจน์ Postfix ABC+D-*E/ 2. ABC+D-*E/ 1. ABC+D-*E/ Push B Push A B A A Data Structure Department of Computer Science

  46. ขั้นตอนการคำนวณจากนิพจน์ Postfix 4. ABC+D-*E/ 3. ABC+D-*E/ Push C 1. Pop C 2. Pop B 3. B+C 4. Push B+C C B+C B A A Data Structure Department of Computer Science

  47. ขั้นตอนการคำนวณจากนิพจน์ Postfix 6. ABC+D-*E/ 5. ABC+D-*E/ Push D 1. Pop D 2. Pop B+C 3. B+C-D 4. Push B+C-D D B+C-D B+C A A Data Structure Department of Computer Science

  48. ขั้นตอนการคำนวณจากนิพจน์ Postfix 8. ABC+D-*E/ 7. ABC+D-*E/ 1. Pop B+C-D 2. Pop A 3. A*B+C-D 4. Push A*B+C-D Push E E A*B+C-D A*B+C-D Data Structure Department of Computer Science

  49. ขั้นตอนการคำนวณจากนิพจน์ Postfix 9. ABC+D-*E/ 1. Pop E 2. Pop A*B+C-D 3. A*B+C-D/E 4. Push A*B+C-D/E A*B+C-D/E 10. ค่าสุดท้ายที่อยู่ในสแตกคือคำตอบที่ต้องการ Data Structure Department of Computer Science

  50. Data Structure Department of Computer Science

More Related