SlideShare a Scribd company logo
1 of 22
DATA STRUCTURE
Chapter 6: Stack
Prepared & Presented by
Mr. Mahmoud R. Alfarra
2011-2012
College of Science & Technology
Dep. Of Computer Science & IT
BCs of Information Technology
http://mfarra.cst.ps
Out Line
 What is Stack data structure?
 Push operation
 Pop operation
 Retrieve the data of an element
 Clear the Stack
 Print all data of stack
 Search about data
 Stack Collection
2
What is the Stack?
 Stack is a dynamic linear data structure.
 Data in a stack are added and removed from
only one end of the list.
3
What is the Stack?
4
‫البيانات‬ ‫تراكيب‬ ‫مساق‬
‫إعداد‬ ‫العلمية‬ ‫المادة‬
/
‫أ‬
.
‫ا‬ َّ‫الفــر‬ ‫رفيق‬ ‫محمود‬
 We define a stack as a list of items that are
accessible only from the end of the list, which
is called the top of the stack.
 Elements are always removed from the top,
and inserted on the top also.
 A stack is known as a Last-in, First-out (LIFO)
data structure
Elements of stack
5
Top
6
1
7
Employee’s element for stack
6
‫البيانات‬ ‫تراكيب‬ ‫مساق‬
‫إعداد‬ ‫العلمية‬ ‫المادة‬
/
‫أ‬
.
‫ا‬ َّ‫الفــر‬ ‫رفيق‬ ‫محمود‬
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; } }
Stack of employees
7
1. class EmployeeStack
2. {
3. Employee Top = null;
4. int length =0;
5. //the operation of stack will be inserted here
6. }
Push operation
8
Top
6
Top
6
1
Top
6
1
7
Push operation
9
‫البيانات‬ ‫تراكيب‬ ‫مساق‬
‫إعداد‬ ‫العلمية‬ ‫المادة‬
/
‫أ‬
.
‫ا‬ َّ‫الفــر‬ ‫رفيق‬ ‫محمود‬
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. Console.WriteLine("A new Employee has been added to Stack:
"+length);
11. }
Pop operation
10
Top
6
1
7 Top
6
1
7
Pop operation
11
1. public void Pop()
2. {
3. if (Top == null)
4. Console.WriteLine("Stack is Empty!!");
5. else
6. {
7. Top = Top.next;
8. length--;
9. Console.WriteLine("Now Top Points to: " +
Top.name);
10. }
11.
12. }
Peek operation: to display info of top
element
12
Top
6
1
7
Top.name
Top.salary
…
Peek operation: to display info of top
element
13
1. public void Peek()
2. {
3. if (Top == null)
4. Console.WriteLine("The Stack is Empty!!");
5. else
6. Console.WriteLine("The Employee Data:n"+
7. "Name: "+Top.name+"nSalary: "+Top.salary);
8. }
Clear the Stack
14
 Clearing the stack means that the top must
points to null.
Top
6
1
7
Clear the Stack
15
1. public void Clear()
2. {
3. if (Top == null)
4. Console.WriteLine("The Stack is Empty!!");
5. else
6. Top = null;
7. Length = 0;
8. }
Print all data of stack
16
Top
6
1
7
next
next
current
Print all data of stack
17
1. public void PrintAll()
2. {
3. Employee current = Top;
4. int x =1;
5. while (current != null)
6. {
7. Console.WriteLine("Data Of Employee # "+x+" isn "+ "Name: “ +
current.name + "nSalary: "+current.salary);
8. Console.WriteLine("==============================");
9. x++;
10. current = current.next;
11. }
12. }
Search about data
18
Top
6
1
7
next
next
current
Search about data
19
‫البيانات‬ ‫تراكيب‬ ‫مساق‬
‫إعداد‬ ‫العلمية‬ ‫المادة‬
/
‫أ‬
.
‫ا‬ َّ‫الفــر‬ ‫رفيق‬ ‫محمود‬
1. public void SearchByName(String name )
2. {
3. Employee current = Top;
4. bool flag = false;
5. while ( current != null)
6. {
7. if (name.CompareTo(current.name) == 0)
8. {
9. flag = true;
10. break;
11. }
12. current = current.next;
13. }
14. if (flag == true)
15. Console.WriteLine("Exist!!");
16. else
17. Console.WriteLine("Does not Exist!!");
18. }
Stack Collection
 The Stack class is an implementation of the
ICollection interface that represents a LIFO
collection, or a stack.
 The class is implemented in the .NET
