Stack, Queue and Linked List in Java for ISC Class 12 (Notes and Programs)
ISC Class 12 Computer Science notes on data structures: stacks (LIFO) and queues (FIFO) using arrays with overflow and underflow, postfix evaluation, and singly linked lists – insert, traverse, count and search – with board-style Java programs.

Section C of the ISC Computer Science theory paper always includes data structures: designing a stack or queue class, and writing linked list methods or algorithms. These questions follow predictable patterns, so learning the standard code properly is the fastest way to secure those marks.
Stack (LIFO)
A stack is a linear data structure where insertion (push) and deletion (pop) happen at the same end, called the top. The last element pushed is the first one popped: Last In, First Out (LIFO).
- Overflow: pushing onto a full stack.
- Underflow: popping from an empty stack.
Stack class (ISC style)
class Stack {
int arr[];
int cap, top;
Stack(int c) {
cap = c;
arr = new int[cap];
top = -1; // empty stack
}
void push(int v) {
if (top == cap - 1)
System.out.println("STACK OVERFLOW");
else
arr[++top] = v;
}
int pop() {
if (top == -1) {
System.out.println("STACK UNDERFLOW");
return -9999;
}
return arr[top--];
}
void display() {
if (top == -1)
System.out.println("STACK EMPTY");
for (int i = top; i >= 0; i--) // print from top to bottom
System.out.println(arr[i]);
}
}Applications of stacks: reversing a string, undo operations, matching brackets, recursion (the call stack), and evaluating postfix expressions.
Evaluating a postfix expression
Scan from left to right. Push each operand. When you reach an operator, pop two operands, apply the operator, and push the result.
**Evaluate 5 3 + 2 *:**
Symbol Action Stack
5 push 5
3 push 5 3
+ pop 3, 5 → 5+3=8 8
2 push 8 2
* pop 2, 8 → 8*2=16 16
Result = 16When popping for a non-commutative operator (− or /), the first value popped is the second operand. For 8 2 −, pop 2 and then 8, and compute 8 − 2 = 6.
Queue (FIFO)
A queue inserts at the rear and deletes from the front: First In, First Out (FIFO), like a line at a ticket counter.
class Queue {
int q[];
int size, front, rear;
Queue(int s) {
size = s;
q = new int[size];
front = rear = -1; // empty queue
}
void add(int v) {
if (rear == size - 1)
System.out.println("QUEUE OVERFLOW");
else {
if (front == -1)
front = 0;
q[++rear] = v;
}
}
int delete() {
if (front == -1 || front > rear) {
System.out.println("QUEUE UNDERFLOW");
return -9999;
}
int v = q[front++];
if (front > rear) // queue became empty: reset
front = rear = -1;
return v;
}
}Limitation of a linear queue: after some deletions, the space at the front cannot be reused, so the queue can report overflow even though cells are free. A circular queue fixes this by wrapping the rear around: rear = (rear + 1) % size.
Stack vs queue
- Order: a stack is LIFO; a queue is FIFO.
- Ends used: a stack inserts and deletes at the top only; a queue inserts at the rear and deletes at the front.
- Pointers: a stack needs one (
top); a queue needs two (frontandrear). - Example: a stack is like a pile of plates; a queue is like a line of people.
Singly linked list
A linked list is a chain of nodes. Each node holds data and a reference to the next node. The last node points to null. Unlike an array, it grows and shrinks at run time.
class Node {
int data;
Node next;
Node(int d) {
data = d;
next = null;
}
}Insert at the beginning
Node insertFirst(Node start, int v) {
Node n = new Node(v);
n.next = start; // the new node points to the old first node
return n; // the new node becomes the start
}Traverse and display
void display(Node start) {
Node temp = start;
while (temp != null) {
System.out.print(temp.data + " -> ");
temp = temp.next;
}
System.out.println("null");
}Count the nodes
int count(Node start) {
int c = 0;
for (Node t = start; t != null; t = t.next)
c++;
return c;
}Search for a value
boolean search(Node start, int key) {
for (Node t = start; t != null; t = t.next)
if (t.data == key)
return true;
return false;
}Array vs linked list
- Size: an array has a fixed size; a linked list is dynamic and can grow or shrink.
- Access: an array gives direct access to any element by index; a linked list must be traversed from the start.
- Insertion and deletion: slow in an array, because elements must be shifted; fast in a linked list, because only links change.
- Memory: an array stores only the data; each linked list node also stores a reference.
Exam tips
- Initialise
top = -1for a stack andfront = rear = -1for a queue, and test those exact conditions for underflow. - Print the exact messages the question asks for, such as "STACK OVERFLOW".
- For linked lists, always use a temporary pointer to traverse. Never move
startitself, or you will lose the list. - If asked for an algorithm instead of code, write numbered steps such as "Step 1: If top = −1, display Underflow and exit."
Revise with Abstract Classes and Interfaces and practise these questions in the solved ISC Class 12 Computer Science Sample Paper 2027.
Related Posts
Continue learning with these related articles

Logic Gates for ISC Class 11: Truth Tables, Universal Gates, Half and Full Adder
ISC Class 11 Computer Science notes on logic gates: AND, OR, NOT, NAND, NOR, XOR and XNOR with truth tables, why NAND and NOR are universal gates, and half adder and full adder circuits with their Boolean expressions.
Decimal to Binary Conversion in Java: 3 Programs with Examples
How to convert a decimal number to binary on paper and in Java: three programs (String, place value and Integer.toBinaryString), binary to decimal, a dry run and common mistakes.

ISC Class 11 Computer Science Sample Paper 2026-27 with Solutions
A full 70-mark ISC Class 11 Computer Science sample paper for the 2026-27 annual exam: MCQs, number systems, propositional logic, logic gates, Java classes, arrays, strings, searching and sorting, and computing ethics, with complete worked solutions.