SlideShare une entreprise Scribd logo
1  sur  23
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

Contenu connexe

Tendances

Data structure and its types
Data structure and its typesData structure and its types
Data structure and its types
Navtar Sidhu Brar
 
Data structures and algorithms lab4
Data structures and algorithms lab4Data structures and algorithms lab4
Data structures and algorithms lab4
Bianca Teşilă
 

Tendances (20)

Chapter 4: basic search algorithms data structure
Chapter 4: basic search algorithms data structureChapter 4: basic search algorithms data structure
Chapter 4: basic search algorithms data structure
 
Chapter 5: linked list data structure
Chapter 5: linked list data structureChapter 5: linked list data structure
Chapter 5: linked list data structure
 
Chapter 3: basic sorting algorithms data structure
Chapter 3: basic sorting algorithms data structureChapter 3: basic sorting algorithms data structure
Chapter 3: basic sorting algorithms data structure
 
Presentation on queue
Presentation on queuePresentation on queue
Presentation on queue
 
08review (1)
08review (1)08review (1)
08review (1)
 
basics of queues
basics of queuesbasics of queues
basics of queues
 
Stacks in data structure
Stacks  in data structureStacks  in data structure
Stacks in data structure
 
Java ArrayList Video Tutorial
Java ArrayList Video TutorialJava ArrayList Video Tutorial
Java ArrayList Video Tutorial
 
Stack and queue
Stack and queueStack and queue
Stack and queue
 
Queue
QueueQueue
Queue
 
Stack Data Structure
Stack Data StructureStack Data Structure
Stack Data Structure
 
Data structure and its types
Data structure and its typesData structure and its types
Data structure and its types
 
Data structures and algorithms lab4
Data structures and algorithms lab4Data structures and algorithms lab4
Data structures and algorithms lab4
 
Ppt presentation of queues
Ppt presentation of queuesPpt presentation of queues
Ppt presentation of queues
 
Stack and Queue by M.Gomathi Lecturer
Stack and Queue by M.Gomathi LecturerStack and Queue by M.Gomathi Lecturer
Stack and Queue by M.Gomathi Lecturer
 
Control statements
Control statementsControl statements
Control statements
 
Java Linked List Tutorial | Edureka
Java Linked List Tutorial |  EdurekaJava Linked List Tutorial |  Edureka
Java Linked List Tutorial | Edureka
 
Java - Collections
Java - CollectionsJava - Collections
Java - Collections
 
Python session 10
Python session 10Python session 10
Python session 10
 
Ds
DsDs
Ds
 

En vedette

Revenue Performance Management & Email – a Powerful Duo by Kath Pay
Revenue Performance Management & Email – a Powerful Duo by Kath PayRevenue Performance Management & Email – a Powerful Duo by Kath Pay
Revenue Performance Management & Email – a Powerful Duo by Kath Pay
Holistic Email Marketing
 
Talk about myself fhad no name
Talk about myself fhad no nameTalk about myself fhad no name
Talk about myself fhad no name
Scott Dagilis
 
Using OU Campus in a Luminis Portal
Using OU Campus in a Luminis PortalUsing OU Campus in a Luminis Portal
Using OU Campus in a Luminis Portal
Rachel Reuben
 
7 reasons why your subscribers don't respond to your email campaigns
7 reasons why your subscribers don't respond to your email campaigns7 reasons why your subscribers don't respond to your email campaigns
7 reasons why your subscribers don't respond to your email campaigns
Holistic Email Marketing
 
ձայնի ընկալումը կենդանիների կողմից
ձայնի ընկալումը կենդանիների կողմիցձայնի ընկալումը կենդանիների կողմից
ձայնի ընկալումը կենդանիների կողմից
Gohar Gasparyan
 

En vedette (20)

Large scale web apps
Large scale web appsLarge scale web apps
Large scale web apps
 
Revenue Performance Management & Email – a Powerful Duo by Kath Pay
Revenue Performance Management & Email – a Powerful Duo by Kath PayRevenue Performance Management & Email – a Powerful Duo by Kath Pay
Revenue Performance Management & Email – a Powerful Duo by Kath Pay
 
Talk about myself fhad no name
Talk about myself fhad no nameTalk about myself fhad no name
Talk about myself fhad no name
 
Juridische tips voor Nederlandse webshops die zich op België richten
Juridische tips voor Nederlandse webshops die zich op België richtenJuridische tips voor Nederlandse webshops die zich op België richten
Juridische tips voor Nederlandse webshops die zich op België richten
 
Threads of Change in Marketing Communications
Threads of Change in Marketing CommunicationsThreads of Change in Marketing Communications
Threads of Change in Marketing Communications
 
Cixlo
CixloCixlo
Cixlo
 
Justin Biggs.Principal,Michigan City Area Schools personalities
Justin Biggs.Principal,Michigan City Area Schools personalitiesJustin Biggs.Principal,Michigan City Area Schools personalities
Justin Biggs.Principal,Michigan City Area Schools personalities
 
