DATA STRUCTURE Presented By: Mahmoud Rafeek Alfarra Using C# MINISTRY OF EDUCATION & HIGHER EDUCATION COLLEGE OF SCIENCE AND TECHNOLOGY KHANYOUNIS- PALESTINE Information Technology, 3’rd Semester Lecture 10: Operations of Stack
Outline Implementing the Push Implementing the Pop Implementing the Peek Implementing the Clear Implementing the Printall Emank X Mezank !!
Implementing the Push operation New nodes should be inserted at the front of the list, so that they become the top of the stack. 3 Presented & Prepared by: Mahmoud R. Alfarra Top
Pseudo code of the Push operation 3 Presented & Prepared by: Mahmoud R. Alfarra Create new object If top pointer points to null top = new new.next = null Else set the new.next = top top = new size++ index++
Implementing the Pop operation The removed element should be removed from the back (Top) of the list. 3 Presented & Prepared by: Mahmoud R. Alfarra Top
Pseudo code of the Pop operation 3 Presented & Prepared by: Mahmoud R. Alfarra If top pointer points to null print (the stack is empty) Else top = top.next top = new Size-- Index--
Implementing the Peek operation The retrieved element should be element of the back (Top) of the list. 3 Presented & Prepared by: Mahmoud R. Alfarra Top 617 Top.name Top.ID Top.data…
Pseudo code of the Peek operation 3 Presented & Prepared by: Mahmoud R. Alfarra If top pointer points to null print (the stack is empty) Else return top
Implementing the Clear operation The clearing of stack means removing all the elements, which should be done set the top points to null. 3 Presented & Prepared by: Mahmoud R. Alfarra Top 617
Pseudo code of the clear operation 3 Presented & Prepared by: Mahmoud R. Alfarra If top pointer points to null print (the stack is empty) Else Top = null; index = -1; size = 0;
Implementing the Printall operation The Print of all elements' data of the stack means that we have to create a new pointer points every time to the next element 3 Presented & Prepared by: Mahmoud R. Alfarra Top 617
Pseudo code of the Printall operation 3 Presented & Prepared by: Mahmoud R. Alfarra If top pointer points to null print (the stack is empty) Else create current pointer points to top for (current = top; current!= null ; current = current.next) print current.data Depending on your understanding of the printAll() operation, write a find method which search about a specific data. HW 10.1 Depending on your understanding of the printAll() operation, write a sort method which sorts the elements of the stack based on the name field. HW 10.2
Function of the PrintAll operation 3 Presented & Prepared by: Mahmoud R. Alfarra public void PrintAll() { Student current; string all = "name\tage\n"; if (Top == null) Console.WriteLine("The Stack is empty!!"); else { for (current = Top; current != null; current = current.next) all = all + current.name + "\t" + current.age + "\n"; } Console.WriteLine(all); }
Emank X Mezank !! أعظم أسبـاب النجاة اتقِ الله
Next Lecture Queue