Framework as a circular buffer, which enables
space for items pushed on the stack to be
allocated dynamically.
 The default constructor is called as follows:
Stack myStack = new Stack();
Practice:
develop a full
application
using stack
class
Thank You …
21
Remember that: question is the key of knowledge
Ahl Eljanna 

َّ‫ت‬ُ
‫جم‬‫ل‬‫ا‬ َ
‫د‬ِ
‫ع‬ُ
‫و‬ ِ
‫ِت‬َّ‫ل‬‫ا‬ ِ
‫َّة‬‫ن‬َ‫ج‬
‫ْل‬‫ا‬ ُ
‫ل‬َ‫ث‬َ
‫م‬
َ
‫م‬ ‫ج‬
‫ن‬ِ
‫م‬ ٌ
‫ار‬َ
‫ه‬‫ج‬‫َن‬‫أ‬ ‫ا‬َ
‫يه‬ِ‫ف‬ َ
‫ن‬‫و‬ُ
‫ق‬
ٍ
‫ن‬ ِ
ِ‫س‬ ِ‫ج‬
‫ر‬َ‫غ‬ ٍ
‫اء‬
‫ج‬ َّ‫ي‬َ َ‫ت‬َ ‫ج‬َ
‫ي‬ ٍَ َ‫ل‬ ‫ج‬
‫ن‬ ِ
‫م‬ ٌ
‫ار‬ َ
‫ه‬‫ج‬‫َن‬‫أ‬َ
‫و‬
‫ج‬َ ‫ج‬
‫ن‬ ِ
‫م‬ ٌ
‫ار‬ َ
‫ه‬‫ج‬‫َن‬‫أ‬َ
‫و‬ َُ ُ
‫م‬‫ج‬‫ع‬َ‫ط‬
ٍَّ َ‫ل‬ ٍ
ُ
‫م‬ ٍ
‫ل‬ َ َ
‫ع‬ ‫ج‬
‫ن‬ ِ
‫م‬ ٌ
‫ار‬ َ
‫ه‬‫ج‬‫َن‬‫أ‬َ
‫و‬ َ
‫و‬َِِ
‫ر‬‫ا‬ َّ
‫لش‬ِ‫ل‬
ِ
‫م‬ ‫ا‬ َ
‫يه‬ِ‫ف‬ ‫ج‬
‫ه‬َُ
‫م‬َ
‫و‬ َ‫و‬ َ
‫ص‬
ِ
‫ل‬ ُ
‫ك‬ ‫ج‬
‫ن‬
‫ج‬
‫ه‬ِِ
‫ِب‬َ
‫ر‬ ‫ج‬
‫ن‬ِ
‫م‬ ٌَ
ِ‫ج‬ َ
‫م‬َ
‫و‬ ِ
‫ات‬ََ
‫َّم‬‫ث‬‫ال‬
22

More Related Content

What's hot

Data structure lab manual
Data structure lab manualData structure lab manual
Data structure lab manualnikshaikh786
 
Queue Data Structure
Queue Data StructureQueue Data Structure
Queue Data StructureZidny Nafan
 
Stacks,queues,linked-list
Stacks,queues,linked-listStacks,queues,linked-list
Stacks,queues,linked-listpinakspatel
 
My lectures circular queue
My lectures circular queueMy lectures circular queue
My lectures circular queueSenthil Kumar
 
queue & its applications
queue & its applicationsqueue & its applications
queue & its applicationssomendra kumar
 
Queue implementation
Queue implementationQueue implementation
Queue implementationRajendran
 
Mca ii dfs u-3 linklist,stack,queue
Mca ii dfs u-3 linklist,stack,queueMca ii dfs u-3 linklist,stack,queue
Mca ii dfs u-3 linklist,stack,queueRai University
 
Queue as data_structure
Queue as data_structureQueue as data_structure
Queue as data_structureeShikshak
 
Queue data structure
Queue data structureQueue data structure
Queue data structureMekk Mhmd
 
stacks in algorithems and data structure
stacks in algorithems and data structurestacks in algorithems and data structure
stacks in algorithems and data structurefaran nawaz
 
Algorithm: priority queue
Algorithm: priority queueAlgorithm: priority queue
Algorithm: priority queueTareq Hasan
 

What's hot (20)

Data structure stack&queue basics
Data structure stack&queue   basicsData structure stack&queue   basics
Data structure stack&queue basics
 
3 Array operations
3   Array operations3   Array operations
3 Array operations
 
Data structure lab manual
Data structure lab manualData structure lab manual
Data structure lab manual
 
