SlideShare a Scribd company logo
Stacks
CS 308 – Data Structures
What is a stack?
• It is an ordered group of homogeneous items of elements.
• Elements are added to and removed from the top of the
stack (the most recently added items are at the top of the
stack).
• The last element to be added is the first to be removed
(LIFO: Last In, First Out).
Stack Specification
• Definitions: (provided by the user)
– MAX_ITEMS: Max number of items that might be on
the stack
– ItemType: Data type of the items on the stack
• Operations
– MakeEmpty
– Boolean IsEmpty
– Boolean IsFull
– Push (ItemType newItem)
– Pop (ItemType& item)
Push (ItemType newItem)
• Function: Adds newItem to the top of
the stack.
• Preconditions: Stack has been
initialized and is not full.
• Postconditions: newItem is at the top
of the stack.
Pop (ItemType& item)
• Function: Removes topItem from stack and
returns it in item.
• Preconditions: Stack has been initialized
and is not empty.
• Postconditions: Top element has been
removed from stack and item is a copy of
the removed element.
Stack Implementation
#include "ItemType.h"
// Must be provided by the user of the class
// Contains definitions for MAX_ITEMS and ItemType
class StackType {
public:
StackType();
void MakeEmpty();
bool IsEmpty() const;
bool IsFull() const;
void Push(ItemType);
void Pop(ItemType&);
private:
int top;
ItemType items[MAX_ITEMS];
};
Stack Implementation (cont.)
StackType::StackType()
{
top = -1;
}
void StackType::MakeEmpty()
{
top = -1;
}
bool StackType::IsEmpty() const
{
return (top == -1);
}
Stack Implementation (cont.)
bool StackType::IsFull() const
{
return (top == MAX_ITEMS-1);
}
void StackType::Push(ItemType newItem)
{
top++;
items[top] = newItem;
}
void StackType::Pop(ItemType& item)
{
item = items[top];
top--;
}
Stack overflow
• The condition resulting from trying to push
an element onto a full stack.
if(!stack.IsFull())
stack.Push(item);
Stack underflow
• The condition resulting from trying to pop
an empty stack.
if(!stack.IsEmpty())
stack.Pop(item);
Implementing stacks using
templates
• Templates allow the compiler to generate
multiple versions of a class type or a
function by allowing parameterized types.
• It is similar to passing a parameter to a
function (we pass a data type to a class !!)
Implementing stacks using templates
template<class ItemType>
class StackType {
public:
StackType();
void MakeEmpty();
bool IsEmpty() const;
bool IsFull() const;
void Push(ItemType);
void Pop(ItemType&);
private:
int top;
ItemType items[MAX_ITEMS];
};
(cont.)
Example using templates
// Client code
StackType<int> myStack;
StackType<float> yourStack;
StackType<StrType> anotherStack;
myStack.Push(35);
yourStack.Push(584.39);
The compiler generates distinct class types
and gives its own internal name to each of
the types.
Function templates
• The definitions of the member functions must be
rewritten as function templates.
template<class ItemType>
StackType<ItemType>::StackType()
{
top = -1;
}
template<class ItemType>
void StackType<ItemType>::MakeEmpty()
{
top = -1;
}
Function templates (cont.)
template<class ItemType>
bool StackType<ItemType>::IsEmpty() const
{
return (top == -1);
}
template<class ItemType>
bool StackType<ItemType>::IsFull() const
{
return (top == MAX_ITEMS-1);
}
template<class ItemType>
void StackType<ItemType>::Push(ItemType newItem)
{
top++;
items[top] = newItem;
}
Function templates (cont.)
template<class ItemType>
void StackType<ItemType>::Pop(ItemType& item)
{
item = items[top];
top--;
}
Comments using templates
• The template<class T> designation must
precede the class method name in the
source code for each template class method.
• The word class is required by the syntax of
the language and does not mean that the
actual parameter must be the name of a
class.
• Passing a parameter to a template has an
effect at compile time.
Implementing stacks using dynamic
array allocation
template<class ItemType>
class StackType {
public:
StackType(int);
~StackType();
void MakeEmpty();
bool IsEmpty() const;
bool IsFull() const;
void Push(ItemType);
void Pop(ItemType&);
private:
int top;
int maxStack;
ItemType *items;
};
Implementing stacks using
dynamic array allocation (cont.)
template<class ItemType>
StackType<ItemType>::StackType(int max)
{
maxStack = max;
top = -1;
items = new ItemType[max];
}
template<class ItemType>
StackType<ItemType>::~StackType()
{
delete [ ] items;
}
Example: postfix expressions
• Postfix notation is another way of writing arithmetic
expressions.
• In postfix notation, the operator is written after the
two operands.
infix: 2+5 postfix: 2 5 +
• Expressions are evaluated from left to right.
• Precedence rules and parentheses are never needed!!
Example: postfix expressions
(cont.)
Postfix expressions:
Algorithm using stacks (cont.)
Postfix expressions:
Algorithm using stacks
WHILE more input items exist
Get an item
IF item is an operand
stack.Push(item)
ELSE
stack.Pop(operand2)
stack.Pop(operand1)
Compute result
stack.Push(result)
stack.Pop(result)
Write the body for a function that replaces each copy of an
item in a stack with another item. Use the following
specification. (this function is a client program).
ReplaceItem(StackType& stack, ItemType oldItem,
ItemType newItem)
Function: Replaces all occurrences of oldItem with
newItem.
Precondition: stack has been initialized.
Postconditions: Each occurrence of oldItem in stack has
been replaced by newItem.
(You may use any of the member functions of the
StackType, but you may not assume any knowledge of
how the stack is implemented).
{
ItemType item;
StackType tempStack;
while (!Stack.IsEmpty()) {
Stack.Pop(item);
if (item==oldItem)
tempStack.Push(newItem);
else
tempStack.Push(item);
}
while (!tempStack.IsEmpty()) {
tempStack.Pop(item);
Stack.Push(item);
}
}
1
2
3
3
5
1
1
5
3
Stack
Stack
tempStack
oldItem = 2
newItem = 5
Exercises
• 1, 3-7, 14, 12, 15, 18, 19

