using Java
2015
Data Structure
Prepared by: Mahmoud Rafeek Al-farra
in Java
7. Stack and Vector
mfarra.cst.ps www.fb.com/MahmoudRFarra
Contents
Implementation Stack using Linked List
Vector and Stack Class
Introduction
Case Study: Evaluating Expressions
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
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.
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.
Vector Class
mfarra.cst.ps www.fb.com/MahmoudRFarra
Stack Class
mfarra.cst.ps www.fb.com/MahmoudRFarra
 The Stack class extends Vector to provide a last-in,
first-out (LIFO) data structure
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).
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
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]
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. }
Push Operation
mfarra.cst.ps www.fb.com/MahmoudRFarra
Top
6
Top
6
1
Top
6
1
7
POP Operation
mfarra.cst.ps www.fb.com/MahmoudRFarra
Top
6
1
7 Top
6
1
7
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.
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. }
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. }
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]
Case Study: Evaluating Expressions
mfarra.cst.ps www.fb.com/MahmoudRFarra
 Stacks can be used to evaluate expressions.
 How does Google evaluate an expression?
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.
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.
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]
Case Study: Evaluating Expressions
mfarra.cst.ps www.fb.com/MahmoudRFarra
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

7 stack and vector

  • 1.
    using Java 2015 Data Structure Preparedby: Mahmoud Rafeek Al-farra in Java 7. Stack and Vector
  • 2.
    mfarra.cst.ps www.fb.com/MahmoudRFarra Contents Implementation Stackusing Linked List Vector and Stack Class Introduction Case Study: Evaluating Expressions
  • 3.
    Introduction mfarra.cst.ps www.fb.com/MahmoudRFarra  Vectoris 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.
    Introduction mfarra.cst.ps www.fb.com/MahmoudRFarra  Vectoris 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.
    Introduction mfarra.cst.ps www.fb.com/MahmoudRFarra  Forthe many applications that do not require synchronization, using ArrayList is more efficient than using Vector.
  • 6.
  • 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.
    Implementation of Stackas 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.
    Application: Stack ofEmployees 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.
    The self-referential Class mfarra.cst.pswww.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.
    The EmpStack Class mfarra.cst.pswww.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.
  • 13.
  • 14.
    Create object ofEmpStack 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.
    Add new nodeto 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.
    Delete a nodefrom 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.
    Stack using ArrayList 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.
    Case Study: EvaluatingExpressions mfarra.cst.ps www.fb.com/MahmoudRFarra  Stacks can be used to evaluate expressions.  How does Google evaluate an expression?
  • 19.
    Case Study: EvaluatingExpressions 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.
    Case Study: EvaluatingExpressions 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.
    Case Study: EvaluatingExpressions 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.
    Case Study: EvaluatingExpressions mfarra.cst.ps www.fb.com/MahmoudRFarra
  • 23.
    using Java 2015 FB: Ma 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