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 the algorithm need not be written.
Comments of Examiners
The concept of stack was not clear to most of the Suggestions for teachers
candidates. Common errors made by candidates were: More practice should be given in
(i) incorrect declaration of array; (ii) the condition / logic for data structure programs like the
underflow and overflow was not answered correctly; stacks, queues, dequeues, etc.
(iii) increment / decrement of top index was not done Working as to how the stack or a
properly; Top was initialized to 0 instead of -1 in some queue performs must be shown.
cases; The pushing of marks from array to stack was The concept of LIFO and FIFO
confusing to many candidates; Some sorted the array and must be explained to students
then pushed the values into the stack whereas in other cases with live examples. The concept
it was directly done; The methods pushmarks() and of ‘underflow’ and ‘overflow’
popmarks() were found to be difficult by some candidates. must be taught. The importance
The class declaration and constructors were well answered. of ‘top’ subscript must be
explained. Implementation of
stacks, queues and dequeue using
arrays should be emphasized.
Only the concept has to be
explained taking the base as an
array. It should be made clear to
students that it is not an array
related program which can be
manipulated by shifting /
inserting or initializing by any
value since these data structures
require pointers and pointers are
not supported in java. So, the
array is used to show the working
of a stack, queue or a dequeue.
MARKING SCHEME
Question 12.
import java.util.*;
class Array_to_Stack
{ int m[], st[];
int cap, top;
static Scanner sc=new Scanner(System.in);
Array_to_Stack(int n)
{ cap = n;
top = -1;
m=new int[cap];
165
st=new int[cap];
}
void input_marks( )
{ System.out.println("Enter "+cap+" elements in ascending order");
for(int i=0;i<cap;i++)
{ m[i]=sc.nextInt();
pushmarks(m[i]);
}
}
void pushmarks(int v)
{ if (top<cap-1)
st[++top]=v;
else
System.out.println("stack is full");
}
int popmarks()
{ if(top>=0)
returnst[top--];
else
return -999;
}
void display()
{ for(int i=top;i>=0;i--)
System.out.println(st[i]);
}
}
Question 13
:
(a) A linked list is formed from the objects of the class: [4]
class Node
{
int number;
Node nextNode;
}
Write an Algorithm OR a Method to add a node at the end of an existing linked list.
The method declaration is as follows:
void addnode ( Node start, int num )
(b) Define the terms complexity and big ‘O’ notation. [2]
166