Using OU Campus in a Luminis Portal
Using OU Campus in a Luminis PortalUsing OU Campus in a Luminis Portal
Using OU Campus in a Luminis Portal
 
7 reasons why your subscribers don't respond to your email campaigns
7 reasons why your subscribers don't respond to your email campaigns7 reasons why your subscribers don't respond to your email campaigns
7 reasons why your subscribers don't respond to your email campaigns
 
Rapport d'activité SoFAB 2014-2015
Rapport d'activité SoFAB 2014-2015Rapport d'activité SoFAB 2014-2015
Rapport d'activité SoFAB 2014-2015
 
Рузан Минасян
Рузан МинасянРузан Минасян
Рузан Минасян
 
Hash map
Hash mapHash map
Hash map
 
ձայնի ընկալումը կենդանիների կողմից
ձայնի ընկալումը կենդանիների կողմիցձայնի ընկալումը կենդանիների կողմից
ձայնի ընկալումը կենդանիների կողմից
 
Informatica
InformaticaInformatica
Informatica
 
Geneva Tourism case – automated charm offensive for foreign tourists
Geneva Tourism case – automated charm offensive for foreign touristsGeneva Tourism case – automated charm offensive for foreign tourists
Geneva Tourism case – automated charm offensive for foreign tourists
 
Revue de presse Telecom Valley - Septembre 2015
Revue de presse Telecom Valley - Septembre 2015Revue de presse Telecom Valley - Septembre 2015
Revue de presse Telecom Valley - Septembre 2015
 
Java IO
Java IOJava IO
Java IO
 
Linked List data structure
Linked List data structureLinked List data structure
Linked List data structure
 
Productive Programming in Java 8 - with Lambdas and Streams
Productive Programming in Java 8 - with Lambdas and Streams Productive Programming in Java 8 - with Lambdas and Streams
Productive Programming in Java 8 - with Lambdas and Streams
 
1 intro of data structure course
1  intro of data structure course1  intro of data structure course
1 intro of data structure course
 

Similaire à 7 stack and vector

Learn java
Learn javaLearn java
Learn java
Palahuja
 
Struts Java I I Lecture 8
Struts  Java  I I  Lecture 8Struts  Java  I I  Lecture 8
Struts Java I I Lecture 8
patinijava
 
Krazykoder struts2 interceptors
Krazykoder struts2 interceptorsKrazykoder struts2 interceptors
Krazykoder struts2 interceptors
Krazy Koder
 
Java findamentals2
Java findamentals2Java findamentals2
Java findamentals2
Todor Kolev
 
Java findamentals2
Java findamentals2Java findamentals2
Java findamentals2
Todor Kolev
 
Abstract Data Types (a) Explain briefly what is meant by the ter.pdf
Abstract Data Types (a) Explain briefly what is meant by the ter.pdfAbstract Data Types (a) Explain briefly what is meant by the ter.pdf
Abstract Data Types (a) Explain briefly what is meant by the ter.pdf
karymadelaneyrenne19
 
Android development training programme , Day 3
Android development training programme , Day 3Android development training programme , Day 3
Android development training programme , Day 3
DHIRAJ PRAVIN
 

Similaire à 7 stack and vector (20)

Learn java
Learn javaLearn java
Learn java
 
Java mcq
Java mcqJava mcq
Java mcq
 
Struts Java I I Lecture 8
Struts  Java  I I  Lecture 8Struts  Java  I I  Lecture 8
Struts Java I I Lecture 8
 
Krazykoder struts2 interceptors
Krazykoder struts2 interceptorsKrazykoder struts2 interceptors
Krazykoder struts2 interceptors
 
Java interview questions
Java interview questionsJava interview questions
Java interview questions
 
Designing Better API
Designing Better APIDesigning Better API
Designing Better API
 
Java findamentals2
Java findamentals2Java findamentals2
Java findamentals2
 
Java findamentals2
Java findamentals2Java findamentals2
Java findamentals2
 
Bt0083 server side programing 2
Bt0083 server side programing  2Bt0083 server side programing  2
Bt0083 server side programing 2
 
Tuning and optimizing webcenter spaces application white paper
Tuning and optimizing webcenter spaces application white paperTuning and optimizing webcenter spaces application white paper
Tuning and optimizing webcenter spaces application white paper
 
Structural pattern 3
Structural pattern 3Structural pattern 3
Structural pattern 3
 
Java concurrency model - The Future Task
Java concurrency model - The Future TaskJava concurrency model - The Future Task
Java concurrency model - The Future Task
 
Best practices in Java
Best practices in JavaBest practices in Java
Best practices in Java
 
Abstract Data Types (a) Explain briefly what is meant by the ter.pdf
Abstract Data Types (a) Explain briefly what is meant by the ter.pdfAbstract Data Types (a) Explain briefly what is meant by the ter.pdf
Abstract Data Types (a) Explain briefly what is meant by the ter.pdf
 