Stack and Queue
Stack and Queue Stack and Queue
Stack and Queue
 
Queue Data Structure
Queue Data StructureQueue Data Structure
Queue Data Structure
 
Queue
QueueQueue
Queue
 
Stacks,queues,linked-list
Stacks,queues,linked-listStacks,queues,linked-list
Stacks,queues,linked-list
 
My lectures circular queue
My lectures circular queueMy lectures circular queue
My lectures circular queue
 
queue & its applications
queue & its applicationsqueue & its applications
queue & its applications
 
Stacks, Queues, Deques
Stacks, Queues, DequesStacks, Queues, Deques
Stacks, Queues, Deques
 
Queue in Data Structure
Queue in Data StructureQueue in Data Structure
Queue in Data Structure
 
Queue implementation
Queue implementationQueue implementation
Queue implementation
 
Mca ii dfs u-3 linklist,stack,queue
Mca ii dfs u-3 linklist,stack,queueMca ii dfs u-3 linklist,stack,queue
Mca ii dfs u-3 linklist,stack,queue
 
Team 6
Team 6Team 6
Team 6
 
Queue as data_structure
Queue as data_structureQueue as data_structure
Queue as data_structure
 
Queue data structure
Queue data structureQueue data structure
Queue data structure
 
Priority queues
Priority queuesPriority queues
Priority queues
 
stacks in algorithems and data structure
stacks in algorithems and data structurestacks in algorithems and data structure
stacks in algorithems and data structure
 
Algorithm: priority queue
Algorithm: priority queueAlgorithm: priority queue
Algorithm: priority queue
 
Stack and queue
Stack and queueStack and queue
Stack and queue
 

Similar to Stack Data Structure Chapter: Push, Pop, Peek Operations

STACK IN DATA STRUCTURE .pptx
STACK IN DATA STRUCTURE .pptxSTACK IN DATA STRUCTURE .pptx
STACK IN DATA STRUCTURE .pptxCHANCHALSEN2
 
Data structure , stack , queue
Data structure , stack , queueData structure , stack , queue
Data structure , stack , queueRajkiran Nadar
 
Data structure week y 5
Data structure week y 5Data structure week y 5
Data structure week y 5karmuhtam
 
Stack and its applications
Stack and its applicationsStack and its applications
Stack and its applicationsAhsan Mansiv
 
stack slide vghbjnkmfgvhbjnmkgvbhjnkmgbhjnkm
stack slide vghbjnkmfgvhbjnmkgvbhjnkmgbhjnkmstack slide vghbjnkmfgvhbjnmkgvbhjnkmgbhjnkm
stack slide vghbjnkmfgvhbjnmkgvbhjnkmgbhjnkmxadihe1593
 
What is Stack, Its Operations, Queue, Circular Queue, Priority Queue
What is Stack, Its Operations, Queue, Circular Queue, Priority QueueWhat is Stack, Its Operations, Queue, Circular Queue, Priority Queue
What is Stack, Its Operations, Queue, Circular Queue, Priority QueueBalwant Gorad
 
Stack linked list
Stack linked listStack linked list
Stack linked listbhargav0077
 
DATA STRUCTURE USING C & C++
DATA STRUCTURE USING C & C++DATA STRUCTURE USING C & C++
DATA STRUCTURE USING C & C++mustkeem khan
 
DSU C&C++ Practical File Diploma
DSU C&C++ Practical File DiplomaDSU C&C++ Practical File Diploma
DSU C&C++ Practical File Diplomamustkeem khan
 
Stacks & Queues By Ms. Niti Arora
Stacks & Queues By Ms. Niti AroraStacks & Queues By Ms. Niti Arora
Stacks & Queues By Ms. Niti Arorakulachihansraj
 
Stacks & Queues
Stacks & QueuesStacks & Queues
Stacks & Queuestech4us
 
Stacks in algorithems & data structure
Stacks in algorithems & data structureStacks in algorithems & data structure
Stacks in algorithems & data structurefaran nawaz
 
Impress Your Friends with EcmaScript 2015
Impress Your Friends with EcmaScript 2015Impress Your Friends with EcmaScript 2015
Impress Your Friends with EcmaScript 2015Lukas Ruebbelke
 

Similar to Stack Data Structure Chapter: Push, Pop, Peek Operations (20)

STACK IN DATA STRUCTURE .pptx
STACK IN DATA STRUCTURE .pptxSTACK IN DATA STRUCTURE .pptx
STACK IN DATA STRUCTURE .pptx
 
