Diese Präsentation wurde erfolgreich gemeldet.
Wir verwenden Ihre LinkedIn Profilangaben und Informationen zu Ihren Aktivitäten, um Anzeigen zu personalisieren und Ihnen relevantere Inhalte anzuzeigen. Sie können Ihre Anzeigeneinstellungen jederzeit ändern.

3

Teilen

Herunterladen, um offline zu lesen

7 stack and vector

Herunterladen, um offline zu lesen

محاضرة جديدة

Ähnliche Bücher

Kostenlos mit einer 30-tägigen Testversion von Scribd

Alle anzeigen

Ähnliche Hörbücher

Kostenlos mit einer 30-tägigen Testversion von Scribd

Alle anzeigen

7 stack and vector

  1. 1. using Java 2015 Data Structure Prepared by: Mahmoud Rafeek Al-farra in Java 7. Stack and Vector
  2. 2. mfarra.cst.ps www.fb.com/MahmoudRFarra Contents Implementation Stack using Linked List Vector and Stack Class Introduction Case Study: Evaluating Expressions
  3. 3. Introduction mfarra.cst.ps www.fb.com/MahmoudRFarra  Vector is a subclass of AbstractList, and Stack is a subclass of Vector in the Java API.  Several data structures were supported earlier, among them the Vector and Stack classes. For an interactive demo on how stacks work, go to www.cs.armstrong.edu/liang/animation/web/Stack.html
  4. 4. Introduction mfarra.cst.ps www.fb.com/MahmoudRFarra  Vector is the same as ArrayList, except that it contains synchronized methods for accessing and modifying the vector.  Synchronized methods can prevent data corruption when a vector is accessed and modified by two or more threads concurrently.
  5. 5. Introduction mfarra.cst.ps www.fb.com/MahmoudRFarra  For the many applications that do not require synchronization, using ArrayList is more efficient than using Vector.
  6. 6. Vector Class mfarra.cst.ps www.fb.com/MahmoudRFarra
  7. 7. Stack Class mfarra.cst.ps www.fb.com/MahmoudRFarra  The Stack class extends Vector to provide a last-in, first-out (LIFO) data structure
  8. 8. Implementation of Stack as Linked List mfarra.cst.ps www.fb.com/MahmoudRFarra  A stack can be viewed as a special type of list whose elements are accessed, inserted, and deleted only from the end (top).
  9. 9. Application: Stack of Employees mfarra.cst.ps www.fb.com/MahmoudRFarra Employee Class EmpStack Class EmpSystem Class In this class we will create an object of EmpStack class, and then manipulates the list using all operations. The class of linked list members and operation, this class simulate the list of any thing, the object of this class will be created and used in the class of EmpSystem. A self referential class called Employee, contains the attributes and behavior of any Employee. 1 2 3
  10. 10. The self-referential Class mfarra.cst.ps www.fb.com/MahmoudRFarra 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; } } Self Study: To convert it to generic class, go to page 907 in our text book. [ind, N.W]
  11. 11. The EmpStack Class mfarra.cst.ps www.fb.com/MahmoudRFarra 1. class EmpStack 2. { 3. Employee Top = null; 4. int length =0; 5. //the operation of stack will be inserted here 6. }
  12. 12. Push Operation mfarra.cst.ps www.fb.com/MahmoudRFarra Top 6 Top 6 1 Top 6 1 7
  13. 13. POP Operation mfarra.cst.ps www.fb.com/MahmoudRFarra Top 6 1 7 Top 6 1 7
  14. 14. Create object of EmpStack mfarra.cst.ps www.fb.com/MahmoudRFarra 1. static void Main(string[] args) 2. { 3. EmpStack stack1 = new EmpStack(); 4. }  This objet of list will be manipulated using the operations.
  15. 15. Add new node to stack mfarra.cst.ps www.fb.com/MahmoudRFarra 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. }
  16. 16. Delete a node from stack mfarra.cst.ps www.fb.com/MahmoudRFarra 1. public void Pop() 2. { 3. if (Top == null) 4. // print "Stack is Empty!!" 5. else 6. { 7. Top = Top.next; 8. length--; 9. } 10. 11. }
  17. 17. Stack using Array List mfarra.cst.ps www.fb.com/MahmoudRFarra  Since the insertion and deletion operations on a stack are made only at the end of the stack, it is more efficient to implement a stack with an array list than a linked list. Self Study: Develop our car Project to based on array list.[P. 921] [GW, N.W]
  18. 18. Case Study: Evaluating Expressions mfarra.cst.ps www.fb.com/MahmoudRFarra  Stacks can be used to evaluate expressions.  How does Google evaluate an expression?
  19. 19. Case Study: Evaluating Expressions mfarra.cst.ps www.fb.com/MahmoudRFarra  The problem can be solved using two stacks named: 1. operandStack 2. operatorStack  Operands and operators are pushed into the stacks before they are processed.
  20. 20. Case Study: Evaluating Expressions mfarra.cst.ps www.fb.com/MahmoudRFarra  When an operator is processed, it is popped from operatorStack and applied to the first two operands from operandStack (the two operands are popped from operandStack).  The resultant value is pushed back to operandStack.
  21. 21. Case Study: Evaluating Expressions mfarra.cst.ps www.fb.com/MahmoudRFarra  The algorithm proceeds in two phases: 1. Phase 1: Scanning the expression 2. Phase 2: Clearing the stack Self Study: Develop a Project to calculate the expressions.[P. 788] [Indi, N.W]
  22. 22. Case Study: Evaluating Expressions mfarra.cst.ps www.fb.com/MahmoudRFarra
  23. 23. using Java 2015 FB: M a h m o u d R F a r r a YouTube: M a h m o u d R F a r SlidesShare: mralfarra Thank you
  • khaledhammoud5

    Dec. 4, 2017
  • BndrAlmjlad

    May. 16, 2016
  • MohammedAlbayouk

    Nov. 30, 2015

محاضرة جديدة

Aufrufe

Aufrufe insgesamt

463

Auf Slideshare

0

Aus Einbettungen

0

Anzahl der Einbettungen

3

Befehle

Downloads

46

Geteilt

0

Kommentare

0

Likes

3

×