Unit-4 OOP_DS
Unit-4 OOP_DS
Unit-4 OOP_DS
In the modern world, Data and its information is an essential part, and various
implementations are being made to store in different ways. Data are just a collection
of facts and figures, or you can say data are values or a set of values that are in a
particular format. A data item refers to a single set of values. Data items are then
further categorized into sub-items, which are the group of items that are not being
called a plain elementary form of items. Let us take an example where the name of the
student may be divided into three sub-items, namely: first name, middle name, and
last name. But the ID that is assigned to a student would typically be considered as a
single item.
In the example mentioned above, such as ID, Age, Gender, First, Middle, Last, Street,
Area, etc. are elementary data items, whereas (Name, Address) is group data items.
In computer terms, a data structure is a Specific way to store and organize data in a
computer's memory so that these data can be used efficiently later. Data may be
arranged in many different ways, such as the logical or mathematical model for a
particular organization of data is termed as a data structure. The variety of a specific
data model depends on the two factors -
A data structure is said to be linear if its elements combine to form any specific order.
There are two techniques of representing such linear structure within memory.
The first way is to provide the linear relationships among all the elements
represented using linear memory location. These linear structures are termed as
arrays.
The second technique is to provide a linear relationship among all the elements
represented by using the concept of pointers or links. These linear structures are
termed as linked lists.
The common examples of the linear data structure are:
Arrays
Queues
Stacks
Linked lists
This structure is mostly used for representing data that contains a hierarchical
relationship among various elements.
Examples of Non-Linear Data Structures are listed below:
Graphs
the family of trees and
table of contents
Tree: In this case, data often contain a hierarchical relationship among various
elements. The data structure that reflects this relationship is termed as a rooted tree
graph or a tree.
Graph: In this case, data sometimes hold a relationship between the pairs of elements,
which is not necessarily following the hierarchical structure. Such a data structure is
termed as a Graph.
What is Stack ?
A stack is a linear data structure in which all the insertion and deletion of data or you
can say its values are done at one end only, rather than in the middle. Stacks can be
implemented by using arrays of type linear.
The stack is mostly used in converting and evaluating expressions in Polish notations,
i.e.:
Infix
Prefix
Postfix
In case of arrays and linked lists, these two allows programmers to insert and delete
elements from any place within the list, i.e., from the beginning or the end or even
from the middle also. But in computer programming and development, there may arise
some situations where insertion and deletion require only at one end wither at the
beginning or end of the list. The stack is a linear data structure, and all the insertion
and deletion of its values are done in the same end which is called the top of the stack.
Let us suppose take the real-life example of a stack of plates or a pile of books etc. As
the item in this form of data structure can be removed or added from the top only
which means the last item to be added to the stack is the first item to be removed. So
you can say that the stack follows the Last In First Out (LIFO) structure.
Real life example of stack is box containing books which can be opened only from
one end and only one book can be removed from box at a time. Tray holder in
cafeteria is also an example of a stack.
Return
3.1.3 An Algorithm To Obtain The Value Of Ith Element From Top Of The Stack
Without Deleting It
The main tasks in this algorithm are:
Check for underflow condition.
Retrieve the desired element.
3.1.4 An Algorithm To Change The Value Of Ith Element From Top Of The
Stack
The main tasks in this algorithm are:
Check for underflow condition.
Update the desired element.
The stack can be represented in memory with the use of arrays. To do this job, you
need to maintain a linear array STACK, a pointer variable top which contains the top
element.
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
class stack {
int stk[5];
int top;
public:
stack()
{
top = -1;
}
void push(int x)
{
if (top >= 4) {
cout << "stack overflow";
return;
}
top=top+1;
stk[top] = x;
cout << "inserted " << x;
}
void pop()
{
if (top < 0) {
cout << "stack underflow";
return;
}
cout << "deleted " << stk[top--];
}
void display();
void peep();
void change();
};
void stack::display()
{
if (top < 0) {
cout << " stack empty"; return;
}
for (int i = top; i >= 0; i--)
cout << stk[i] << " ";
}
void stack::peep()
{ int i;
cout<<"enter the position";
cin>>i;
if (top < 0) {
cout << " stack empty"; return; }
cout<<stk[top-i+1]; //
}
void stack::change()
{ int i,item;
//cout<<"\n value of top is "<<top;
cout<<"\n enter the position to enter";
cin>>i;
cout<<"\n Enter the item to be insert :";
cin>>item;
if (top < 0) {
cout << " stack empty"; return; }
stk[top-i+1]=item;
}
int main()
{ clrscr();
int ch;
stack st;
while (1)
{
cout << "\n1.push 2.pop 3.display 4.peep 5.change
6.exit\nenter ur choice: ";
cin >> ch;
switch (ch)
{
case 1:
cout << "enter the element: "; cin >> ch;
st.push(ch);
break;
case 2:
st.pop();
break;
case 3:
st.display();
break;
case 4:
st.peep();
break;
case 5:
st.change();
break;
case 6:
exit(0);
}
}
}
Algorithm to convert infix to postfix expression
In this case, We use the stacks to convert infix to postfix. We have operator's stack,
output's stack and one input string. Operator's stack works as FILO(First In Last
Out). Output's stack works as FIFO (First In First Out).
10) If the scanned character is an ‘)’, pop the stack and output
12) At the end ,pop operator stack until it is not empty and push all operator into
output stack.
Infix: A+B*C/(E-F)
Postfix: ABC*EF-/+
A+B*C/(E-F) A
A+B*C/(E-F) A +
A+B*C/(E-F) AB +
A+B*C/(E-F) AB +*
A+B*C/(E-F) ABC +*
A+B*C/(E-F) ABC* +/
A+B*C/(E-F) ABC*EF- +/
ABC*EF-/+
A+B*C/(E-F)
In this case, we have operator's stack, output's stack and one input string.
Operator's stack works as FILO(First In Last Out). Output's stack works as FIFO
(First In First Out).
7)For associativity of left to right pop and push it onto output stack
12) At the end ,pop operator stack until it is not empty and push all operator into
output stack.
STACK -
1.it is called as LIFO which stands for Last in first out as stack uses this property
2.element inserted first will be accessed last in stack as it has only one open end
3.recent element in stack is accessedd using the value of top which points to the
topmost element of stack
4.for inserting an element push() function is used
5.for deleting an element pop() function is used
6.example is plates placed one above other
QUEUE -
1.it is called as FIFO which stands for First in first out as queue uses these property
2.element inserted first will be served first
3.it has two open ends called as rear end and front end
4.element is deleted using pop() function from front end of queue
5.element is inserted using push() function from rear end of queue
6.example is queue for bus
Stack A stack is a linear data structure in which elements can be inserted and
deleted only from one side of the list, called the top. A stack follows the LIFO (Last
In First Out) principle, i.e., the element inserted at the last is the first element to
come out. The insertion of an element into stack is called push operation, and
deletion of an element from the stack is called pop operation. In stack we always
keep track of the last element present in the list with a pointer called top.
The diagrammatic representation of stack is given below:
Queue: A queue is a linear data structure in which elements can be inserted only
from one side of the list called rear, and the elements can be deleted only from the
other side called the front. The queue data structure follows the FIFO (First In First
Out) principle, i.e. the element inserted at first in the list, is the first element to be
removed from the list. The insertion of an element in a queue is called
an enqueue operation and the deletion of an element is called a dequeue operation.
In queue we always maintain two pointers, one pointing to the element which was
inserted at the first and still present in the list with the front pointer and the second
pointer pointing to the element inserted at the last with the rear pointer.