04 stacks
04 stacks04 stacks
04 stacks
 
Data structure , stack , queue
Data structure , stack , queueData structure , stack , queue
Data structure , stack , queue
 
Data structure week y 5
Data structure week y 5Data structure week y 5
Data structure week y 5
 
Stack of Data structure
Stack of Data structureStack of Data structure
Stack of Data structure
 
Stack data structure
Stack data structureStack data structure
Stack data structure
 
Stack and its applications
Stack and its applicationsStack and its applications
Stack and its applications
 
Stack
StackStack
Stack
 
Stacks in c++
Stacks in c++Stacks in c++
Stacks in c++
 
stack slide vghbjnkmfgvhbjnmkgvbhjnkmgbhjnkm
stack slide vghbjnkmfgvhbjnmkgvbhjnkmgbhjnkmstack slide vghbjnkmfgvhbjnmkgvbhjnkmgbhjnkm
stack slide vghbjnkmfgvhbjnmkgvbhjnkmgbhjnkm
 
MO 2020 DS Stacks 1 AB.ppt
MO 2020 DS Stacks 1 AB.pptMO 2020 DS Stacks 1 AB.ppt
MO 2020 DS Stacks 1 AB.ppt
 
What is Stack, Its Operations, Queue, Circular Queue, Priority Queue
What is Stack, Its Operations, Queue, Circular Queue, Priority QueueWhat is Stack, Its Operations, Queue, Circular Queue, Priority Queue
What is Stack, Its Operations, Queue, Circular Queue, Priority Queue
 
Stack linked list
Stack linked listStack linked list
Stack linked list
 
DATA STRUCTURE USING C & C++
DATA STRUCTURE USING C & C++DATA STRUCTURE USING C & C++
DATA STRUCTURE USING C & C++
 
DSU C&C++ Practical File Diploma
DSU C&C++ Practical File DiplomaDSU C&C++ Practical File Diploma
DSU C&C++ Practical File Diploma
 
Stacks & Queues By Ms. Niti Arora
Stacks & Queues By Ms. Niti AroraStacks & Queues By Ms. Niti Arora
Stacks & Queues By Ms. Niti Arora
 
Stacks & Queues
Stacks & QueuesStacks & Queues
Stacks & Queues
 
Stacks in algorithems & data structure
Stacks in algorithems & data structureStacks in algorithems & data structure
Stacks in algorithems & data structure
 
U3.stack queue
U3.stack queueU3.stack queue
U3.stack queue
 
Impress Your Friends with EcmaScript 2015
Impress Your Friends with EcmaScript 2015Impress Your Friends with EcmaScript 2015
Impress Your Friends with EcmaScript 2015
 

More from Mahmoud Alfarra

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 2Mahmoud Alfarra
 
Computer Programming, Loops using Java
Computer Programming, Loops using JavaComputer Programming, Loops using Java
Computer Programming, Loops using JavaMahmoud Alfarra
 
Chapter 10: hashing data structure
Chapter 10:  hashing data structureChapter 10:  hashing data structure
Chapter 10: hashing data structureMahmoud Alfarra
 
Chapter9 graph data structure
Chapter9  graph data structureChapter9  graph data structure
Chapter9 graph data structureMahmoud Alfarra
 
Chapter 8: tree data structure
Chapter 8:  tree data structureChapter 8:  tree data structure
Chapter 8: tree data structureMahmoud Alfarra
 
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 structureMahmoud Alfarra
 
Chapter 0: introduction to data structure
Chapter 0: introduction to data structureChapter 0: introduction to data structure
Chapter 0: introduction to data structureMahmoud 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 20102011Mahmoud Alfarra
 
7 programming-using-java decision-making220102011
7 programming-using-java decision-making2201020117 programming-using-java decision-making220102011
7 programming-using-java decision-making220102011Mahmoud Alfarra
 
6 programming-using-java decision-making20102011-
6 programming-using-java decision-making20102011-6 programming-using-java decision-making20102011-
6 programming-using-java decision-making20102011-Mahmoud Alfarra
 
5 programming-using-java intro-tooop20102011
5 programming-using-java intro-tooop201020115 programming-using-java intro-tooop20102011
5 programming-using-java intro-tooop20102011Mahmoud Alfarra
 
4 programming-using-java intro-tojava20102011
4 programming-using-java intro-tojava201020114 programming-using-java intro-tojava20102011
4 programming-using-java intro-tojava20102011Mahmoud Alfarra
 
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 computerMahmoud Alfarra
 
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 applicationMahmoud Alfarra
 
