23
using Java 2015 Data Structure Prepared by: Mahmoud Rafeek Al-farra in Java 7. Stack and Vector

7 stack and vector

Embed Size (px)

Citation preview

Page 1: 7 stack and vector

using Java

2015

Data Structure Prepared by: Mahmoud Rafeek Al-farra

in Java

7. Stack and Vector

Page 2: 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

Page 3: 7 stack and vector

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

Page 4: 7 stack and vector

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.

Page 5: 7 stack and vector

Introductionmfarra.cst.ps www.fb.com/MahmoudRFarra

For the many applications that do not require synchronization, using ArrayList is more efficient than using Vector.

Page 6: 7 stack and vector

Vector Classmfarra.cst.ps www.fb.com/MahmoudRFarra

Page 7: 7 stack and vector

Stack Classmfarra.cst.ps www.fb.com/MahmoudRFarra

The Stack class extends Vector to provide a last-in,

first-out (LIFO) data structure

Page 8: 7 stack and vector

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).

Page 9: 7 stack and vector

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

Page 10: 7 stack and vector

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]

Page 11: 7 stack and vector

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

Page 12: 7 stack and vector

Push Operationmfarra.cst.ps www.fb.com/MahmoudRFarra

Top

6

Top

6

1

Top

6

1

7

Page 13: 7 stack and vector

POP Operationmfarra.cst.ps www.fb.com/MahmoudRFarra

Top

6

1

7 Top

6

1

7

Page 14: 7 stack and vector

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.

Page 15: 7 stack and vector

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

Page 16: 7 stack and vector

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

Page 17: 7 stack and vector

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]

Page 18: 7 stack and vector

Case Study: Evaluating Expressionsmfarra.cst.ps www.fb.com/MahmoudRFarra

Stacks can be used to evaluate expressions.

How does Google evaluate an expression?

Page 19: 7 stack and vector

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.

Page 20: 7 stack and vector

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.

Page 21: 7 stack and vector

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]

Page 22: 7 stack and vector

Case Study: Evaluating Expressionsmfarra.cst.ps www.fb.com/MahmoudRFarra

Page 23: 7 stack and vector

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