More Related Content

What's hot

2CPP13 - Operator Overloading
2CPP13 - Operator Overloading2CPP13 - Operator Overloading
2CPP13 - Operator Overloading
Michael Heron
 
stack presentation
stack presentationstack presentation
Stack Data Structure
Stack Data StructureStack Data Structure
Stack Data Structure
Afaq Mansoor Khan
 
Stacks
StacksStacks
Stacks
Sadaf Ismail
 
data structure, stack, stack data structure
data structure, stack, stack data structuredata structure, stack, stack data structure
data structure, stack, stack data structure
pcnmtutorials
 
stack and queue array implementation in java.
stack and queue array implementation in java.stack and queue array implementation in java.
stack and queue array implementation in java.
CIIT Atd.
 
Queue Data Structure
Queue Data StructureQueue Data Structure
Queue Data Structure
Lovely Professional University
 
Stack
StackStack
STACK ( LIFO STRUCTURE) - Data Structure
STACK ( LIFO STRUCTURE) - Data StructureSTACK ( LIFO STRUCTURE) - Data Structure
STACK ( LIFO STRUCTURE) - Data Structure
Yaksh Jethva
 
Stack a Data Structure
Stack a Data StructureStack a Data Structure
Stack a Data Structure
ForwardBlog Enewzletter
 
Priority Queue
Priority QueuePriority Queue
Priority Queue
Joyjit Choudhury
 
Queue
QueueQueue
basics of queues
basics of queuesbasics of queues
basics of queues
sirmanohar
 
Lists
ListsLists
Learning Web Development with Django - Templates
Learning Web Development with Django - TemplatesLearning Web Development with Django - Templates
Learning Web Development with Django - Templates
Hsuan-Wen Liu
 
Queues
QueuesQueues
Ppt presentation of queues
Ppt presentation of queuesPpt presentation of queues
Ppt presentation of queues
Buxoo Abdullah
 
Queue as data_structure
Queue as data_structureQueue as data_structure
Queue as data_structure
eShikshak
 
Python programming
Python programmingPython programming
Python programming
sirikeshava
 
Queues
QueuesQueues

What's hot (20)

2CPP13 - Operator Overloading
2CPP13 - Operator Overloading2CPP13 - Operator Overloading
2CPP13 - Operator Overloading
 
stack presentation
stack presentationstack presentation
stack presentation
 
Stack Data Structure
Stack Data StructureStack Data Structure
Stack Data Structure
 