1 programming-using-java -introduction
1 programming-using-java -introduction1 programming-using-java -introduction
1 programming-using-java -introductionMahmoud Alfarra
 
تلخيص النصوص تلقائيا
تلخيص النصوص تلقائياتلخيص النصوص تلقائيا
تلخيص النصوص تلقائياMahmoud Alfarra
 
4×4×4 لتحصيل التميز
4×4×4 لتحصيل التميز4×4×4 لتحصيل التميز
4×4×4 لتحصيل التميزMahmoud Alfarra
 
Data preparation and processing chapter 2
Data preparation and processing chapter  2Data preparation and processing chapter  2
Data preparation and processing chapter 2Mahmoud Alfarra
 
Introduction to-data-mining chapter 1
Introduction to-data-mining  chapter 1Introduction to-data-mining  chapter 1
Introduction to-data-mining chapter 1Mahmoud Alfarra
 

More from 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
 
Chapter 10: hashing data structure
Chapter 10:  hashing data structureChapter 10:  hashing data structure
Chapter 10: hashing data structure
 
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 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
 
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 لتحصيل التميز
 
Data preparation and processing chapter 2
Data preparation and processing chapter  2Data preparation and processing chapter  2
Data preparation and processing chapter 2
 
Introduction to-data-mining chapter 1
Introduction to-data-mining  chapter 1Introduction to-data-mining  chapter 1
Introduction to-data-mining chapter 1
 

Recently uploaded

18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesFatimaKhan178732
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpinRaunakKeshri1
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
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 ConsultingTechSoup
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactPECB
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 

Recently uploaded (20)

18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and Actinides
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpin
 
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
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
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 

