Data Structures Document
Data Structures Document
5 Mark Questions
Q.1. Queue is an entity which can hold a maximum of 100 integers. The
queue enables the user to add integers from the rear and remove integers
from the front. [ISC 2017]
Define a class Queue with the following details:
Class Name: Queue Data member/instance variables:
Que[]: array to hold the integer elements size: stores the size of the array
rear: to point the index of the rear
Member functions:
Queue (int mm) : constructor to initialize the data size = mm, front = 0,
rear = 0
void addele (int v) : to add integer from the rear if possible else display
the massage ‘‘Overflow’’ int delele(): returns elements from front if
present, otherwise displays the message ‘‘Underflow’’ and return -9999
void display () : display the array elements
Specify the class Queue giving details of ONLY the functions void
addele(int) and int delele(). Assume that the other functions have been
defined. The main function and algorithm need NOT be written.
Ans. void addele(int v)
{ if(rear == Que.length-1) {
System.out.println("Overflow") }
else{
Que[rear++] = v; } }
int delele() {
if(front==0) {
System.out.println("Underflow");
return -9999; }
else{
return Que [front--];
} }
Ans. (a)
public class WordPile {
char ch []= new char [20];
int capacity, top;
WordPile (int cap) {
capacity = cap;
top = –1;
ch = new char[capacity]; }
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 Undo Infix to Postfix (anyone)
Q.2. A stack is a linear data structure which enable the user to add and
remove integers from one end only, using the concept of LIFO (Last In First
Out). An array containing the marks of 50 students in ascending order to
be pushed into the stack. Define a Class Array_to_Stack with the following
details: [ISC 2014]
Class Name Array_to_Stack
Data members/Instance variables:
m[] to store the marks.
st [] to store the stack elements.
cap :maximum capacity of the array and stack.
Top: to point the index of the topmost element of the stack.
Member Functions
Array_to_Stack (int n) Parameterised constructor to initialise cap = n and
top = –1.
void input_ marks () to input the marks from user and store it in the array
m[] in ascending order and simultaneously push the marks into the stack
st[] by invoking the function pushmarks ().
void pushmarks (int v) to push marks into the stack at top location if
possible, otherwise display “not possible”.
int popmarks () to return marks from the stack if possible, otherwise
returns – 999.
void display () to display the stack elements.
Specify the class Array_to_Stack giving the details of the constructor (int),
void input_marks(). void pushmarks(int). int popmarks() and void
display(). The main() function and algorithm need to not be written.
Ans.
import java.io.*;
class Array_to_stack {
public int m[];
public int st[], top;
public static int cap;
public Array_to_Stack (int n) {
cap = n; top = – 1;
m = new int [n]; st = new int[n]; }
void input_marks () throws IOException {
BufferedReader br = new BufferedReader (new InputStreamReader
System. in));
int i;
System.out.println (“Enter array element:’’);
for (i = 0; i < cap; i++) {
m[i]= Integer.parseInt (br.readLine()); }
int len = m.length;
int j,small, p,temp;
for (i = 0; i < len; i++) {
small = m[i];
int pos = i;
for(j = i + 1; j < len; j++)
{ if(m[j] < small) {
small = m[j]; pos = j; } }
temp = m[i];
m[i] = m[pos];
m[pos] = temp;
pushmarks(m[i]); } }
void pushmarks (int v) {
if(top < cap) { top++; st[top] = v;
System.out.println (“Element” +v+ “is pushed to stack!”); }
else {
System.out.println (“not possible”); } }
int popmarks () {
if (top >= 0) {
int x = st[top];
top ––;
return x; }
else {
return – 999; } }
public void display () {
if (top >= 0) {
System.out.println (“Elements in stack:”); for (int i = 0; i <= top; i++)
Q.5. Stack is a kind of data structure which can add elements with the
restriction that an element can be added or removed from the top only.[ISC
2011]
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 ctr to count the number of elements of the stack
Member Functions
Stack () default constructor
Stack (int cap) parameterized constructor to initialise size = cap and top
=-1
void pushname (string n) to push a name into the stack if the stack is full,
display the message “OVERFLOW”.
String popname () removes a name from the top of the stack is empty,
display the message “UNDERFLOW”
void display display the elements of the stack
(i) Specify class Stack giving details of the constructor,
void pushname (String n), String popname () and void display ().
The main () function and algorithm need not to be written.
(ii) Under what principle does the above entity work?
Ans. (i) class Stack { String st[];
int size, top, ctr;
Stack () {
top = – 1;
ctr = 0;
size = 0; }
Stack (int cap) {
st = new String [cap];
size = cap;