Stacks
StacksStacks
Stacks
 
data structure, stack, stack data structure
data structure, stack, stack data structuredata structure, stack, stack data structure
data structure, stack, stack data structure
 
stack and queue array implementation in java.
stack and queue array implementation in java.stack and queue array implementation in java.
stack and queue array implementation in java.
 
Queue Data Structure
Queue Data StructureQueue Data Structure
Queue Data Structure
 
Stack
StackStack
Stack
 
STACK ( LIFO STRUCTURE) - Data Structure
STACK ( LIFO STRUCTURE) - Data StructureSTACK ( LIFO STRUCTURE) - Data Structure
STACK ( LIFO STRUCTURE) - Data Structure
 
Stack a Data Structure
Stack a Data StructureStack a Data Structure
Stack a Data Structure
 
Priority Queue
Priority QueuePriority Queue
Priority Queue
 
Queue
QueueQueue
Queue
 
basics of queues
basics of queuesbasics of queues
basics of queues
 
Lists
ListsLists
Lists
 
Learning Web Development with Django - Templates
Learning Web Development with Django - TemplatesLearning Web Development with Django - Templates
Learning Web Development with Django - Templates
 
Queues
QueuesQueues
Queues
 
Ppt presentation of queues
Ppt presentation of queuesPpt presentation of queues
Ppt presentation of queues
 
Queue as data_structure
Queue as data_structureQueue as data_structure
Queue as data_structure
 
Python programming
Python programmingPython programming
Python programming
 
Queues
QueuesQueues
Queues
 

Similar to Stacks

stack in java data structure - muhammed .ppt
stack in java data structure - muhammed .pptstack in java data structure - muhammed .ppt
stack in java data structure - muhammed .ppt
ssusere3b1a2
 
stack in java ds - muhammed sdasdasdasdasdas.ppt
stack in java ds - muhammed sdasdasdasdasdas.pptstack in java ds - muhammed sdasdasdasdasdas.ppt
stack in java ds - muhammed sdasdasdasdasdas.ppt
ssusere3b1a2
 
Queues.ppt
Queues.pptQueues.ppt
Queues.ppt
ssuserff72e4
 
Queues.ppt
Queues.pptQueues.ppt
Queues.ppt
ssuserff72e4
 
QueuYUGHIJLK;KJHGFCFYUGIHOJUHYGHJIOIHGes.ppt
QueuYUGHIJLK;KJHGFCFYUGIHOJUHYGHJIOIHGes.pptQueuYUGHIJLK;KJHGFCFYUGIHOJUHYGHJIOIHGes.ppt
QueuYUGHIJLK;KJHGFCFYUGIHOJUHYGHJIOIHGes.ppt
GanaviBY
 
Queues.ppt
Queues.pptQueues.ppt
Queues.ppt
MeghadriGhosh4
 
LectureNotes-06-DSA
LectureNotes-06-DSALectureNotes-06-DSA
LectureNotes-06-DSA
Haitham El-Ghareeb
 
Queues.ppt
Queues.pptQueues.ppt
Queues.ppt
soniya555961
 
Stacks
StacksStacks
Stacks
sweta dargad
 
9 python data structure-2
9 python data structure-29 python data structure-2
9 python data structure-2
Prof. Dr. K. Adisesha
 
Data structures and algorithms lab3
Data structures and algorithms lab3Data structures and algorithms lab3
Data structures and algorithms lab3
Bianca Teşilă
 
Ch03_stacks_and_queues.ppt
Ch03_stacks_and_queues.pptCh03_stacks_and_queues.ppt
Ch03_stacks_and_queues.ppt
OliverKane3
 
(674335607) cs2309 java-lab-manual
(674335607) cs2309 java-lab-manual(674335607) cs2309 java-lab-manual
(674335607) cs2309 java-lab-manual
Chandrapriya Jayabal
 
강의자료8
강의자료8강의자료8
강의자료8
Young Wook Kim
 
Stacks
StacksStacks
CD3291 2.5 stack.pptx
CD3291 2.5 stack.pptxCD3291 2.5 stack.pptx
CD3291 2.5 stack.pptx
mareeswari15
 
Chapter 5 Stack and Queue.pdf
Chapter 5 Stack and Queue.pdfChapter 5 Stack and Queue.pdf
Chapter 5 Stack and Queue.pdf
GirT2
 
