SlideShare a Scribd company logo
1 of 26
Stacks - java
Muhammed ssss PTUK
What is a Stackin 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).
StackinjavaSpecification
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.
StackinjavaImplementation
#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];
};
Stackin javaImplementation(cont.)
StackType::StackType()
{
top = -1;
}
void StackType::MakeEmpty()
{
top = -1;
}
bool StackType::IsEmpty() const
{
return (top == -1);
}
StackinjavaImplementation(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);
Implementingstacksusingtemplates
 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.)
Exampleusingtemplates
// 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.
Functiontemplates
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;
}
Functiontemplates(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--;
}
Commentsusing
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;
};
Implementingstacksusingdynamicarray
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:postfixexpressions
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:
Algorithmusingstacks(cont.)
Postfix
expressions:
Algorithmusing
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

More Related Content

Similar to stack in java ds - muhammed sdasdasdasdasdas.ppt

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
 
Stack linked list
Stack linked listStack linked list
Stack linked list
bhargav0077
 
ObjectivesMore practice with recursion.Practice writing some tem.docx
ObjectivesMore practice with recursion.Practice writing some tem.docxObjectivesMore practice with recursion.Practice writing some tem.docx
ObjectivesMore practice with recursion.Practice writing some tem.docx
vannagoforth
 

Similar to stack in java ds - muhammed sdasdasdasdasdas.ppt (20)

(674335607) cs2309 java-lab-manual
(674335607) cs2309 java-lab-manual(674335607) cs2309 java-lab-manual
(674335607) cs2309 java-lab-manual
 
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
 
2.1 STACK & QUEUE ADTS
2.1 STACK & QUEUE ADTS2.1 STACK & QUEUE ADTS
2.1 STACK & QUEUE ADTS
 
Java Stack Data Structure.pptx
Java Stack Data Structure.pptxJava Stack Data Structure.pptx
Java Stack Data Structure.pptx
 
Java Stack Data Structure.pptx
Java Stack Data Structure.pptxJava Stack Data Structure.pptx
Java Stack Data Structure.pptx
 
Extending javascript part one
Extending javascript part oneExtending javascript part one
Extending javascript part one
 
Java Generics
Java GenericsJava Generics
Java Generics
 
104332 sri vidhya eng notes
104332 sri vidhya eng notes104332 sri vidhya eng notes
104332 sri vidhya eng notes
 
stack presentation
stack presentationstack presentation
stack presentation
 
Stack linked list
Stack linked listStack linked list
Stack linked list
 
Lec2
Lec2Lec2
Lec2
 
2 a stacks
2 a stacks2 a stacks
2 a stacks
 
ObjectivesMore practice with recursion.Practice writing some tem.docx
ObjectivesMore practice with recursion.Practice writing some tem.docxObjectivesMore practice with recursion.Practice writing some tem.docx
ObjectivesMore practice with recursion.Practice writing some tem.docx
 
Data Structure.pptx
Data Structure.pptxData Structure.pptx
Data Structure.pptx
 
01-intro_stacks.ppt
01-intro_stacks.ppt01-intro_stacks.ppt
01-intro_stacks.ppt
 
Stack and Queue
Stack and Queue Stack and Queue
Stack and Queue
 
Ch03_stacks_and_queues.ppt
Ch03_stacks_and_queues.pptCh03_stacks_and_queues.ppt
Ch03_stacks_and_queues.ppt
 
Create this code in java An IShelfCustomer interface that h.pdf
Create this code in java An IShelfCustomer interface that h.pdfCreate this code in java An IShelfCustomer interface that h.pdf
Create this code in java An IShelfCustomer interface that h.pdf
 
Stacks in data structure
Stacks  in data structureStacks  in data structure
Stacks in data structure
 
stack coding.pptx
stack coding.pptxstack coding.pptx
stack coding.pptx
 

More from ssusere3b1a2 (7)

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
 
slide6.pptx
slide6.pptxslide6.pptx
slide6.pptx
 
Chapter 6 Methods.pptx
Chapter 6 Methods.pptxChapter 6 Methods.pptx
Chapter 6 Methods.pptx
 
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
 
information retrieval project
information retrieval projectinformation retrieval project
information retrieval project
 
information retrieval --> dictionary.ppt
information retrieval --> dictionary.pptinformation retrieval --> dictionary.ppt
information retrieval --> dictionary.ppt
 
introduction into IR
introduction into IRintroduction into IR
introduction into IR
 

Recently uploaded

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
SoniaTolstoy
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
ciinovamais
 
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
PECB
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Krashi Coaching
 

Recently uploaded (20)

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
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
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
 
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdf
 
social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajan
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.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
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room service
 
fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writing
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdf
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
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
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 

stack in java ds - muhammed sdasdasdasdasdas.ppt

  • 2. What is a Stackin 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. StackinjavaSpecification 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.
  • 6.
  • 7. StackinjavaImplementation #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. Stackin javaImplementation(cont.) StackType::StackType() { top = -1; } void StackType::MakeEmpty() { top = -1; } bool StackType::IsEmpty() const { return (top == -1); }
  • 9. StackinjavaImplementation(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. Implementingstacksusingtemplates  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. Exampleusingtemplates // 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. Functiontemplates 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. Functiontemplates(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; }
  • 17. Commentsusing 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. Implementingstacksusingdynamicarray 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:postfixexpressions 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: Algorithmusing 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