7 stack and vector
TRANSCRIPT
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
Introductionmfarra.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
Introductionmfarra.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.
Introductionmfarra.cst.ps www.fb.com/MahmoudRFarra
For the many applications that do not require synchronization, using ArrayList is more efficient than using Vector.
Vector Classmfarra.cst.ps www.fb.com/MahmoudRFarra
Stack Classmfarra.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 Listmfarra.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 Employeesmfarra.cst.ps www.fb.com/MahmoudRFarra
EmployeeClass
EmpStackClass
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 Classmfarra.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 Classmfarra.cst.ps www.fb.com/MahmoudRFarra
1. class EmpStack2. {3. Employee Top = null;4. int length =0;5. //the operation of stack will be inserted here 6. }
Push Operationmfarra.cst.ps www.fb.com/MahmoudRFarra
Top
6
Top
6
1
Top
6
1
7
POP Operationmfarra.cst.ps www.fb.com/MahmoudRFarra
Top
6
1
7 Top
6
1
7
Create object of EmpStackmfarra.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 stackmfarra.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 stackmfarra.cst.ps www.fb.com/MahmoudRFarra
1. public void Pop()2. {3. if (Top == null)4. // print "Stack is Empty!!"5. else6. {7. Top = Top.next;8. length--; 9. }10. 11. }
Stack using Array Listmfarra.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 Expressionsmfarra.cst.ps www.fb.com/MahmoudRFarra
Stacks can be used to evaluate expressions.
How does Google evaluate an expression?
Case Study: Evaluating Expressionsmfarra.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 Expressionsmfarra.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 Expressionsmfarra.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 Expressionsmfarra.cst.ps www.fb.com/MahmoudRFarra
using Java
2015
FB: M a h m o u d R F a r r aYouTube: M a h m o u d R F a r SlidesShare: mralfarra
Thank you