Stack and its applications
Stack and its applicationsStack and its applications
Stack and its applications
Ahsan Mansiv
 
Given the following ADT definition of a stack to use stack .docx
Given the following ADT definition of a stack to use stack .docxGiven the following ADT definition of a stack to use stack .docx
Given the following ADT definition of a stack to use stack .docx
shericehewat
 
02 stackqueue
02 stackqueue02 stackqueue
02 stackqueue
Chandan Kumar
 

Similar to Stacks (20)

stack in java data structure - muhammed .ppt
stack in java data structure - muhammed .pptstack in java data structure - muhammed .ppt
stack in java data structure - muhammed .ppt
 
stack in java ds - muhammed sdasdasdasdasdas.ppt
stack in java ds - muhammed sdasdasdasdasdas.pptstack in java ds - muhammed sdasdasdasdasdas.ppt
stack in java ds - muhammed sdasdasdasdasdas.ppt
 
Queues.ppt
Queues.pptQueues.ppt
Queues.ppt
 
Queues.ppt
Queues.pptQueues.ppt
Queues.ppt
 
QueuYUGHIJLK;KJHGFCFYUGIHOJUHYGHJIOIHGes.ppt
QueuYUGHIJLK;KJHGFCFYUGIHOJUHYGHJIOIHGes.pptQueuYUGHIJLK;KJHGFCFYUGIHOJUHYGHJIOIHGes.ppt
QueuYUGHIJLK;KJHGFCFYUGIHOJUHYGHJIOIHGes.ppt
 
Queues.ppt
Queues.pptQueues.ppt
Queues.ppt
 
LectureNotes-06-DSA
LectureNotes-06-DSALectureNotes-06-DSA
LectureNotes-06-DSA
 
Queues.ppt
Queues.pptQueues.ppt
Queues.ppt
 
Stacks
StacksStacks
Stacks
 
9 python data structure-2
9 python data structure-29 python data structure-2
9 python data structure-2
 
Data structures and algorithms lab3
Data structures and algorithms lab3Data structures and algorithms lab3
Data structures and algorithms lab3
 
Ch03_stacks_and_queues.ppt
Ch03_stacks_and_queues.pptCh03_stacks_and_queues.ppt
Ch03_stacks_and_queues.ppt
 
(674335607) cs2309 java-lab-manual
(674335607) cs2309 java-lab-manual(674335607) cs2309 java-lab-manual
(674335607) cs2309 java-lab-manual
 
강의자료8
강의자료8강의자료8
강의자료8
 
Stacks
StacksStacks
Stacks
 
CD3291 2.5 stack.pptx
CD3291 2.5 stack.pptxCD3291 2.5 stack.pptx
CD3291 2.5 stack.pptx
 
Chapter 5 Stack and Queue.pdf
Chapter 5 Stack and Queue.pdfChapter 5 Stack and Queue.pdf
Chapter 5 Stack and Queue.pdf
 
Stack and its applications
Stack and its applicationsStack and its applications
Stack and its applications
 
Given the following ADT definition of a stack to use stack .docx
Given the following ADT definition of a stack to use stack .docxGiven the following ADT definition of a stack to use stack .docx
Given the following ADT definition of a stack to use stack .docx
 
02 stackqueue
02 stackqueue02 stackqueue
02 stackqueue
 

Recently uploaded

一比一原版南十字星大学毕业证(SCU毕业证书)学历如何办理
一比一原版南十字星大学毕业证(SCU毕业证书)学历如何办理一比一原版南十字星大学毕业证(SCU毕业证书)学历如何办理
一比一原版南十字星大学毕业证(SCU毕业证书)学历如何办理
slg6lamcq
 
"Financial Odyssey: Navigating Past Performance Through Diverse Analytical Lens"
"Financial Odyssey: Navigating Past Performance Through Diverse Analytical Lens""Financial Odyssey: Navigating Past Performance Through Diverse Analytical Lens"
"Financial Odyssey: Navigating Past Performance Through Diverse Analytical Lens"
sameer shah
 
Build applications with generative AI on Google Cloud
Build applications with generative AI on Google CloudBuild applications with generative AI on Google Cloud
Build applications with generative AI on Google Cloud
Márton Kodok
 