Android session-5-sajib
Android session-5-sajibAndroid session-5-sajib
Android session-5-sajib
 
Session 4/5 working with salesforce data including LDS
Session 4/5   working with salesforce data including LDSSession 4/5   working with salesforce data including LDS
Session 4/5 working with salesforce data including LDS
 
My 70-480 HTML5 certification learning
My 70-480 HTML5 certification learningMy 70-480 HTML5 certification learning
My 70-480 HTML5 certification learning
 
Java server pages
Java server pagesJava server pages
Java server pages
 
Android development training programme , Day 3
Android development training programme , Day 3Android development training programme , Day 3
Android development training programme , Day 3
 
java_inheritance.pdf
java_inheritance.pdfjava_inheritance.pdf
java_inheritance.pdf
 

Plus de Mahmoud Alfarra

8 programming-using-java decision-making practices 20102011
8 programming-using-java decision-making practices 201020118 programming-using-java decision-making practices 20102011
8 programming-using-java decision-making practices 20102011
Mahmoud Alfarra
 

Plus de Mahmoud Alfarra (20)

Computer Programming, Loops using Java - part 2
Computer Programming, Loops using Java - part 2Computer Programming, Loops using Java - part 2
Computer Programming, Loops using Java - part 2
 
Computer Programming, Loops using Java
Computer Programming, Loops using JavaComputer Programming, Loops using Java
Computer Programming, Loops using Java
 
Chapter9 graph data structure
Chapter9  graph data structureChapter9  graph data structure
Chapter9 graph data structure
 
Chapter 8: tree data structure
Chapter 8:  tree data structureChapter 8:  tree data structure
Chapter 8: tree data structure
 
Chapter 7: Queue data structure
Chapter 7:  Queue data structureChapter 7:  Queue data structure
Chapter 7: Queue data structure
 
Chapter 6: stack data structure
Chapter 6:  stack data structureChapter 6:  stack data structure
Chapter 6: stack data structure
 
Chapter 2: array and array list data structure
Chapter 2: array and array list  data structureChapter 2: array and array list  data structure
Chapter 2: array and array list data structure
 
Chapter1 intro toprincipleofc#_datastructure_b_cs
Chapter1  intro toprincipleofc#_datastructure_b_csChapter1  intro toprincipleofc#_datastructure_b_cs
Chapter1 intro toprincipleofc#_datastructure_b_cs
 
Chapter 0: introduction to data structure
Chapter 0: introduction to data structureChapter 0: introduction to data structure
Chapter 0: introduction to data structure
 
3 classification
3  classification3  classification
3 classification
 
8 programming-using-java decision-making practices 20102011
8 programming-using-java decision-making practices 201020118 programming-using-java decision-making practices 20102011
8 programming-using-java decision-making practices 20102011
 
7 programming-using-java decision-making220102011
7 programming-using-java decision-making2201020117 programming-using-java decision-making220102011
7 programming-using-java decision-making220102011
 
6 programming-using-java decision-making20102011-
6 programming-using-java decision-making20102011-6 programming-using-java decision-making20102011-
6 programming-using-java decision-making20102011-
 
5 programming-using-java intro-tooop20102011
5 programming-using-java intro-tooop201020115 programming-using-java intro-tooop20102011
5 programming-using-java intro-tooop20102011
 
4 programming-using-java intro-tojava20102011
4 programming-using-java intro-tojava201020114 programming-using-java intro-tojava20102011
4 programming-using-java intro-tojava20102011
 
3 programming-using-java introduction-to computer
3 programming-using-java introduction-to computer3 programming-using-java introduction-to computer
3 programming-using-java introduction-to computer
 
2 programming-using-java how to built application
2 programming-using-java how to built application2 programming-using-java how to built application
2 programming-using-java how to built application
 
1 programming-using-java -introduction
1 programming-using-java -introduction1 programming-using-java -introduction
1 programming-using-java -introduction
 
تلخيص النصوص تلقائيا
تلخيص النصوص تلقائياتلخيص النصوص تلقائيا
تلخيص النصوص تلقائيا
 
4×4×4 لتحصيل التميز
4×4×4 لتحصيل التميز4×4×4 لتحصيل التميز
4×4×4 لتحصيل التميز
 

Dernier

Dernier (20)

Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docx
 
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxSKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
Dyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptxDyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptx
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Asian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptxAsian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptx
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 

7 stack and vector

  • 1. using Java 2015 Data Structure Prepared by: Mahmoud Rafeek Al-farra in Java 7. Stack and Vector
  • 2. mfarra.cst.ps www.fb.com/MahmoudRFarra Contents Implementation Stack using Linked List Vector and Stack Class Introduction Case Study: Evaluating Expressions
  • 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.
  • 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.
  • 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 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]
  • 22. Case Study: Evaluating Expressions mfarra.cst.ps www.fb.com/MahmoudRFarra
  • 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