DATA STRUCTURE Chapter 6: Stack Prepared & Presented by Mr. Mahmoud R. Alfarra 2011-2012 College of Science & Technology Dep. Of Computer Science & IT BCs of Information Technology http://mfarra.cst.ps
Out Line  What is Stack data structure?  Push operation  Pop operation  Retrieve the data of an element  Clear the Stack  Print all data of stack  Search about data  Stack Collection 2
What is the Stack?  Stack is a dynamic linear data structure.  Data in a stack are added and removed from only one end of the list. 3
What is the Stack? 4 ‫البيانات‬ ‫تراكيب‬ ‫مساق‬ ‫إعداد‬ ‫العلمية‬ ‫المادة‬ / ‫أ‬ . ‫ا‬ َّ‫الفــر‬ ‫رفيق‬ ‫محمود‬  We define a stack as a list of items that are accessible only from the end of the list, which is called the top of the stack.  Elements are always removed from the top, and inserted on the top also.  A stack is known as a Last-in, First-out (LIFO) data structure
Elements of stack 5 Top 6 1 7
Employee’s element for stack 6 ‫البيانات‬ ‫تراكيب‬ ‫مساق‬ ‫إعداد‬ ‫العلمية‬ ‫المادة‬ / ‫أ‬ . ‫ا‬ َّ‫الفــر‬ ‫رفيق‬ ‫محمود‬ 1. class Employee { 2. public int salary; 3. public String name; 4. Employee next; 5. public Employee() 6. { 7. salary = 300; 8. name = "no name"; 9. next = null; 10. } 11. public Employee(int salary, String name) 12. { 13. this.salary = salary; 14. this.name = name; } }
Stack of employees 7 1. class EmployeeStack 2. { 3. Employee Top = null; 4. int length =0; 5. //the operation of stack will be inserted here 6. }
Push operation 8 Top 6 Top 6 1 Top 6 1 7
Push operation 9 ‫البيانات‬ ‫تراكيب‬ ‫مساق‬ ‫إعداد‬ ‫العلمية‬ ‫المادة‬ / ‫أ‬ . ‫ا‬ َّ‫الفــر‬ ‫رفيق‬ ‫محمود‬ 1. public void Push(Employee NewEmp) { 2. Employee newe = NewEmp; 3. if (Top == null) { 4. Top = newe; 5. newe.next = null; } 6. else { 7. newe.next = Top; 8. Top = newe; } 9. length++; 10. Console.WriteLine("A new Employee has been added to Stack: "+length); 11. }
Pop operation 10 Top 6 1 7 Top 6 1 7
Pop operation 11 1. public void Pop() 2. { 3. if (Top == null) 4. Console.WriteLine("Stack is Empty!!"); 5. else 6. { 7. Top = Top.next; 8. length--; 9. Console.WriteLine("Now Top Points to: " + Top.name); 10. } 11. 12. }
Peek operation: to display info of top element 12 Top 6 1 7 Top.name Top.salary …
Peek operation: to display info of top element 13 1. public void Peek() 2. { 3. if (Top == null) 4. Console.WriteLine("The Stack is Empty!!"); 5. else 6. Console.WriteLine("The Employee Data:n"+ 7. "Name: "+Top.name+"nSalary: "+Top.salary); 8. }
Clear the Stack 14  Clearing the stack means that the top must points to null. Top 6 1 7
Clear the Stack 15 1. public void Clear() 2. { 3. if (Top == null) 4. Console.WriteLine("The Stack is Empty!!"); 5. else 6. Top = null; 7. Length = 0; 8. }
Print all data of stack 16 Top 6 1 7 next next current
Print all data of stack 17 1. public void PrintAll() 2. { 3. Employee current = Top; 4. int x =1; 5. while (current != null) 6. { 7. Console.WriteLine("Data Of Employee # "+x+" isn "+ "Name: “ + current.name + "nSalary: "+current.salary); 8. Console.WriteLine("=============================="); 9. x++; 10. current = current.next; 11. } 12. }
Search about data 18 Top 6 1 7 next next current
Search about data 19 ‫البيانات‬ ‫تراكيب‬ ‫مساق‬ ‫إعداد‬ ‫العلمية‬ ‫المادة‬ / ‫أ‬ . ‫ا‬ َّ‫الفــر‬ ‫رفيق‬ ‫محمود‬ 1. public void SearchByName(String name ) 2. { 3. Employee current = Top; 4. bool flag = false; 5. while ( current != null) 6. { 7. if (name.CompareTo(current.name) == 0) 8. { 9. flag = true; 10. break; 11. } 12. current = current.next; 13. } 14. if (flag == true) 15. Console.WriteLine("Exist!!"); 16. else 17. Console.WriteLine("Does not Exist!!"); 18. }
Stack Collection  The Stack class is an implementation of the ICollection interface that represents a LIFO collection, or a stack.  The class is implemented in the .NET Framework as a circular buffer, which enables space for items pushed on the stack to be allocated dynamically.  The default constructor is called as follows: Stack myStack = new Stack(); Practice: develop a full application using stack class
Thank You … 21 Remember that: question is the key of knowledge
Ahl Eljanna   َّ‫ت‬ُ ‫جم‬‫ل‬‫ا‬ َ ‫د‬ِ ‫ع‬ُ ‫و‬ ِ ‫ِت‬َّ‫ل‬‫ا‬ ِ ‫َّة‬‫ن‬َ‫ج‬ ‫ْل‬‫ا‬ ُ ‫ل‬َ‫ث‬َ ‫م‬ َ ‫م‬ ‫ج‬ ‫ن‬ِ ‫م‬ ٌ ‫ار‬َ ‫ه‬‫ج‬‫َن‬‫أ‬ ‫ا‬َ ‫يه‬ِ‫ف‬ َ ‫ن‬‫و‬ُ ‫ق‬ ٍ ‫ن‬ ِ ِ‫س‬ ِ‫ج‬ ‫ر‬َ‫غ‬ ٍ ‫اء‬ ‫ج‬ َّ‫ي‬َ َ‫ت‬َ ‫ج‬َ ‫ي‬ ٍَ َ‫ل‬ ‫ج‬ ‫ن‬ ِ ‫م‬ ٌ ‫ار‬ َ ‫ه‬‫ج‬‫َن‬‫أ‬َ ‫و‬ ‫ج‬َ ‫ج‬ ‫ن‬ ِ ‫م‬ ٌ ‫ار‬ َ ‫ه‬‫ج‬‫َن‬‫أ‬َ ‫و‬ َُ ُ ‫م‬‫ج‬‫ع‬َ‫ط‬ ٍَّ َ‫ل‬ ٍ ُ ‫م‬ ٍ ‫ل‬ َ َ ‫ع‬ ‫ج‬ ‫ن‬ ِ ‫م‬ ٌ ‫ار‬ َ ‫ه‬‫ج‬‫َن‬‫أ‬َ ‫و‬ َ ‫و‬َِِ ‫ر‬‫ا‬ َّ ‫لش‬ِ‫ل‬ ِ ‫م‬ ‫ا‬ َ ‫يه‬ِ‫ف‬ ‫ج‬ ‫ه‬َُ ‫م‬َ ‫و‬ َ‫و‬ َ ‫ص‬ ِ ‫ل‬ ُ ‫ك‬ ‫ج‬ ‫ن‬ ‫ج‬ ‫ه‬ِِ ‫ِب‬َ ‫ر‬ ‫ج‬ ‫ن‬ِ ‫م‬ ٌَ ِ‫ج‬ َ ‫م‬َ ‫و‬ ِ ‫ات‬ََ ‫َّم‬‫ث‬‫ال‬ 22

Chapter 6: stack data structure

  • 1.
    DATA STRUCTURE Chapter 6:Stack Prepared & Presented by Mr. Mahmoud R. Alfarra 2011-2012 College of Science & Technology Dep. Of Computer Science & IT BCs of Information Technology http://mfarra.cst.ps
  • 2.
    Out Line  Whatis Stack data structure?  Push operation  Pop operation  Retrieve the data of an element  Clear the Stack  Print all data of stack  Search about data  Stack Collection 2
  • 3.
    What is theStack?  Stack is a dynamic linear data structure.  Data in a stack are added and removed from only one end of the list. 3
  • 4.
    What is theStack? 4 ‫البيانات‬ ‫تراكيب‬ ‫مساق‬ ‫إعداد‬ ‫العلمية‬ ‫المادة‬ / ‫أ‬ . ‫ا‬ َّ‫الفــر‬ ‫رفيق‬ ‫محمود‬  We define a stack as a list of items that are accessible only from the end of the list, which is called the top of the stack.  Elements are always removed from the top, and inserted on the top also.  A stack is known as a Last-in, First-out (LIFO) data structure
  • 5.
  • 6.
    Employee’s element forstack 6 ‫البيانات‬ ‫تراكيب‬ ‫مساق‬ ‫إعداد‬ ‫العلمية‬ ‫المادة‬ / ‫أ‬ . ‫ا‬ َّ‫الفــر‬ ‫رفيق‬ ‫محمود‬ 1. class Employee { 2. public int salary; 3. public String name; 4. Employee next; 5. public Employee() 6. { 7. salary = 300; 8. name = "no name"; 9. next = null; 10. } 11. public Employee(int salary, String name) 12. { 13. this.salary = salary; 14. this.name = name; } }
  • 7.
    Stack of employees 7 1.class EmployeeStack 2. { 3. Employee Top = null; 4. int length =0; 5. //the operation of stack will be inserted here 6. }
  • 8.
  • 9.
    Push operation 9 ‫البيانات‬ ‫تراكيب‬‫مساق‬ ‫إعداد‬ ‫العلمية‬ ‫المادة‬ / ‫أ‬ . ‫ا‬ َّ‫الفــر‬ ‫رفيق‬ ‫محمود‬ 1. public void Push(Employee NewEmp) { 2. Employee newe = NewEmp; 3. if (Top == null) { 4. Top = newe; 5. newe.next = null; } 6. else { 7. newe.next = Top; 8. Top = newe; } 9. length++; 10. Console.WriteLine("A new Employee has been added to Stack: "+length); 11. }
  • 10.
  • 11.
    Pop operation 11 1. publicvoid Pop() 2. { 3. if (Top == null) 4. Console.WriteLine("Stack is Empty!!"); 5. else 6. { 7. Top = Top.next; 8. length--; 9. Console.WriteLine("Now Top Points to: " + Top.name); 10. } 11. 12. }
  • 12.
    Peek operation: todisplay info of top element 12 Top 6 1 7 Top.name Top.salary …
  • 13.
    Peek operation: todisplay info of top element 13 1. public void Peek() 2. { 3. if (Top == null) 4. Console.WriteLine("The Stack is Empty!!"); 5. else 6. Console.WriteLine("The Employee Data:n"+ 7. "Name: "+Top.name+"nSalary: "+Top.salary); 8. }
  • 14.
    Clear the Stack 14 Clearing the stack means that the top must points to null. Top 6 1 7
  • 15.
    Clear the Stack 15 1.public void Clear() 2. { 3. if (Top == null) 4. Console.WriteLine("The Stack is Empty!!"); 5. else 6. Top = null; 7. Length = 0; 8. }
  • 16.
    Print all dataof stack 16 Top 6 1 7 next next current
  • 17.
    Print all dataof stack 17 1. public void PrintAll() 2. { 3. Employee current = Top; 4. int x =1; 5. while (current != null) 6. { 7. Console.WriteLine("Data Of Employee # "+x+" isn "+ "Name: “ + current.name + "nSalary: "+current.salary); 8. Console.WriteLine("=============================="); 9. x++; 10. current = current.next; 11. } 12. }
  • 18.
  • 19.
    Search about data 19 ‫البيانات‬‫تراكيب‬ ‫مساق‬ ‫إعداد‬ ‫العلمية‬ ‫المادة‬ / ‫أ‬ . ‫ا‬ َّ‫الفــر‬ ‫رفيق‬ ‫محمود‬ 1. public void SearchByName(String name ) 2. { 3. Employee current = Top; 4. bool flag = false; 5. while ( current != null) 6. { 7. if (name.CompareTo(current.name) == 0) 8. { 9. flag = true; 10. break; 11. } 12. current = current.next; 13. } 14. if (flag == true) 15. Console.WriteLine("Exist!!"); 16. else 17. Console.WriteLine("Does not Exist!!"); 18. }
  • 20.
    Stack Collection  TheStack class is an implementation of the ICollection interface that represents a LIFO collection, or a stack.  The class is implemented in the .NET Framework as a circular buffer, which enables space for items pushed on the stack to be allocated dynamically.  The default constructor is called as follows: Stack myStack = new Stack(); Practice: develop a full application using stack class
  • 21.
    Thank You … 21 Rememberthat: question is the key of knowledge
  • 22.
    Ahl Eljanna   َّ‫ت‬ُ ‫جم‬‫ل‬‫ا‬َ ‫د‬ِ ‫ع‬ُ ‫و‬ ِ ‫ِت‬َّ‫ل‬‫ا‬ ِ ‫َّة‬‫ن‬َ‫ج‬ ‫ْل‬‫ا‬ ُ ‫ل‬َ‫ث‬َ ‫م‬ َ ‫م‬ ‫ج‬ ‫ن‬ِ ‫م‬ ٌ ‫ار‬َ ‫ه‬‫ج‬‫َن‬‫أ‬ ‫ا‬َ ‫يه‬ِ‫ف‬ َ ‫ن‬‫و‬ُ ‫ق‬ ٍ ‫ن‬ ِ ِ‫س‬ ِ‫ج‬ ‫ر‬َ‫غ‬ ٍ ‫اء‬ ‫ج‬ َّ‫ي‬َ َ‫ت‬َ ‫ج‬َ ‫ي‬ ٍَ َ‫ل‬ ‫ج‬ ‫ن‬ ِ ‫م‬ ٌ ‫ار‬ َ ‫ه‬‫ج‬‫َن‬‫أ‬َ ‫و‬ ‫ج‬َ ‫ج‬ ‫ن‬ ِ ‫م‬ ٌ ‫ار‬ َ ‫ه‬‫ج‬‫َن‬‫أ‬َ ‫و‬ َُ ُ ‫م‬‫ج‬‫ع‬َ‫ط‬ ٍَّ َ‫ل‬ ٍ ُ ‫م‬ ٍ ‫ل‬ َ َ ‫ع‬ ‫ج‬ ‫ن‬ ِ ‫م‬ ٌ ‫ار‬ َ ‫ه‬‫ج‬‫َن‬‫أ‬َ ‫و‬ َ ‫و‬َِِ ‫ر‬‫ا‬ َّ ‫لش‬ِ‫ل‬ ِ ‫م‬ ‫ا‬ َ ‫يه‬ِ‫ف‬ ‫ج‬ ‫ه‬َُ ‫م‬َ ‫و‬ َ‫و‬ َ ‫ص‬ ِ ‫ل‬ ُ ‫ك‬ ‫ج‬ ‫ن‬ ‫ج‬ ‫ه‬ِِ ‫ِب‬َ ‫ر‬ ‫ج‬ ‫ن‬ِ ‫م‬ ٌَ ِ‫ج‬ َ ‫م‬َ ‫و‬ ِ ‫ات‬ََ ‫َّم‬‫ث‬‫ال‬ 22