writing report business partner b1+ .pdf
writing report business partner b1+ .pdfwriting report business partner b1+ .pdf
writing report business partner b1+ .pdf
VyNguyen709676
 
Orchestrating the Future: Navigating Today's Data Workflow Challenges with Ai...
Orchestrating the Future: Navigating Today's Data Workflow Challenges with Ai...Orchestrating the Future: Navigating Today's Data Workflow Challenges with Ai...
Orchestrating the Future: Navigating Today's Data Workflow Challenges with Ai...
Kaxil Naik
 
Population Growth in Bataan: The effects of population growth around rural pl...
Population Growth in Bataan: The effects of population growth around rural pl...Population Growth in Bataan: The effects of population growth around rural pl...
Population Growth in Bataan: The effects of population growth around rural pl...
Bill641377
 
Challenges of Nation Building-1.pptx with more important
Challenges of Nation Building-1.pptx with more importantChallenges of Nation Building-1.pptx with more important
Challenges of Nation Building-1.pptx with more important
Sm321
 
Experts live - Improving user adoption with AI
Experts live - Improving user adoption with AIExperts live - Improving user adoption with AI
Experts live - Improving user adoption with AI
jitskeb
 
一比一原版(GWU,GW文凭证书)乔治·华盛顿大学毕业证如何办理
一比一原版(GWU,GW文凭证书)乔治·华盛顿大学毕业证如何办理一比一原版(GWU,GW文凭证书)乔治·华盛顿大学毕业证如何办理
一比一原版(GWU,GW文凭证书)乔治·华盛顿大学毕业证如何办理
bopyb
 
原版一比一弗林德斯大学毕业证(Flinders毕业证书)如何办理
原版一比一弗林德斯大学毕业证(Flinders毕业证书)如何办理原版一比一弗林德斯大学毕业证(Flinders毕业证书)如何办理
原版一比一弗林德斯大学毕业证(Flinders毕业证书)如何办理
a9qfiubqu
 
Predictably Improve Your B2B Tech Company's Performance by Leveraging Data
Predictably Improve Your B2B Tech Company's Performance by Leveraging DataPredictably Improve Your B2B Tech Company's Performance by Leveraging Data
Predictably Improve Your B2B Tech Company's Performance by Leveraging Data
Kiwi Creative
 
Module 1 ppt BIG DATA ANALYTICS_NOTES FOR MCA
Module 1 ppt BIG DATA ANALYTICS_NOTES FOR MCAModule 1 ppt BIG DATA ANALYTICS_NOTES FOR MCA
Module 1 ppt BIG DATA ANALYTICS_NOTES FOR MCA
yuvarajkumar334
 
一比一原版巴斯大学毕业证(Bath毕业证书)学历如何办理
一比一原版巴斯大学毕业证(Bath毕业证书)学历如何办理一比一原版巴斯大学毕业证(Bath毕业证书)学历如何办理
一比一原版巴斯大学毕业证(Bath毕业证书)学历如何办理
y3i0qsdzb
 
办(uts毕业证书)悉尼科技大学毕业证学历证书原版一模一样
办(uts毕业证书)悉尼科技大学毕业证学历证书原版一模一样办(uts毕业证书)悉尼科技大学毕业证学历证书原版一模一样
办(uts毕业证书)悉尼科技大学毕业证学历证书原版一模一样
apvysm8
 
STATATHON: Unleashing the Power of Statistics in a 48-Hour Knowledge Extravag...
STATATHON: Unleashing the Power of Statistics in a 48-Hour Knowledge Extravag...STATATHON: Unleashing the Power of Statistics in a 48-Hour Knowledge Extravag...
STATATHON: Unleashing the Power of Statistics in a 48-Hour Knowledge Extravag...
sameer shah
 
University of New South Wales degree offer diploma Transcript
University of New South Wales degree offer diploma TranscriptUniversity of New South Wales degree offer diploma Transcript
University of New South Wales degree offer diploma Transcript
soxrziqu
 
DATA COMMS-NETWORKS YR2 lecture 08 NAT & CLOUD.docx
DATA COMMS-NETWORKS YR2 lecture 08 NAT & CLOUD.docxDATA COMMS-NETWORKS YR2 lecture 08 NAT & CLOUD.docx
DATA COMMS-NETWORKS YR2 lecture 08 NAT & CLOUD.docx
SaffaIbrahim1
 
