Unit4 OOP&DS
Unit4 OOP&DS
Unit4 OOP&DS
The data structure name indicates itself that organizing the data in memory.
There are many ways of organizing the data in the memory as we have already seen one of the
data structures, i.e., array in C language.
Array is a collection of memory elements in which data is stored sequentially, i.e., one after
another.
In other words, we can say that array stores the elements in a continuous manner.
Page 1
Object Oriented Programming & Data Structure
The primitive data structures are primitive data types. The int, char, float, double, and pointer
are the primitive data structures that can hold a single value.
The arrangement of data in a sequential manner is known as a linear data structure. Ex. Arrays,
Linked list, Stacks, and Queues.
In these data structures, one element is connected to only one another element in a linear form.
Static data structure: It is a type of data structure where the size is allocated at the
compile time. Therefore, the maximum size is fixed.
Dynamic data structure: It is a type of data structure where the size is allocated at the
run time. Therefore, the maximum size is flexible.
Major Operations
The major or the common operations that can be performed on the data structures are:
Page 2
Object Oriented Programming & Data Structure
Efficiency: If the choice of a data structure for implementing a particular ADT is proper,
it makes the program very efficient in terms of time and space.
Reusability: The data structure provides reusability means that multiple client programs
can use the data structure.
Abstraction: The data structure specified by an ADT also provides the level of
abstraction. The client cannot see the internal working of the data structure, so it does not
have to worry about the implementation part. The client can only see the interface.
Primitive data structure is a kind of data Non-primitive data structure is a type of data
structure that stores the data of only one structure that can store the data of more than one
type. type.
Examples of primitive data structure are Examples of non-primitive data structure are
integer, character, float. Array, Linked list, stack.
Primitive data structure will contain some Non-primitive data structure can consist of a
value, i.e., it cannot be NULL. NULL value.
The size depends on the type of the data In case of non-primitive data structure, size is not
structure. fixed.
Primitive data structure can be used to call Non-primitive data structure cannot be used to call
the methods. the methods.
What is a Stack?
A Stack is a linear data structure that follows the LIFO (Last-In-First-Out) principle.
Stack has one end, whereas the Queue has two ends (front and rear).
It contains only one pointer top pointer pointing to the topmost element of the stack.
Whenever an element is added in the stack, it is added on the top of the stack, and the element
can be deleted only from the stack.
In other words, a stack can be defined as a container in which insertion and deletion can be
done from the one end known as the top of the stack.
Working of Stack
Stack works on the LIFO pattern. As we can observe in the below figure there are five
memory blocks in the stack; therefore, the size of the stack is 5.
Suppose we want to store the elements in a stack and let's assume that stack is empty.
We have taken the stack of size 5 as shown below in which we are pushing the elements one
by one until the stack becomes full.
push(): When we insert an element in a stack then the operation is known as a push. If
the stack is full then the overflow condition occurs.
pop(): When we delete an element from the stack, the operation is known as a pop. If the
stack is empty means that no element exists in the stack, this state is known as an
underflow state.
display() or peep(): It prints all the elements available in the stack.
isEmpty(): It determines whether the stack is empty or not.
isFull(): It determines whether the stack is full or not.'
peek(): It returns the element at the given position.
count(): It returns the total number of elements available in a stack.
change(): It changes the element at the given position.
PUSH operation
1) PUSH Operation:
It means to insert the elements in the stack. In executing the procedure push, one must first test whether
there is the room in stack for the new item, if not, then the condition is known as overflow.
Algorithm:
Push (Item,n,value)
value=item to push, n=size of stack
Step-1 Set top=-1
Step-2 [Check for empty or full stack]
If (top>=n-1)
Print “Overflow/stack is full”
Return(0)
Else
Step-3 [Increment the stack pointer by one]
Top=top+1
Step-4 [Insert the value to be pushed in the stack at the location of top]
Item [top] =value
Step-5 exit
POP operation
Before deleting the element from the stack, we check whether the stack is empty.
If we try to delete the element from the empty stack, then the underflow condition
occurs.
If the stack is not empty, we first access the element which is pointed by the top
Once the pop operation is performed, the top is decremented by 1, i.e., top=top-1.
1) POP Operation:
It means to take elements from stack. In executing the procedure pop, one must first test whether there is
an element in the stack to be deleted. If not, then condition is known as underflow.
Algorithm:
Step-1 [Check for empty]
If (top==-1)
Print “Underflow/stack is empty”
Else
Step-2 [Take out the value of the stack where pointer top is pointing]
Value=Item [top]
Step-3 [Decrement the value of top of the stack by one so as to point at the next node to be popped]
Top=top-1
Step-4 return(value) & exit
1) PEEP Operation:
If only information store at some location in a stack is needed, then this operation can be performed. We
move logically to desired location & then fetch the information associated with that location. To access desire
elements from top of the stack is known as Peep Operation. For example suppose we have to access 3 element
from top than it is 2=c, ith=desired element=3 and top=4.the formula is :
E
Top = top-i+1
D
C 2=C = 4-3+1
B = 1+1
A
Algorithm:
Peep(i)
Step-1 [Check for empty]
If (top-i+1<0)
Print “Underflow/stack is empty”
Else
Step-2 [Return ith element from stack]
return(Item[top-i+1])
Step-3 Exit
2) UPDATE Operation
It is required when the content of some location in a stack is to be changed suppose one wants to
update information at the ith location in a stack than we first move the pointer to the ith location and than input the
new value of that location.
Algorithm: update(i,value)
If (top-i+1<0)
Else
Item[top-i+1]=value
Step-3 Exit
Example of Stack(Stack_ex.cpp):
#include<iostream.h>
#include<conio.h>
#include<process.h>
int a[5],top=-1,max=5;
class StackOperation
{
public:
void push();
void pop();
void peep();
void update();
void display();
};
void StackOperation::push()
{
if(top==max-1)
{
cout<<"\nStack is Overflow";
}
else
{
cout<<"\n Enter element for Push:";
cin>>a[++top];
Damyanti Patel Page 9
Object Oriented Programming & Data Structure
}
display();
}
void StackOperation::pop()
{
if(top==-1)
{
cout<<"Stack is underflow";
}
else
{
cout<<"\n Your Poped element is: "<<a[top--];
}
display();
}
void StackOperation::peep()
{
int n;
cout<<"\nEnter the position which you want to get:";
cin>>n;
if(top<n-1)
{
cout<<"\n sorry!! Element does not exit";
}
else
{
cout<<"\n Your Peeped element is: "<<a[n-1];
}
display();
}
void StackOperation::update()
{
int p,v;
cout<<"\nEnter the position which you want to update:";
cin>>p;
if(top<p-1)
{
cout<<"\n sorry!! Element does not exit";
}
else
Damyanti Patel Page 10
Object Oriented Programming & Data Structure
{
cout<<"\nEnter the value:";
cin>>v;
a[p-1]=v;
}
display();
}
void StackOperation::display()
{
int i;
if(top==-1)
{
cout<<"\n Stack is Empty";
}
else
{
for(i=0;i<=top;i++)
{
cout<<"\n "<<a[i];
}
}
}
void main()
{
int ch,n;
StackOperation s1;
do
{
clrscr();
cout<<"\n===========================";
cout<<"\n Stack Operation ";
cout<<"\n===========================";
cout<<"\n 1. PUSH\n 2. POP\n 3. Peep \n 4. Update\n 5. Display \n 6. Exit";
cout<<"\nEnter Your Choice:";
cin>>ch;
switch(ch)
{
case 1:s1.push();break;
case 2:s1.pop();break;
Damyanti Patel Page 11
Object Oriented Programming & Data Structure
case 3:s1.peep();break;
case 4:s1.update();break;
case 5:s1.display();break;
case 6:exit(0); break;
default:cout<<"You Have Wrong Choice";
}
cout<<"\n Do you want to continue (1-Yes 0-No):";
cin>>n;
}while(n!=0);
getch();
}
Stack Application:-
When operators are written after the operands, it is called Reverse Polish Notation.
There are three types of expression:
1) Infix
2) Prefix
3) Postfix
“Pre”,”Post” &”In” refer to the relative position of the operator with respect to the two operands.
1. Infix notation:-
🠆 In this type of expression operators are placed between the operands.
E.g.: A+ (B*C)
🠆 In this notation the order of operations to be performed is determined by the use of parenthesis or by the
use of some operator precedence conventions.
🠆 The levels of precedence are (highest to lowest):
Priority Operator
1 (
2 ^,$,
3 %
4 / or * (left to right)
5 + or –(left to right)
6 )
🠆 During conversion of an infix notation we must begin with inner parenthesis and then precede towards,
the outside expression i.e. the operations with higher precedence are converted first.
Example: z + (y * x - (w / v ^ u) *t) *s
Y *)-* stuv^w/*xy
( * stuv^w/*xy*-
+ + stuv^w/*xy*-*
Z + stuv^w/*xy*-*z
(stack is empty) stuv^w/*xy*-*z+
F /( abcd^*e/+f
* /(* abcd^*e/+f
G /(* abcd^*e/+fg
- /(- abcd^*e/+fg*
H /(- abcd^*e/+fg*h
) / abcd^*e/+fg*h-
(stack is empty) abcd^*e/+fg*h-/
Example:
#include<iostream.h>
#include<conio.h>
void main()
int n;
cin >> n;
cout << "Factorial of " << n << " = " << factorial(n);
getch();
int factorial(int n) {
if(n > 1)
else
return 1;