Upload
iiui
View
544
Download
0
Tags:
Embed Size (px)
DESCRIPTION
Data Structures and Algorithms : This is about Stack and program and question.
Citation preview
Data Structures
Linear: One to One RelationshipStatic and Dynamic
Non linearOne to ManyMany to Many
We will first cover Linear DSs
Stack
A stack is used to store elements where the Last element In is the First one Out (LIFO).
A common model of a stack is a plate or coin stacker.
Plates are "pushed" onto to the top and “pooped” off the top.
Stack
New elements are added or pushed onto the top of the stack.
The first element to be removed or popped is taken from the top - the last one in.
Stack Operations
A stack is generally implemented with only two principle operations Push adds an item to a stack Pop extracts the most recently pushed item from the
stack
Other methods such as
Top returns the item at the top without removing it Isempty determines whether the stack has anything
in it
Stack Implementation
Static Implementation (Using arrays)
Dynamic Implementation (Using dynamic lists)
Stack Implementation Using Arrays
For the static implementation of stack an array will be used.
This array will hold the stack elements.
The top of a stack is represented by an integer type variable which contains the index of an array containing top element of a stack.
Stack Implementation Using Arrays
4
3
2
1
0
Empty stack StackSize = 5top = -1
70
1
2
3
4
top
Push 7
70
81
2
3
4
top
Push 8Push 9
70
81
92
3
4
topPush 4
70
81
92
43
4
top
Push 5
70
81
92
43
54top
top = StackSize – 1,Stack is full,We can’t push more elements.
Stack Implementation Using Arrays
push(element)
{
if (top == StackSize – 1)
cout<<“stack is full”;
else
Stack[++top] = element;
}
Stack Implementation Using Arrays
4
3
2
1
0
Empty stack top = -1We can’t pop mpre elements
70
1
2
3
4
top
Pop
70
81
2
3
4
top
70
81
92
3
4
top
70
81
92
43
4
top
70
81
92
43
54top
top = StackSize – 1,Stack is full,We can’t push more elements.
PopPop Pop Pop
Stack Implementation Using Arrays
pop()
{
if (top == –1)
cout<<“stack is empty”;
else
return Stack[top--];
}
Stack Implementation Using Arrays
topElement() //returns the top element of stack //without removing it.
{
if (top == –1)
cout<<“stack is empty”;
else
return Stack[top];
}
Stack Implementation Using Arrays
isEmpty() //checks stack is empty or not
{
if (top == –1)
return true
else
return false
}
Stack Implementation Using Arrays
template <class Element_Type>class Stack{
private:/* This variable is used to indicate stack is
full or not*/unsigned int Full_Stack;/* This variable is used to indicate top of the
stack */int Top_of_Stack;/* This pointer points to the array which
behaves as stack, the space for this array is allocated dynamically */
Element_Type *Stack_Array
Continue on next slide…
Stack Implementation Using Arrays
//This constructor creates a stack.Stack(unsigned int Max_Size)
{Full_Stack = Max_Size;Top_of_Stack = -1;Stack_Array = new Element_Type[Max_Size];
}/* This Destructor frees the dynamically allocated
space to the array */ ~Stack(){
delete Stack_Array;}
Continue on next slide…
Stack Implementation Using Arrays
/*This function Return TRUE if the stack is full, FALSE otherwise.*/bool Is_Full(){ if (Top_of_Stack == Full_Stack-1)
returns True;else
returns False;}
/*This function Return TRUE if the stack is empty, FALSE otherwise.*/bool Is_Empty(){ if(Top_of_Stack == -1)
returns True;else
returns False;}
Continue on next slide…
Stack Implementation Using Arrays
// If stack is not full then push an element x in itvoid Push(Element_Type x){ if(is_Full())
cout<<“stack is full”;else
Stack_Array[++Top_of_Stack] = x;}//if Stack is not empty then pop an element form itElement_Type pop(){ if(is_Empty())
cout<<“stack is empty”;else
return Stack_Array[Top_of_Stack--];}
Continue on next slide…
Stack Implementation Using Arrays
// This function makes the stack empty void Make_Empty(){ Top_of_Stack = -1;}/* This function returns the top element of stack */Element_Type Top(){if(is_Empty())cout<<“stack is emepty”;elsereturn Stack_Array[Top_of_Stack];}
};
Applications of Stack
Reversing the string
push each character on to a stack as it is read.
When the line is finished, we then pop characters off the stack, and they will come off in the reverse order.
Applications of StackReversing the string
void ReverseRead (void){ STACK<char> stack(256); //The stack stack is created and can
//hold at most 256 elements of type charchar item;cin>>item;while (!stack.is_Full() && item ! = “\n”){ stack.Push (item); // push each character onto the stack cin>>item;}while(! stack.is_Empty() ){ item = stack.Pop (); //Pop an element from stack cout<<item;}
}