一比一原版(UMN文凭证书)明尼苏达大学毕业证如何办理
一比一原版(UMN文凭证书)明尼苏达大学毕业证如何办理一比一原版(UMN文凭证书)明尼苏达大学毕业证如何办理
一比一原版(UMN文凭证书)明尼苏达大学毕业证如何办理
nyfuhyz
 
一比一原版(UO毕业证)渥太华大学毕业证如何办理
一比一原版(UO毕业证)渥太华大学毕业证如何办理一比一原版(UO毕业证)渥太华大学毕业证如何办理
一比一原版(UO毕业证)渥太华大学毕业证如何办理
bmucuha
 
在线办理(英国UCA毕业证书)创意艺术大学毕业证在读证明一模一样
在线办理(英国UCA毕业证书)创意艺术大学毕业证在读证明一模一样在线办理(英国UCA毕业证书)创意艺术大学毕业证在读证明一模一样
在线办理(英国UCA毕业证书)创意艺术大学毕业证在读证明一模一样
v7oacc3l
 

Recently uploaded (20)

一比一原版南十字星大学毕业证(SCU毕业证书)学历如何办理
一比一原版南十字星大学毕业证(SCU毕业证书)学历如何办理一比一原版南十字星大学毕业证(SCU毕业证书)学历如何办理
一比一原版南十字星大学毕业证(SCU毕业证书)学历如何办理
 
"Financial Odyssey: Navigating Past Performance Through Diverse Analytical Lens"
"Financial Odyssey: Navigating Past Performance Through Diverse Analytical Lens""Financial Odyssey: Navigating Past Performance Through Diverse Analytical Lens"
"Financial Odyssey: Navigating Past Performance Through Diverse Analytical Lens"
 
Build applications with generative AI on Google Cloud
Build applications with generative AI on Google CloudBuild applications with generative AI on Google Cloud
Build applications with generative AI on Google Cloud
 
writing report business partner b1+ .pdf
writing report business partner b1+ .pdfwriting report business partner b1+ .pdf
writing report business partner b1+ .pdf
 
Orchestrating the Future: Navigating Today's Data Workflow Challenges with Ai...
Orchestrating the Future: Navigating Today's Data Workflow Challenges with Ai...Orchestrating the Future: Navigating Today's Data Workflow Challenges with Ai...
Orchestrating the Future: Navigating Today's Data Workflow Challenges with Ai...
 
Population Growth in Bataan: The effects of population growth around rural pl...
Population Growth in Bataan: The effects of population growth around rural pl...Population Growth in Bataan: The effects of population growth around rural pl...
Population Growth in Bataan: The effects of population growth around rural pl...
 
Challenges of Nation Building-1.pptx with more important
Challenges of Nation Building-1.pptx with more importantChallenges of Nation Building-1.pptx with more important
Challenges of Nation Building-1.pptx with more important
 
Experts live - Improving user adoption with AI
Experts live - Improving user adoption with AIExperts live - Improving user adoption with AI
Experts live - Improving user adoption with AI
 
一比一原版(GWU,GW文凭证书)乔治·华盛顿大学毕业证如何办理
一比一原版(GWU,GW文凭证书)乔治·华盛顿大学毕业证如何办理一比一原版(GWU,GW文凭证书)乔治·华盛顿大学毕业证如何办理
一比一原版(GWU,GW文凭证书)乔治·华盛顿大学毕业证如何办理
 
原版一比一弗林德斯大学毕业证(Flinders毕业证书)如何办理
原版一比一弗林德斯大学毕业证(Flinders毕业证书)如何办理原版一比一弗林德斯大学毕业证(Flinders毕业证书)如何办理
原版一比一弗林德斯大学毕业证(Flinders毕业证书)如何办理
 
Predictably Improve Your B2B Tech Company's Performance by Leveraging Data
Predictably Improve Your B2B Tech Company's Performance by Leveraging DataPredictably Improve Your B2B Tech Company's Performance by Leveraging Data
Predictably Improve Your B2B Tech Company's Performance by Leveraging Data
 
Module 1 ppt BIG DATA ANALYTICS_NOTES FOR MCA
Module 1 ppt BIG DATA ANALYTICS_NOTES FOR MCAModule 1 ppt BIG DATA ANALYTICS_NOTES FOR MCA
Module 1 ppt BIG DATA ANALYTICS_NOTES FOR MCA
 