Stack Data Structure Chapter: Push, Pop, Peek Operations

  • 1. DATA STRUCTURE Chapter 6: Stack Prepared & Presented by Mr. Mahmoud R. Alfarra 2011-2012 College of Science & Technology Dep. Of Computer Science & IT BCs of Information Technology http://mfarra.cst.ps
  • 2. Out Line  What is Stack data structure?  Push operation  Pop operation  Retrieve the data of an element  Clear the Stack  Print all data of stack  Search about data  Stack Collection 2
  • 3. What is the Stack?  Stack is a dynamic linear data structure.  Data in a stack are added and removed from only one end of the list. 3
  • 4. What is the Stack? 4 ‫البيانات‬ ‫تراكيب‬ ‫مساق‬ ‫إعداد‬ ‫العلمية‬ ‫المادة‬ / ‫أ‬ . ‫ا‬ َّ‫الفــر‬ ‫رفيق‬ ‫محمود‬  We define a stack as a list of items that are accessible only from the end of the list, which is called the top of the stack.  Elements are always removed from the top, and inserted on the top also.  A stack is known as a Last-in, First-out (LIFO) data structure
  • 6. Employee’s element for stack 6 ‫البيانات‬ ‫تراكيب‬ ‫مساق‬ ‫إعداد‬ ‫العلمية‬ ‫المادة‬ / ‫أ‬ . ‫ا‬ َّ‫الفــر‬ ‫رفيق‬ ‫محمود‬ 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; } }
  • 7. Stack of employees 7 1. class EmployeeStack 2. { 3. Employee Top = null; 4. int length =0; 5. //the operation of stack will be inserted here 6. }
  • 9. Push operation 9 ‫البيانات‬ ‫تراكيب‬ ‫مساق‬ ‫إعداد‬ ‫العلمية‬ ‫المادة‬ / ‫أ‬ . ‫ا‬ َّ‫الفــر‬ ‫رفيق‬ ‫محمود‬ 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. Console.WriteLine("A new Employee has been added to Stack: "+length); 11. }
  • 11. Pop operation 11 1. public void Pop() 2. { 3. if (Top == null) 4. Console.WriteLine("Stack is Empty!!"); 5. else 6. { 7. Top = Top.next; 8. length--; 9. Console.WriteLine("Now Top Points to: " + Top.name); 10. } 11. 12. }
  • 12. Peek operation: to display info of top element 12 Top 6 1 7 Top.name Top.salary …
  • 13. Peek operation: to display info of top element 13 1. public void Peek() 2. { 3. if (Top == null) 4. Console.WriteLine("The Stack is Empty!!"); 5. else 6. Console.WriteLine("The Employee Data:n"+ 7. "Name: "+Top.name+"nSalary: "+Top.salary); 8. }
  • 14. Clear the Stack 14  Clearing the stack means that the top must points to null. Top 6 1 7
  • 15. Clear the Stack 15 1. public void Clear() 2. { 3. if (Top == null) 4. Console.WriteLine("The Stack is Empty!!"); 5. else 6. Top = null; 7. Length = 0; 8. }
  • 16. Print all data of stack 16 Top 6 1 7 next next current
  • 17. Print all data of stack 17 1. public void PrintAll() 2. { 3. Employee current = Top; 4. int x =1; 5. while (current != null) 6. { 7. Console.WriteLine("Data Of Employee # "+x+" isn "+ "Name: “ + current.name + "nSalary: "+current.salary); 8. Console.WriteLine("=============================="); 9. x++; 10. current = current.next; 11. } 12. }
  • 19. Search about data 19 ‫البيانات‬ ‫تراكيب‬ ‫مساق‬ ‫إعداد‬ ‫العلمية‬ ‫المادة‬ / ‫أ‬ . ‫ا‬ َّ‫الفــر‬ ‫رفيق‬ ‫محمود‬ 1. public void SearchByName(String name ) 2. { 3. Employee current = Top; 4. bool flag = false; 5. while ( current != null) 6. { 7. if (name.CompareTo(current.name) == 0) 8. { 9. flag = true; 10. break; 11. } 12. current = current.next; 13. } 14. if (flag == true) 15. Console.WriteLine("Exist!!"); 16. else 17. Console.WriteLine("Does not Exist!!"); 18. }
  • 20. Stack Collection  The Stack class is an implementation of the ICollection interface that represents a LIFO collection, or a stack.  The class is implemented in the .NET Framework as a circular buffer, which enables space for items pushed on the stack to be allocated dynamically.  The default constructor is called as follows: Stack myStack = new Stack(); Practice: develop a full application using stack class
  • 21. Thank You … 21 Remember that: question is the key of knowledge
  • 22. Ahl Eljanna   َّ‫ت‬ُ ‫جم‬‫ل‬‫ا‬ َ ‫د‬ِ ‫ع‬ُ ‫و‬ ِ ‫ِت‬َّ‫ل‬‫ا‬ ِ ‫َّة‬‫ن‬َ‫ج‬ ‫ْل‬‫ا‬ ُ ‫ل‬َ‫ث‬َ ‫م‬ َ ‫م‬ ‫ج‬ ‫ن‬ِ ‫م‬ ٌ ‫ار‬َ ‫ه‬‫ج‬‫َن‬‫أ‬ ‫ا‬َ ‫يه‬ِ‫ف‬ َ ‫ن‬‫و‬ُ ‫ق‬ ٍ ‫ن‬ ِ ِ‫س‬ ِ‫ج‬ ‫ر‬َ‫غ‬ ٍ ‫اء‬ ‫ج‬ َّ‫ي‬َ َ‫ت‬َ ‫ج‬َ ‫ي‬ ٍَ َ‫ل‬ ‫ج‬ ‫ن‬ ِ ‫م‬ ٌ ‫ار‬ َ ‫ه‬‫ج‬‫َن‬‫أ‬َ ‫و‬ ‫ج‬َ ‫ج‬ ‫ن‬ ِ ‫م‬ ٌ ‫ار‬ َ ‫ه‬‫ج‬‫َن‬‫أ‬َ ‫و‬ َُ ُ ‫م‬‫ج‬‫ع‬َ‫ط‬ ٍَّ َ‫ل‬ ٍ ُ ‫م‬ ٍ ‫ل‬ َ َ ‫ع‬ ‫ج‬ ‫ن‬ ِ ‫م‬ ٌ ‫ار‬ َ ‫ه‬‫ج‬‫َن‬‫أ‬َ ‫و‬ َ ‫و‬َِِ ‫ر‬‫ا‬ َّ ‫لش‬ِ‫ل‬ ِ ‫م‬ ‫ا‬ َ ‫يه‬ِ‫ف‬ ‫ج‬ ‫ه‬َُ ‫م‬َ ‫و‬ َ‫و‬ َ ‫ص‬ ِ ‫ل‬ ُ ‫ك‬ ‫ج‬ ‫ن‬ ‫ج‬ ‫ه‬ِِ ‫ِب‬َ ‫ر‬ ‫ج‬ ‫ن‬ِ ‫م‬ ٌَ ِ‫ج‬ َ ‫م‬َ ‫و‬ ِ ‫ات‬ََ ‫َّم‬‫ث‬‫ال‬ 22