Data Structure - 25
Data Structure - 25
In computer science, a data structure is a particular way of storing and organizing data
in a computer so that it can be used efficiently.
Data Structure is the way how data is organized or stored in memory. For eg stack, queue,
linked list
Types of Data Structure
Linear Data Structure: A data structure is said to be linear, if its elements are stored
sequentially. For eg array ,stack, queue, linked list.
Non Linear Data Structure: A data structure is said to be non linear, if its elements can be
traversed and accessed randomly. For eg tree, graph.
Memory Allocation: The process of giving memory is called memory allocation. Memory can
be allocated in two ways. (i) Static Memory Allocation (ii) Dynamic Memory Allocation
A stack is a linear structure implemented in LIFO (Last In First Out) manner where insertions
and deletions can occur only at one end i.e. at stack‟s top.
When the stack is full and no new element can be accommodated and if we try to insert a new
element is called an overflow.
When the stack is empty and if we try to delete an element from the stack is called an
underflow.
1|Page
Convert the following Infix expressions into Postfix.
2|Page
Convert the following Infix Expression to PostFix Expression
Applications of stack
1. Parenthesis Matching
2. Evaluation of Arithmetic Expressions(Infix to Post Fix Conversion and prefix notation)
3. Stack is a Linear Data Structure and is applied where LIFO (Last In First Out) principle
is applied.
4. A stack is used in recursion to keep track of the function calls in a program..
5. Backtracking
6. Reverse a Data
3|Page
Question : A stack is a kind of data structure which can store elements with the restriction that an element can be
added or removed from the top only.
The details of the class Stack is given below:
Class name: Stack
Data members/instance variables :
st[ ]: the array to hold the integer numbers
size: the maximum capacity of the int array
top: the index of the topmost element of the stack
Member functions :
Stack(int cap): constructor to initialize size = cap and top = -1 and declare array st[ ]
void push(int n): to push a number onto the stack. If the stack is full, display the message “stack if full ( overflow)”
int pop(): removes an element from the top of the stack and returns it. If the stack is empty, it returns -1
void display(): Display the elements of the stack.
(a) Specify class Stack giving details of the constructors(), void push (int n), int pop( ) and void display().THE
MAIN() FUNCTION AND ALGORITHM NEED NOT BE WRITTEN.
(b) Under what Principle does the above entity work?
class Stack
{ int size,top; If top=0; in question
int st[ ]; class Stack
Stack(int cap) { int size,top;
{ int st[ ];
size=cap; Stack(int cap)
st=new int[size]; {
top=-1; size=cap;
} st=new int[size];
top=0;
void push(int n) }
{ void push(int n)
if(top<size-1) {
{ if(top<=size-1)
st[++top]=n; //since -1 first increase top then assign {
} st[top++]=n; // since 0 so
//assign then increase
else
}
{ else
System.out.println(" stack if full ( overflow)") ; {
} System.out.println(" stack if full
} ( overflow)") ;
}
int pop( ) public int pop( )
{ {
int v; int v;
if(top>=0)
if(top>=0)
{
{ return st[--top];
return st[top - -]; }
} else
else return(-1);
return(-1); }
} public void show()
{
public void show()
System.out.println(" The element of stack
{
");
System.out.println(" The element of stack ");
for(int i=0 ;i<=top-1 ;i++)
for(int i=0 ;i<=top ;i++)
{
{
System.out.println(st[i]);
System.out.println(st[i]);
}
}
}
} }
4|Page
(b) Stack works on LIFO (Last in First Out) Principle
A stack is a kind of data structure which can store elements with the restriction that an
element can be added or removed from the top only.
The details of the class Stack is given below:
Class name: Stack
Data members/instance variables :
st[ ]: the array to hold the integer numbers
size: the maximum capacity of the int array
top: the index of the topmost element of the stack
Member functions :
Stack(int cap): constructor to initialize size = cap andtop = 0 and declare array st[ ]
void push(int n): to push a number onto the stack. If the stack is full, display the message “stack if full (
overflow)”
int pop(): removes an element from the top of the stack and returns it. If the stack is empty, it returns -1
void display(): Display the elements of the stack
import java.util.*;
class Stack
{ int size,top;
int st[ ];
public Stack(int cap)
{
size=cap;
st=new int[size];
top=0;
}
void push(int n)
{
if(top<=size-1)
{
st[top++]=n;
}
else
{
System.out.println("stack if full ( overflow) ") ;
}
}
int pop( )
{
int v;
if(top>=0)
{
return st[--top];
}
else
return(-1);
}
void show()
{
System.out.println(" The element of stack ");
for(int i=0 ;i<=top-1 ;i++)
{
System.out.println(st[i]); }}
5|Page
Queue: is a linear structure implemented in FIFO (First in first out) manner where
insertions can occur at the “rear” end and deletions can occur only at the “front” end.
Dequeues : are the double ended queues in which the elements can be added or
removed at either end not in the middle.
An input restricted Dequeue allows insertions at only one end but allows deletions at
both the ends of the list
An output restricted Dequeue allows deletions at only one end but allows insertions at
both the ends of the list
Applications of Queue
For implementing FIFO services like telephone enquiries, reservation requests, traffic
flow, printer queues( the order in which print jobs are processed), for handling
scheduling of processes in multitasking operating system.
6|Page
Queue is an entity which can hold a maximum of n integers. Queue enables the user to add elements from the rear
end and remove integers from the front end of the entity. Define a class queue with the following details:
Class name : queue
Data members/instance variables :
qu[ ] : entity to hold the integer elements
n : stores the maximum capacity of the entity
front : to point to the index of the front end
rear : to point to the index of the rear end
Member functions:
qu (int mm) : constructor to initialize n= mm, front = 0, rear= 0
void insert(int ele) : to add an element from the rear index if possible otherwise display the message “Queue
is full.”
int delete( ) : to remove and return an element from the front index , if possible
otherwise and return -1
void display( ): displays the elements of the entity
Specify the class queue giving details of the constructor(int), void insert(int ), int delete( ) and void
display( ). THE MAIN FUNCTION AND ALGORITHM NEED NOT BE WRITTEN.
import java.io.*;
class queue // front and rear starts with -1
{ import java.util.*;
int qu[]; class Queue
{
int n,front,rear; int qu[];
public queue(int nn) int n,front,rear;
{ Queue(int nn)
n=nn; front=0; rear=0; {
qu =new int[n]; n=nn;
} front=-1;
rear=-1;
public void insert(int ele)
qu =new int[n];
{ }
if(rear<=n-1) void insert(int ele)
{ {
qu[rear++]=ele; if(rear<n-1)
} qu[++rear]=ele;
else }
else
{
{
System.out.println("\n Queue is full"); System.out.println("\n Queue is full");
} }
} }
int deleteq() int delete()
{ {
if(front<rear) // or if(front!=rear) if( front<rear)
{
{ return (qu[++front]);
return qu[front++]; }
} else
else {
{ return(-1);
return(-1); }
}
}
void display()
} {
public void display() System.out.println("\n Element of Queue \n");
{ if(front<rear) for(int i=front+1;i<=rear;i++)
{ System.out.println("\n element of queue \n"); {
for(int i=front;i<rear;i++) System.out.print(qu[i]+"\t");
{ System.out.print(qu[i]+"\t"); }
}
} }
} else { System.out.println("\n queue is empty \n"); } }
7|Page
Queue is an entity which can hold a maximum of n integers. Queue enables the user to add elements
from the rear end and remove integers from the front end of the entity. Define a class queue with the
following details:
Class name : queue
Data members/instance variables :
qu[ ] : entity to hold the integer elements
n : stores the maximum capacity of the entity
front : to point to the index of the front end
rear : to point to the index of the rear end
Member functions:
qu (int mm) : constructor to initialize n= mm, front = -1, rear= -1
void insert(int ele) : to add an element from the rear index if possible
otherwise display the message “Queue is full.”
int delete( ) : to remove and return an element from the front index , if possible
otherwise and return -1
void display( ): displays the elements of the entity
Specify the class queue giving details of the constructor(int), void insert(int ), int delete( ) and
void display( ). THE MAIN FUNCTION AND ALGORITHM NEED NOT BE WRITTEN.
8|Page
Circular Queue Program
A Circular queue is a linear data structure which works on the principle of FIFO, enables the user to
enter data from the rear end and remove data from the front end with the rear end connected to the
front end to form a circular pattern. Define a class CirQueue with the following details:
class name CirQueue
Data member/instance variable:
cq[ ] array to store the integers
size stores the maximum capacity of the array
front to point the index of the front end
rear to point the index of the rear end
Member functions/methods:
CirQueue (int s) constructor to initialize the data member size=s, front=0 and rear=0
void push(int n) to add integer in the queue from the rear end if possible,
otherwise display the message “QUEUE IS FULL”
int pop( ) removes and returns the integer from the front end of the queue if any,
else returns – 9999
void show( ) displays the queue elements
class CirQueue
{ int cq[];
int size,front,rear;
Cqueue(int s)
{ size=n;
front=0;
rear=0;
cq=new int[5];
}
void insertq(int num)
{
if((rear+1)%size==front)
System.out.println("overflow"); void display()
else {
{ int i=front;
rear=(rear+1)%size; while(i!=rear)
System.out.println("rear"+rear); {
cq[rear]=num; i=(i+1)%size;
} } System.out.print(cq[i]+" ");
}
int deleteq() }
{ }
if(front==rear)
{
System.out.println("underflow");
return(-99);
}
else
{
front=(front+1)%size;
System.out.println("front="+front);
return(cq[front]);}}
9|Page
Question( Dequeue Program)
A double-ended queue is a linear data structure which enables the user to add and remove
integers from either ends i.e. from front or rear. The details of the class DeQueue are given
below:
Class name: DeQueue
Data members/instance variables:
dq[]: array to hold integer elements
max: maximum capacity of the dequeue
front: to point the index of the front end
rear: to point the index of the rear end
Methods/Member functions:
DeQueue(int m): constructor to initialize max = m, front = 0 and rear = 0
void addFront(int n): to add integers in the dequeue at the front end if possible, otherwise
display the message “OVERFLOW FROM FRONT”
void addRear(int n): to add integers in the dequeue at the rear end if possible, otherwise display
the message “OVERFLOW FROM REAR”
int deleteFront(): removes and returns the integers from the front end of the dequeue if any,
else returns -999
int deleteRear(): removes and returns the integers from the rear end of the dequeue if any, else
returns -999
void show(): displays the elements of the dequeue
class DeQueue
{
int dq[];
int front;
int rear;
int max;
DeQueue (int m)
{
max=m;
front=0;
rear=0;
dq=new int[max];
}
void addrear(int n)
{
if(rear==max-1)
System.out.println("OVERFLOW FROM REAR ");
else
{
rear++;
dq[rear]=n;
}
}
10 | P a g e
void addfront(int n)
{
if(front==0)
System.out.println("OVERFLOW FROM FRONT ");
else
{
dq[front]=n;
front--;
}
}
int deletefront()
{
if(front==rear)
{
System.out.println("underflow");
return -999;
}
else
{
front++;
return dq[front];
}
}
int deleterear()
{
if(front==rear)
{
System.out.println("underflow");
return -999;
}
else
return dq[rear--];
}
void display()
{
for(int i=front+1;i<=rear;i++)
{
System.out.print(dq[i]+"\t");
}
}
}
11 | P a g e
Assignment
A stack is a kind of data structure which can store elements with the restriction that an
element can be added or removed from the top only.
The details of the class Stack is given below:
Class name: Stack
Data members/instance variables :
st[ ]: the array to hold the names
size: the maximum capacity of the string array
top: the index of the topmost element of the stack
Member functions :
Stack(int cap): constructor to initialize size = cap and top = -1 and declare array st[ ]
void push(int n): to push a number onto the stack. If the stack is full, display the message
“stack if full ( overflow)”
String pop(): removes an element from the top of the stack and returns it. If the stack is
empty, it returns “”.
void display(): Display the elements of the stack.
(a) Specify class Stack giving details of the constructors(), void push (int n), String pop( ) and
void display().
THE MAIN() FUNCTION AND ALGORITHM NEED NOT BE WRITTEN.
(b) Under what Principle does the above entity work?
Question
A bookshelf is designed to store the books in a stack with LIFO(Last In First Out)
operation.
Define a class Book with the following specifications:
Class name : Book
Data members/instance variables:
name[ ] : stores the names of the books
point : stores the index of the topmost book
max : stores the maximum capacity of the bookshelf
Methods/Member functions:
Book(int cap) : constructor to initialise the data members
max=cap and point=-1
void tell() : displays the name of the book which was last entered in the shelf. If there
is no book left in the shelf, displays the message “SHELF EMPTY”
void add(String v) : adds the name of the book to the shelf if possible, otherwise
displays the message “SHELF FULL”
void display( ) : displays all the names of the books available in the shelf
Specify the class Book giving the details of ONLY the functions void tell( ) and
void add(String).
Assume that the other functions have been defined.
The main function need not be written.
12 | P a g e
class Book
{
String name[ ];
int point,max;
Book(int cap)
{
max=cap;
point = -1;
name=new String[max];
}
void tell()
{
if(point>=0)
System.out.println( name[point]);
else
System.out.println(" SHELF EMPTY");
}
void add(String v)
{
if(point<max-1)
name[++point]=v;
else
System.out.println(“ SHELF FULL");
}
void display()
{
for(int i=point ;i>=0 ;i- -)
System.out.println( name[i]);
}}
It works on the Principle of LIFO
13 | P a g e
ISC2015 : WordPile is an entity which can hold maximum of 20 characters. The restriction is that a
character can be added or removed from one end only. Some of the members of classes are given below:
Class name : WordPile
Data members/instance variables:
ch[ ] : character array to hold the character elements
capacity : integer variable to store the maximum capacity
top : to point to the index of the topmost element
Methods/Member functions:
WordPile( int cap) : constructor to initialise the data member
capacity = cap, top = -1 and create the WordPile
void pushChar( char v) : adds the character to the top of WordPile if
possible, otherwise output a message “WordPile is full”
char popChar( ) : returns the deleted character from the top of the
WordPile if possible, otherwise it returns „\\‟
(a) Specify the class WordPile giving the details of the constructor, void
pushChar(char) and char popChar( ). The main function and
algorithm need not be written.
(b) What is the name of the entity described above and state one of its applications.
(a) class WordPile
{
char ch[ ] = new char[20];
int capacity,top;
WordPile(int cap)
{
capacity=cap;
top=-1;
}
void pushChar(char v)
{
if(top<capacity-1)
ch[++top]=v;
else
System.out.println("WordPile is full");
}
char popChar()
{
if(top>=0)
return ch[top--];
else
return '\\';
}
}
(b) Name of the Entity : Stack
Application : LIFO (Last In First Out ) , Recursion , infix to postfix
14 | P a g e
ISC 2012
(a)Link is an entity which can hold a maximum of 100 integers. Link enables the user to add elements from the
rear end and remove integers from the front end of the entity. Define a class Link with the following details:
Class name : Link
Data members/instance variables :
lnk[ ] : entity to hold the integer elements
max : stores the maximum capacity of the entity
begin : to point to the index of the front end
end : to point to the index of the rear end
Member functions:
lnk(int mm) :constructor to initialize max = mm, begin = 0, end = 0
void add1ink(int v): to add an element from the rear index if possible otherwise display the message “OUT
OF SIZE. ..”
int dellink( ) : to remove and return an element from the front index , if possible otherwise display the message
“EMPTY...” and return -99
void display( ): displays the elements of the entity
Specify the class Link giving details of the constructor(int), void addlink(int). int dellink( ) and void display( ).
THE MAIN FUNCTION AND ALGORITHM NEED NOT BE WRITTEN.
(b)What type of data structure is the above entity?
(a) class Link
{
int lnk[ ] = new int[100];
int max.begin,end:
Link (int mm )
{ max=mm:
begin=0;
end=0;
}
void addlink(int v)
{
if(end<=max-1)
Ink[end++]=v;
else
System.out.println("OUT OF SIZE. . .");
}
int dellink( )
{ int v;
if((begin<end) //or if (begin!=end)
{
v=Ink[begin++];
return v;
}
else
{
System.out. print1n(“ EMPTY. ..”);
return -99;
}} void display ( )
{
for(int i=begin ;i<end;i++)
{
System.out.println(Ink[i]);
}}
(c) Queue / Linear Data Structure /FIFO.
15 | P a g e