一比一原版巴斯大学毕业证(Bath毕业证书)学历如何办理
一比一原版巴斯大学毕业证(Bath毕业证书)学历如何办理一比一原版巴斯大学毕业证(Bath毕业证书)学历如何办理
一比一原版巴斯大学毕业证(Bath毕业证书)学历如何办理
 
办(uts毕业证书)悉尼科技大学毕业证学历证书原版一模一样
办(uts毕业证书)悉尼科技大学毕业证学历证书原版一模一样办(uts毕业证书)悉尼科技大学毕业证学历证书原版一模一样
办(uts毕业证书)悉尼科技大学毕业证学历证书原版一模一样
 
STATATHON: Unleashing the Power of Statistics in a 48-Hour Knowledge Extravag...
STATATHON: Unleashing the Power of Statistics in a 48-Hour Knowledge Extravag...STATATHON: Unleashing the Power of Statistics in a 48-Hour Knowledge Extravag...
STATATHON: Unleashing the Power of Statistics in a 48-Hour Knowledge Extravag...
 
University of New South Wales degree offer diploma Transcript
University of New South Wales degree offer diploma TranscriptUniversity of New South Wales degree offer diploma Transcript
University of New South Wales degree offer diploma Transcript
 
DATA COMMS-NETWORKS YR2 lecture 08 NAT & CLOUD.docx
DATA COMMS-NETWORKS YR2 lecture 08 NAT & CLOUD.docxDATA COMMS-NETWORKS YR2 lecture 08 NAT & CLOUD.docx
DATA COMMS-NETWORKS YR2 lecture 08 NAT & CLOUD.docx
 
一比一原版(UMN文凭证书)明尼苏达大学毕业证如何办理
一比一原版(UMN文凭证书)明尼苏达大学毕业证如何办理一比一原版(UMN文凭证书)明尼苏达大学毕业证如何办理
一比一原版(UMN文凭证书)明尼苏达大学毕业证如何办理
 
一比一原版(UO毕业证)渥太华大学毕业证如何办理
一比一原版(UO毕业证)渥太华大学毕业证如何办理一比一原版(UO毕业证)渥太华大学毕业证如何办理
一比一原版(UO毕业证)渥太华大学毕业证如何办理
 
在线办理(英国UCA毕业证书)创意艺术大学毕业证在读证明一模一样
在线办理(英国UCA毕业证书)创意艺术大学毕业证在读证明一模一样在线办理(英国UCA毕业证书)创意艺术大学毕业证在读证明一模一样
在线办理(英国UCA毕业证书)创意艺术大学毕业证在读证明一模一样
 

