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. 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.
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. 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. 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. 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. }
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. 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. 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. 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. 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. 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. 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. 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]