0% found this document useful (0 votes)
103 views

Data Structures Document

The document describes several questions related to data structures such as stacks, queues, and arrays. The first question defines a Queue class with array Que to hold integers, size variable to store array size, rear pointer to point to rear element, and methods addEle and delEle to add and remove elements. The second question defines a Book class with name array to store book names, point variable to store top index, max variable to store capacity, and methods tell and add to display top book and add books. The third question involves defining a WordPile class similar to a stack, with array ch to hold characters, capacity and top variables, and methods pushChar and popChar to add and remove characters.

Uploaded by

Andrina Praveen
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
103 views

Data Structures Document

The document describes several questions related to data structures such as stacks, queues, and arrays. The first question defines a Queue class with array Que to hold integers, size variable to store array size, rear pointer to point to rear element, and methods addEle and delEle to add and remove elements. The second question defines a Book class with name array to store book names, point variable to store top index, max variable to store capacity, and methods tell and add to display top book and add books. The third question involves defining a WordPile class similar to a stack, with array ch to hold characters, capacity and top variables, and methods pushChar and popChar to add and remove characters.

Uploaded by

Andrina Praveen
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

Data Structures

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--];
} }

Q.2. A bookshelf is designed to store the books in a stack with LIFO(Last In


Last First Out) operation. Define a class Book with the following
specifications: [ISC 2016]
Class Name: Book
Data member/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
Method/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 we 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 () : display 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.
Ans.
public 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.print (“\n SHELF EMPTY”);
else
System.out.print (“\n” +name[point]); }
void add (String v) {
if (point < max – 1)
name[++point] = v;
else
System.out.print (“\n SHELF FULL”); }
void display () {
for (int i = point; i >= 0; i– –)
System.out.print (“\n” +name[i]); }}
10 Mark Questions
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.
[ISC 2015]
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 Word Pile giving the details of the constructor,
voidpushChar (char) and char popChar (). The main function and
algorithm need not be written.
(b) What is the name of the entity described

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;

top = –1; ctr = 0; }


void pushname(String n) {
if(top >= size – 1)
System.out.println(“OVERFLOW”);
else { top++;
st[top] = n;
ctr++; } }
String popname() {
String S = “ ”;
if(top < 0)
System.out.println (“UNDERFLOW”);
else {
S = st[top]; top;
ctr--; return S; } }
void display () {
for (int i = 0; i <= top; i++)
System.out.println(st[i]); } }
(ii) Stack works on LIFO (Last In First Out) principle.
Q.6. Define a class Repeat which allows the user to add elements from one
end (rear) and remove elements from the other end (front) only. [ISC
2010]
The following details of the class Repeat are given below:
Class Name Repeat
Data members/Instance variables: st[] an array to hold a maximum of
100 integer elements
cap stores the capacity of the array.
f to point the index of the front.
r to point the index of the rear.
Member Functions
Repeat (int m) constructor to initialise the data number
cap = m, f = 0, r = 0 and to create the integer array.
void pushvalue (int v) to add integers from the rear index if possible else
display the message (“OVERFLOW”).
int popvalue() to remove and return elements from the front if array is
empty then return –9999 void disp () display the elements present in the
list
(i) Specify the class Repeat giving details of the constructor(int), member
functions, void push value(int), int popvalue() and void disp(). The main()
function need not to be written.
(ii) What is the common name of the entity described above?
(iii) On what principle does this entity work?
Ans. (i) class Repeat {
int st[];
int cap;
int f, r;
Repeat (int m) {
cap = m;
f = 0; r = 0;
st = new int [100]; }
void pushvalue(int v) {
if (r == cap – 1) {
System.out.Println (“OVERFLOW”) }
else if (r ==0) {
f = r = 1;
st[r] = v; }
else st[++r] = v; }
int popvalue () {
if(f == 0) {
System.out.println (“EMPTY”);
return –9999; }
else if (f == r)
int k = st[f];
f = r = 0;
return k; }
else
return (st[f++]); }
void disp() {
for (int i = f; i <= r; i++) {
System.out.print (st[i]+“ ”); } } }
(ii) The common name of the entity described above is queue.
(iii) The entity work on FIFO (First In First Out) Principal.
Q.7. Give the output of the following program segment. [ISC 2006]
class Stack {
int Stack [] = new int [50];
int top; Stack () {
top = 0; }
void Push (int x) {
Stack [++top] = x; }
int pop () {
return Stack [top – –]; }
public static void main (String args []) {
Stack s1 = new Stack ();
s1.push (51);
s1.push (27);
s1.push (5);
Sytem.out.println(s1.pop());
System.out.println(s1.pop());
s1.push(18); s1.push(72);
s1.push(517);
System.out.println(s).pop ()); } }
Ans. 5
27
517

You might also like