Stacks

  • 1. Stacks CS 308 – Data Structures
  • 2. What is a stack? • It is an ordered group of homogeneous items of elements. • Elements are added to and removed from the top of the stack (the most recently added items are at the top of the stack). • The last element to be added is the first to be removed (LIFO: Last In, First Out).
  • 3. Stack Specification • Definitions: (provided by the user) – MAX_ITEMS: Max number of items that might be on the stack – ItemType: Data type of the items on the stack • Operations – MakeEmpty – Boolean IsEmpty – Boolean IsFull – Push (ItemType newItem) – Pop (ItemType& item)
  • 4. Push (ItemType newItem) • Function: Adds newItem to the top of the stack. • Preconditions: Stack has been initialized and is not full. • Postconditions: newItem is at the top of the stack.
  • 5. Pop (ItemType& item) • Function: Removes topItem from stack and returns it in item. • Preconditions: Stack has been initialized and is not empty. • Postconditions: Top element has been removed from stack and item is a copy of the removed element.
  • 6.
  • 7. Stack Implementation #include "ItemType.h" // Must be provided by the user of the class // Contains definitions for MAX_ITEMS and ItemType class StackType { public: StackType(); void MakeEmpty(); bool IsEmpty() const; bool IsFull() const; void Push(ItemType); void Pop(ItemType&); private: int top; ItemType items[MAX_ITEMS]; };
  • 8. Stack Implementation (cont.) StackType::StackType() { top = -1; } void StackType::MakeEmpty() { top = -1; } bool StackType::IsEmpty() const { return (top == -1); }
  • 9. Stack Implementation (cont.) bool StackType::IsFull() const { return (top == MAX_ITEMS-1); } void StackType::Push(ItemType newItem) { top++; items[top] = newItem; } void StackType::Pop(ItemType& item) { item = items[top]; top--; }
  • 10. Stack overflow • The condition resulting from trying to push an element onto a full stack. if(!stack.IsFull()) stack.Push(item); Stack underflow • The condition resulting from trying to pop an empty stack. if(!stack.IsEmpty()) stack.Pop(item);
  • 11. Implementing stacks using templates • Templates allow the compiler to generate multiple versions of a class type or a function by allowing parameterized types. • It is similar to passing a parameter to a function (we pass a data type to a class !!)
  • 12. Implementing stacks using templates template<class ItemType> class StackType { public: StackType(); void MakeEmpty(); bool IsEmpty() const; bool IsFull() const; void Push(ItemType); void Pop(ItemType&); private: int top; ItemType items[MAX_ITEMS]; }; (cont.)
  • 13. Example using templates // Client code StackType<int> myStack; StackType<float> yourStack; StackType<StrType> anotherStack; myStack.Push(35); yourStack.Push(584.39); The compiler generates distinct class types and gives its own internal name to each of the types.
  • 14. Function templates • The definitions of the member functions must be rewritten as function templates. template<class ItemType> StackType<ItemType>::StackType() { top = -1; } template<class ItemType> void StackType<ItemType>::MakeEmpty() { top = -1; }
  • 15. Function templates (cont.) template<class ItemType> bool StackType<ItemType>::IsEmpty() const { return (top == -1); } template<class ItemType> bool StackType<ItemType>::IsFull() const { return (top == MAX_ITEMS-1); } template<class ItemType> void StackType<ItemType>::Push(ItemType newItem) { top++; items[top] = newItem; }
  • 16. Function templates (cont.) template<class ItemType> void StackType<ItemType>::Pop(ItemType& item) { item = items[top]; top--; }
  • 17. Comments using templates • The template<class T> designation must precede the class method name in the source code for each template class method. • The word class is required by the syntax of the language and does not mean that the actual parameter must be the name of a class. • Passing a parameter to a template has an effect at compile time.
  • 18. Implementing stacks using dynamic array allocation template<class ItemType> class StackType { public: StackType(int); ~StackType(); void MakeEmpty(); bool IsEmpty() const; bool IsFull() const; void Push(ItemType); void Pop(ItemType&); private: int top; int maxStack; ItemType *items; };
  • 19. Implementing stacks using dynamic array allocation (cont.) template<class ItemType> StackType<ItemType>::StackType(int max) { maxStack = max; top = -1; items = new ItemType[max]; } template<class ItemType> StackType<ItemType>::~StackType() { delete [ ] items; }
  • 20. Example: postfix expressions • Postfix notation is another way of writing arithmetic expressions. • In postfix notation, the operator is written after the two operands. infix: 2+5 postfix: 2 5 + • Expressions are evaluated from left to right. • Precedence rules and parentheses are never needed!!
  • 23. Postfix expressions: Algorithm using stacks WHILE more input items exist Get an item IF item is an operand stack.Push(item) ELSE stack.Pop(operand2) stack.Pop(operand1) Compute result stack.Push(result) stack.Pop(result)
  • 24. Write the body for a function that replaces each copy of an item in a stack with another item. Use the following specification. (this function is a client program). ReplaceItem(StackType& stack, ItemType oldItem, ItemType newItem) Function: Replaces all occurrences of oldItem with newItem. Precondition: stack has been initialized. Postconditions: Each occurrence of oldItem in stack has been replaced by newItem. (You may use any of the member functions of the StackType, but you may not assume any knowledge of how the stack is implemented).
  • 25. { ItemType item; StackType tempStack; while (!Stack.IsEmpty()) { Stack.Pop(item); if (item==oldItem) tempStack.Push(newItem); else tempStack.Push(item); } while (!tempStack.IsEmpty()) { tempStack.Pop(item); Stack.Push(item); } } 1 2 3 3 5 1 1 5 3 Stack Stack tempStack oldItem = 2 newItem = 5
  • 26. Exercises • 1, 3-7, 14, 12, 15, 18, 19