SlideShare a Scribd company logo
STACKS - JAVA
MUHAMMED SSSS PTUK
WHAT IS A STACK IN JAVAIN
JAVA?
• It is an ordered group of homogeneous items of
elements.
• Elements are added to and removed from the top
of the Stack in java(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 IN JAVASPECIFICATION
• 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 in javahas
been initialized and is not full.
•Postconditions: newItem is at the
top of the stack.
POP (ITEMTYPE& ITEM)
• Function: Removes topItem from Stack in
javaand returns it in item.
• Preconditions: Stack in javahas been initialized
and is not empty.
• Postconditions: Top element has been
removed from Stack in javaand item is a copy
of the removed element.
stack in java data structure - muhammed .ppt
STACK IN JAVAIMPLEMENTATION
#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 IN JAVAIMPLEMENTATION
(CONT.)
StackType::StackType()
{
top = -1;
}
void StackType::MakeEmpty()
{
top = -1;
}
bool StackType::IsEmpty() const
{
return (top == -1);
}
STACK IN JAVAIMPLEMENTATION
(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 in javaoverflow
• The condition resulting from trying to push an
element onto a full stack.
if(!stack.IsFull())
stack.Push(item);
Stack in javaunderflow
• 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 in java= 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 in javawith 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 in javahas been initialized.
Postconditions: Each occurrence of oldItem in
Stack in javahas 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 in javais
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
Ad

More Related Content

Similar to stack in java data structure - muhammed .ppt (20)

Data structures and algorithms lab3
Data structures and algorithms lab3Data structures and algorithms lab3
Data structures and algorithms lab3
Bianca Teşilă
 
Stack Implementation
Stack ImplementationStack Implementation
Stack Implementation
Zidny Nafan
 
(674335607) cs2309 java-lab-manual
(674335607) cs2309 java-lab-manual(674335607) cs2309 java-lab-manual
(674335607) cs2309 java-lab-manual
Chandrapriya Jayabal
 
stack presentation
stack presentationstack presentation
stack presentation
Shivalik college of engineering
 
강의자료8
강의자료8강의자료8
강의자료8
Young Wook Kim
 
Stack in Sata Structure
Stack in Sata StructureStack in Sata Structure
Stack in Sata Structure
Muhazzab Chouhadry
 
CD3291 2.5 stack.pptx
CD3291 2.5 stack.pptxCD3291 2.5 stack.pptx
CD3291 2.5 stack.pptx
mareeswari15
 
9 python data structure-2
9 python data structure-29 python data structure-2
9 python data structure-2
Prof. Dr. K. Adisesha
 
LinkedSQ.ppt, Stack and Queue implementation using array
LinkedSQ.ppt, Stack and Queue implementation using arrayLinkedSQ.ppt, Stack and Queue implementation using array
LinkedSQ.ppt, Stack and Queue implementation using array
maitypradip938
 
Extending javascript part one
Extending javascript part oneExtending javascript part one
Extending javascript part one
Vijaya Anand
 
Ch03_stacks_and_queues.ppt
Ch03_stacks_and_queues.pptCh03_stacks_and_queues.ppt
Ch03_stacks_and_queues.ppt
OliverKane3
 
02 stackqueue
02 stackqueue02 stackqueue
02 stackqueue
Chandan Kumar
 
stack_presentaton_HUSNAIN[2].pojklklklptx
stack_presentaton_HUSNAIN[2].pojklklklptxstack_presentaton_HUSNAIN[2].pojklklklptx
stack_presentaton_HUSNAIN[2].pojklklklptx
HusnainNaqvi2
 
Stacks in data structure
Stacks  in data structureStacks  in data structure
Stacks in data structure
lodhran-hayat
 
Stacks
StacksStacks
Stacks
Lovely Professional University
 
Stacks in Data Structure
Stacks in Data StructureStacks in Data Structure
Stacks in Data Structure
Lovely Professional University
 
Stacks
StacksStacks
Stacks
sweta dargad
 
Queues.ppt
Queues.pptQueues.ppt
Queues.ppt
soniya555961
 
Queues.ppt
Queues.pptQueues.ppt
Queues.ppt
ssuserff72e4
 
Queues.ppt
Queues.pptQueues.ppt
Queues.ppt
ssuserff72e4
 

More from ssusere3b1a2 (14)

HTML-EVENTsssssssssssssssssssssssssS.pptx
HTML-EVENTsssssssssssssssssssssssssS.pptxHTML-EVENTsssssssssssssssssssssssssS.pptx
HTML-EVENTsssssssssssssssssssssssssS.pptx
ssusere3b1a2
 
Operators, Strings and Math built-in functions.pdf
Operators, Strings and Math built-in functions.pdfOperators, Strings and Math built-in functions.pdf
Operators, Strings and Math built-in functions.pdf
ssusere3b1a2
 
Introduction_to_Algorithms---------.pptx
Introduction_to_Algorithms---------.pptxIntroduction_to_Algorithms---------.pptx
Introduction_to_Algorithms---------.pptx
ssusere3b1a2
 
introductionintroductionintroductionintroduction.ppt
introductionintroductionintroductionintroduction.pptintroductionintroductionintroductionintroduction.ppt
introductionintroductionintroductionintroduction.ppt
ssusere3b1a2
 
Course_Weekly_Breakdown-algorithem-.pptx
Course_Weekly_Breakdown-algorithem-.pptxCourse_Weekly_Breakdown-algorithem-.pptx
Course_Weekly_Breakdown-algorithem-.pptx
ssusere3b1a2
 
week9-prefixinfixandpostfixnotations-191013065821.pptx
week9-prefixinfixandpostfixnotations-191013065821.pptxweek9-prefixinfixandpostfixnotations-191013065821.pptx
week9-prefixinfixandpostfixnotations-191013065821.pptx
ssusere3b1a2
 
13-Doubly Linked List data structure.pdf
13-Doubly Linked List data structure.pdf13-Doubly Linked List data structure.pdf
13-Doubly Linked List data structure.pdf
ssusere3b1a2
 
Hashing data structure in genaral ss.ppt
Hashing data structure in genaral ss.pptHashing data structure in genaral ss.ppt
Hashing data structure in genaral ss.ppt
ssusere3b1a2
 
slide6.pptx
slide6.pptxslide6.pptx
slide6.pptx
ssusere3b1a2
 
Chapter 6 Methods.pptx
Chapter 6 Methods.pptxChapter 6 Methods.pptx
Chapter 6 Methods.pptx
ssusere3b1a2
 
Chapter 4 Mathematical Functions, Characters, and Strings.pptx
Chapter 4 Mathematical Functions, Characters, and Strings.pptxChapter 4 Mathematical Functions, Characters, and Strings.pptx
Chapter 4 Mathematical Functions, Characters, and Strings.pptx
ssusere3b1a2
 
information retrieval project
information retrieval projectinformation retrieval project
information retrieval project
ssusere3b1a2
 
information retrieval --> dictionary.ppt
information retrieval --> dictionary.pptinformation retrieval --> dictionary.ppt
information retrieval --> dictionary.ppt
ssusere3b1a2
 
introduction into IR
introduction into IRintroduction into IR
introduction into IR
ssusere3b1a2
 
HTML-EVENTsssssssssssssssssssssssssS.pptx
HTML-EVENTsssssssssssssssssssssssssS.pptxHTML-EVENTsssssssssssssssssssssssssS.pptx
HTML-EVENTsssssssssssssssssssssssssS.pptx
ssusere3b1a2
 
Operators, Strings and Math built-in functions.pdf
Operators, Strings and Math built-in functions.pdfOperators, Strings and Math built-in functions.pdf
Operators, Strings and Math built-in functions.pdf
ssusere3b1a2
 
Introduction_to_Algorithms---------.pptx
Introduction_to_Algorithms---------.pptxIntroduction_to_Algorithms---------.pptx
Introduction_to_Algorithms---------.pptx
ssusere3b1a2
 
introductionintroductionintroductionintroduction.ppt
introductionintroductionintroductionintroduction.pptintroductionintroductionintroductionintroduction.ppt
introductionintroductionintroductionintroduction.ppt
ssusere3b1a2
 
Course_Weekly_Breakdown-algorithem-.pptx
Course_Weekly_Breakdown-algorithem-.pptxCourse_Weekly_Breakdown-algorithem-.pptx
Course_Weekly_Breakdown-algorithem-.pptx
ssusere3b1a2
 
week9-prefixinfixandpostfixnotations-191013065821.pptx
week9-prefixinfixandpostfixnotations-191013065821.pptxweek9-prefixinfixandpostfixnotations-191013065821.pptx
week9-prefixinfixandpostfixnotations-191013065821.pptx
ssusere3b1a2
 
13-Doubly Linked List data structure.pdf
13-Doubly Linked List data structure.pdf13-Doubly Linked List data structure.pdf
13-Doubly Linked List data structure.pdf
ssusere3b1a2
 
Hashing data structure in genaral ss.ppt
Hashing data structure in genaral ss.pptHashing data structure in genaral ss.ppt
Hashing data structure in genaral ss.ppt
ssusere3b1a2
 
Chapter 6 Methods.pptx
Chapter 6 Methods.pptxChapter 6 Methods.pptx
Chapter 6 Methods.pptx
ssusere3b1a2
 
Chapter 4 Mathematical Functions, Characters, and Strings.pptx
Chapter 4 Mathematical Functions, Characters, and Strings.pptxChapter 4 Mathematical Functions, Characters, and Strings.pptx
Chapter 4 Mathematical Functions, Characters, and Strings.pptx
ssusere3b1a2
 
information retrieval project
information retrieval projectinformation retrieval project
information retrieval project
ssusere3b1a2
 
information retrieval --> dictionary.ppt
information retrieval --> dictionary.pptinformation retrieval --> dictionary.ppt
information retrieval --> dictionary.ppt
ssusere3b1a2
 
introduction into IR
introduction into IRintroduction into IR
introduction into IR
ssusere3b1a2
 
Ad

Recently uploaded (20)

LDMMIA Reiki Yoga S5 Daily Living Workshop
LDMMIA Reiki Yoga S5 Daily Living WorkshopLDMMIA Reiki Yoga S5 Daily Living Workshop
LDMMIA Reiki Yoga S5 Daily Living Workshop
LDM Mia eStudios
 
How to Configure Scheduled Actions in odoo 18
How to Configure Scheduled Actions in odoo 18How to Configure Scheduled Actions in odoo 18
How to Configure Scheduled Actions in odoo 18
Celine George
 
PHYSIOLOGY MCQS By DR. NASIR MUSTAFA (PHYSIOLOGY)
PHYSIOLOGY MCQS By DR. NASIR MUSTAFA (PHYSIOLOGY)PHYSIOLOGY MCQS By DR. NASIR MUSTAFA (PHYSIOLOGY)
PHYSIOLOGY MCQS By DR. NASIR MUSTAFA (PHYSIOLOGY)
Dr. Nasir Mustafa
 
Computer crime and Legal issues Computer crime and Legal issues
Computer crime and Legal issues Computer crime and Legal issuesComputer crime and Legal issues Computer crime and Legal issues
Computer crime and Legal issues Computer crime and Legal issues
Abhijit Bodhe
 
spinal cord disorders (Myelopathies and radiculoapthies)
spinal cord disorders (Myelopathies and radiculoapthies)spinal cord disorders (Myelopathies and radiculoapthies)
spinal cord disorders (Myelopathies and radiculoapthies)
Mohamed Rizk Khodair
 
dynastic art of the Pallava dynasty south India
dynastic art of the Pallava dynasty south Indiadynastic art of the Pallava dynasty south India
dynastic art of the Pallava dynasty south India
PrachiSontakke5
 
Cultivation Practice of Garlic in Nepal.pptx
Cultivation Practice of Garlic in Nepal.pptxCultivation Practice of Garlic in Nepal.pptx
Cultivation Practice of Garlic in Nepal.pptx
UmeshTimilsina1
 
Rock Art As a Source of Ancient Indian History
Rock Art As a Source of Ancient Indian HistoryRock Art As a Source of Ancient Indian History
Rock Art As a Source of Ancient Indian History
Virag Sontakke
 
Link your Lead Opportunities into Spreadsheet using odoo CRM
Link your Lead Opportunities into Spreadsheet using odoo CRMLink your Lead Opportunities into Spreadsheet using odoo CRM
Link your Lead Opportunities into Spreadsheet using odoo CRM
Celine George
 
Form View Attributes in Odoo 18 - Odoo Slides
Form View Attributes in Odoo 18 - Odoo SlidesForm View Attributes in Odoo 18 - Odoo Slides
Form View Attributes in Odoo 18 - Odoo Slides
Celine George
 
How to Configure Public Holidays & Mandatory Days in Odoo 18
How to Configure Public Holidays & Mandatory Days in Odoo 18How to Configure Public Holidays & Mandatory Days in Odoo 18
How to Configure Public Holidays & Mandatory Days in Odoo 18
Celine George
 
Lecture 4 INSECT CUTICLE and moulting.pptx
Lecture 4 INSECT CUTICLE and moulting.pptxLecture 4 INSECT CUTICLE and moulting.pptx
Lecture 4 INSECT CUTICLE and moulting.pptx
Arshad Shaikh
 
Grade 2 - Mathematics - Printable Worksheet
Grade 2 - Mathematics - Printable WorksheetGrade 2 - Mathematics - Printable Worksheet
Grade 2 - Mathematics - Printable Worksheet
Sritoma Majumder
 
Sugar-Sensing Mechanism in plants....pptx
Sugar-Sensing Mechanism in plants....pptxSugar-Sensing Mechanism in plants....pptx
Sugar-Sensing Mechanism in plants....pptx
Dr. Renu Jangid
 
Herbs Used in Cosmetic Formulations .pptx
Herbs Used in Cosmetic Formulations .pptxHerbs Used in Cosmetic Formulations .pptx
Herbs Used in Cosmetic Formulations .pptx
RAJU THENGE
 
Drive Supporter Growth from Awareness to Advocacy with TechSoup Marketing Ser...
Drive Supporter Growth from Awareness to Advocacy with TechSoup Marketing Ser...Drive Supporter Growth from Awareness to Advocacy with TechSoup Marketing Ser...
Drive Supporter Growth from Awareness to Advocacy with TechSoup Marketing Ser...
TechSoup
 
Ajanta Paintings: Study as a Source of History
Ajanta Paintings: Study as a Source of HistoryAjanta Paintings: Study as a Source of History
Ajanta Paintings: Study as a Source of History
Virag Sontakke
 
How to Add Customer Note in Odoo 18 POS - Odoo Slides
How to Add Customer Note in Odoo 18 POS - Odoo SlidesHow to Add Customer Note in Odoo 18 POS - Odoo Slides
How to Add Customer Note in Odoo 18 POS - Odoo Slides
Celine George
 
03#UNTAGGED. Generosity in architecture.
03#UNTAGGED. Generosity in architecture.03#UNTAGGED. Generosity in architecture.
03#UNTAGGED. Generosity in architecture.
MCH
 
Myopathies (muscle disorders) for undergraduate
Myopathies (muscle disorders) for undergraduateMyopathies (muscle disorders) for undergraduate
Myopathies (muscle disorders) for undergraduate
Mohamed Rizk Khodair
 
LDMMIA Reiki Yoga S5 Daily Living Workshop
LDMMIA Reiki Yoga S5 Daily Living WorkshopLDMMIA Reiki Yoga S5 Daily Living Workshop
LDMMIA Reiki Yoga S5 Daily Living Workshop
LDM Mia eStudios
 
How to Configure Scheduled Actions in odoo 18
How to Configure Scheduled Actions in odoo 18How to Configure Scheduled Actions in odoo 18
How to Configure Scheduled Actions in odoo 18
Celine George
 
PHYSIOLOGY MCQS By DR. NASIR MUSTAFA (PHYSIOLOGY)
PHYSIOLOGY MCQS By DR. NASIR MUSTAFA (PHYSIOLOGY)PHYSIOLOGY MCQS By DR. NASIR MUSTAFA (PHYSIOLOGY)
PHYSIOLOGY MCQS By DR. NASIR MUSTAFA (PHYSIOLOGY)
Dr. Nasir Mustafa
 
Computer crime and Legal issues Computer crime and Legal issues
Computer crime and Legal issues Computer crime and Legal issuesComputer crime and Legal issues Computer crime and Legal issues
Computer crime and Legal issues Computer crime and Legal issues
Abhijit Bodhe
 
spinal cord disorders (Myelopathies and radiculoapthies)
spinal cord disorders (Myelopathies and radiculoapthies)spinal cord disorders (Myelopathies and radiculoapthies)
spinal cord disorders (Myelopathies and radiculoapthies)
Mohamed Rizk Khodair
 
dynastic art of the Pallava dynasty south India
dynastic art of the Pallava dynasty south Indiadynastic art of the Pallava dynasty south India
dynastic art of the Pallava dynasty south India
PrachiSontakke5
 
Cultivation Practice of Garlic in Nepal.pptx
Cultivation Practice of Garlic in Nepal.pptxCultivation Practice of Garlic in Nepal.pptx
Cultivation Practice of Garlic in Nepal.pptx
UmeshTimilsina1
 
Rock Art As a Source of Ancient Indian History
Rock Art As a Source of Ancient Indian HistoryRock Art As a Source of Ancient Indian History
Rock Art As a Source of Ancient Indian History
Virag Sontakke
 
Link your Lead Opportunities into Spreadsheet using odoo CRM
Link your Lead Opportunities into Spreadsheet using odoo CRMLink your Lead Opportunities into Spreadsheet using odoo CRM
Link your Lead Opportunities into Spreadsheet using odoo CRM
Celine George
 
Form View Attributes in Odoo 18 - Odoo Slides
Form View Attributes in Odoo 18 - Odoo SlidesForm View Attributes in Odoo 18 - Odoo Slides
Form View Attributes in Odoo 18 - Odoo Slides
Celine George
 
How to Configure Public Holidays & Mandatory Days in Odoo 18
How to Configure Public Holidays & Mandatory Days in Odoo 18How to Configure Public Holidays & Mandatory Days in Odoo 18
How to Configure Public Holidays & Mandatory Days in Odoo 18
Celine George
 
Lecture 4 INSECT CUTICLE and moulting.pptx
Lecture 4 INSECT CUTICLE and moulting.pptxLecture 4 INSECT CUTICLE and moulting.pptx
Lecture 4 INSECT CUTICLE and moulting.pptx
Arshad Shaikh
 
Grade 2 - Mathematics - Printable Worksheet
Grade 2 - Mathematics - Printable WorksheetGrade 2 - Mathematics - Printable Worksheet
Grade 2 - Mathematics - Printable Worksheet
Sritoma Majumder
 
Sugar-Sensing Mechanism in plants....pptx
Sugar-Sensing Mechanism in plants....pptxSugar-Sensing Mechanism in plants....pptx
Sugar-Sensing Mechanism in plants....pptx
Dr. Renu Jangid
 
Herbs Used in Cosmetic Formulations .pptx
Herbs Used in Cosmetic Formulations .pptxHerbs Used in Cosmetic Formulations .pptx
Herbs Used in Cosmetic Formulations .pptx
RAJU THENGE
 
Drive Supporter Growth from Awareness to Advocacy with TechSoup Marketing Ser...
Drive Supporter Growth from Awareness to Advocacy with TechSoup Marketing Ser...Drive Supporter Growth from Awareness to Advocacy with TechSoup Marketing Ser...
Drive Supporter Growth from Awareness to Advocacy with TechSoup Marketing Ser...
TechSoup
 
Ajanta Paintings: Study as a Source of History
Ajanta Paintings: Study as a Source of HistoryAjanta Paintings: Study as a Source of History
Ajanta Paintings: Study as a Source of History
Virag Sontakke
 
How to Add Customer Note in Odoo 18 POS - Odoo Slides
How to Add Customer Note in Odoo 18 POS - Odoo SlidesHow to Add Customer Note in Odoo 18 POS - Odoo Slides
How to Add Customer Note in Odoo 18 POS - Odoo Slides
Celine George
 
03#UNTAGGED. Generosity in architecture.
03#UNTAGGED. Generosity in architecture.03#UNTAGGED. Generosity in architecture.
03#UNTAGGED. Generosity in architecture.
MCH
 
Myopathies (muscle disorders) for undergraduate
Myopathies (muscle disorders) for undergraduateMyopathies (muscle disorders) for undergraduate
Myopathies (muscle disorders) for undergraduate
Mohamed Rizk Khodair
 
Ad

stack in java data structure - muhammed .ppt

  • 2. WHAT IS A STACK IN JAVAIN JAVA? • It is an ordered group of homogeneous items of elements. • Elements are added to and removed from the top of the Stack in java(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 IN JAVASPECIFICATION • 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 in javahas been initialized and is not full. •Postconditions: newItem is at the top of the stack.
  • 5. POP (ITEMTYPE& ITEM) • Function: Removes topItem from Stack in javaand returns it in item. • Preconditions: Stack in javahas been initialized and is not empty. • Postconditions: Top element has been removed from Stack in javaand item is a copy of the removed element.
  • 7. STACK IN JAVAIMPLEMENTATION #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 IN JAVAIMPLEMENTATION (CONT.) StackType::StackType() { top = -1; } void StackType::MakeEmpty() { top = -1; } bool StackType::IsEmpty() const { return (top == -1); }
  • 9. STACK IN JAVAIMPLEMENTATION (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 in javaoverflow • The condition resulting from trying to push an element onto a full stack. if(!stack.IsFull()) stack.Push(item); Stack in javaunderflow • 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 in java= 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 in javawith 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 in javahas been initialized. Postconditions: Each occurrence of oldItem in Stack in javahas 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 in javais 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