ISC Practicals and Theory - Singly Linked List (OOP)
Updated 21 Oct 2026
A singly linked list is a chain of nodes in which each node stores a value and a reference to the next node, so elements can be added and removed without shifting any array. This version is written in the object-oriented style: the methods take the value as a parameter and return results (a boolean, a position, a count or the deleted value), and main reads the input and prints the messages.
Definition
A single linked list is made of nodes. Each node has data (the value) and next (a reference to the following node). The reference head points to the first node, and the next of the last node is null. An empty list has head == null. Nodes are created with new Node(...), so the list has no fixed size.
For example, the list holding 10, 20 and 30 is shown as:
10 -> 20 -> 30 -> null
Return values used here:
deleteValue(v)returnstrueif a node was deleted andfalseif the list is empty orvis not found.search(v)returns the position (the first node is position 1), or 0 ifvis not in the list.deleteFront()returns the deleted value, or -1 if the list is empty. As -1 can also be stored in a node,maincallsisEmpty()first.
Algorithm
- In
main, createobj = new SinglyList(); the constructor setshead = null. Repeat the menu until the choice is 8: print the menu, readchoiceand act on it. Any other number printsInvalid choice. Enter 1 to 8. - Choice 1: read
v, callinsertFront(v).- Create
p = new Node(v), setp.next = head, thenhead = p.
- Create
- Choice 2: read
v, callinsertEnd(v).- Create
p. Ifhead == null, sethead = p. - Otherwise start with
t = head, movet = t.nextwhilet.next != null, then sett.next = p.
- Create
- Choice 3: if
isEmpty()print a message; otherwisedeleteFront()saveshead.data, setshead = head.nextand returns the value. - Choice 4: read
v. If the list is empty print a message. Otherwise calldeleteValue(v):- If
head.data == v, sethead = head.nextand returntrue. - Otherwise move
tfromheadwhilet.next != nullandt.next.data != v. Ift.next == nullreturnfalse; else sett.next = t.next.nextand returntrue. Only the first node holdingvis deleted. mainprints the deleted message fortrueand the not-found message forfalse.
- If
- Choice 5: read
vand callsearch(v): start witht = headandpos = 1, returnposwhent.data == v, otherwise move on and increasepos; return 0 whentbecomesnull. - Choice 6:
display()prints eacht.datafollowed by->, thennull, or a message if the list is empty. - Choice 7:
countNodes()walks fromheadtonull, adds 1 tocper node and returnsc. - Choice 8 prints
Program ended.and the loop stops.
Program
Node builds one node. In SinglyList, isEmpty, search and countNodes return information, the insert and delete methods change the links, and main holds the one Scanner and prints all messages except the list display. main is in the class SinglyList, which is the public class.
import java.util.Scanner;
// A node of the list: holds one integer and a reference to the next node
class Node
{
int data; // the value stored in the node
Node next; // reference to the next node; null for the last node
// Constructor: makes a node holding d that points to nothing
Node(int d)
{
data = d;
next = null;
}
}
// The singly linked list: only the head (first node) is stored
public class SinglyList
{
Node head; // reference to the first node; null when the list is empty
// Constructor: starts with an empty list
SinglyList()
{
head = null;
}
// Returns true if the list has no node
boolean isEmpty()
{
return head == null;
}
// Inserts a new node holding v at the front of the list
void insertFront(int v)
{
Node p = new Node(v);
p.next = head; // new node points to the old first node
head = p; // new node becomes the first node
}
// Inserts a new node holding v at the end of the list
void insertEnd(int v)
{
Node p = new Node(v);
if (head == null)
{
head = p; // empty list: the new node is the only node
}
else
{
Node t = head;
while (t.next != null) // move t to the last node
{
t = t.next;
}
t.next = p; // last node now points to the new node
}
}
// Deletes the first node and returns its value.
// Returns -1 if the list is empty (the caller checks isEmpty() first, as -1 can also be a stored value)
int deleteFront()
{
if (head == null)
{
return -1;
}
int v = head.data;
head = head.next; // second node (or null) becomes the first node
return v;
}
// Deletes the first node holding v.
// Returns true if a node was deleted, false if the list is empty or v is not found
boolean deleteValue(int v)
{
if (head == null)
{
return false;
}
if (head.data == v)
{
head = head.next; // the first node holds the value
return true;
}
Node t = head;
while (t.next != null && t.next.data != v) // stop just before the node to delete
{
t = t.next;
}
if (t.next == null)
{
return false;
}
t.next = t.next.next; // bypass the node holding v
return true;
}
// Returns the position of v (first node is position 1), or 0 if v is not in the list
int search(int v)
{
Node t = head;
int pos = 1; // position of the node t
while (t != null)
{
if (t.data == v)
{
return pos;
}
t = t.next;
pos++;
}
return 0;
}
// Returns the number of nodes by walking from the head
int countNodes()
{
int c = 0;
Node t = head;
while (t != null)
{
c++;
t = t.next;
}
return c;
}
// Displays the list from the head, e.g. 10 -> 20 -> null
void display()
{
if (head == null)
{
System.out.println("The list is empty.");
}
else
{
Node t = head;
while (t != null)
{
System.out.print(t.data + " -> ");
t = t.next;
}
System.out.println("null");
}
}
// main: creates the list and runs the menu until Exit is chosen
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in); // the only Scanner of the program
SinglyList obj = new SinglyList();
int choice, v, pos;
do
{
System.out.println();
System.out.println("1. Insert at front");
System.out.println("2. Insert at end");
System.out.println("3. Delete from front");
System.out.println("4. Delete a value");
System.out.println("5. Search a value");
System.out.println("6. Display");
System.out.println("7. Count nodes");
System.out.println("8. Exit");
System.out.print("Enter your choice: ");
choice = sc.nextInt();
switch (choice)
{
case 1:
System.out.print("Enter the value to insert: ");
v = sc.nextInt();
obj.insertFront(v);
System.out.println("Inserted " + v + " at the front.");
break;
case 2:
System.out.print("Enter the value to insert: ");
v = sc.nextInt();
obj.insertEnd(v);
System.out.println("Inserted " + v + " at the end.");
break;
case 3:
if (obj.isEmpty())
{
System.out.println("The list is empty. Nothing to delete.");
}
else
{
System.out.println("Deleted " + obj.deleteFront() + " from the front.");
}
break;
case 4:
System.out.print("Enter the value to delete: ");
v = sc.nextInt();
if (obj.isEmpty())
{
System.out.println("The list is empty. Nothing to delete.");
}
else if (obj.deleteValue(v))
{
System.out.println("Deleted " + v + ".");
}
else
{
System.out.println(v + " not found in the list.");
}
break;
case 5:
System.out.print("Enter the value to search: ");
v = sc.nextInt();
pos = obj.search(v);
if (pos == 0)
{
System.out.println(v + " not found in the list.");
}
else
{
System.out.println(v + " found at position " + pos + ".");
}
break;
case 6:
obj.display();
break;
case 7:
System.out.println("Number of nodes: " + obj.countNodes());
break;
case 8:
System.out.println("Program ended.");
break;
default:
System.out.println("Invalid choice. Enter 1 to 8.");
}
} while (choice != 8);
}
}Variable description table
| Variable | Data Type | Purpose |
|---|---|---|
data (in Node) | int | Value stored in the node |
next (in Node) | Node | Reference to the next node; null for the last node |
d (in Node constructor) | int | Parameter: the value to store in the new node |
head (data member) | Node | Reference to the first node; null when the list is empty |
v (in insertFront) | int | Parameter: the value to insert |
p (in insertFront) | Node | The new node being inserted |
v (in insertEnd) | int | Parameter: the value to insert |
p (in insertEnd) | Node | The new node being inserted |
t (in insertEnd) | Node | Moves to the last node of the list |
v (in deleteFront) | int | Value of the first node, returned after deletion |
v (in deleteValue) | int | Parameter: the value to delete |
t (in deleteValue) | Node | Stops at the node just before the one to delete |
v (in search) | int | Parameter: the value to search |
t (in search) | Node | Moves along the list while searching |
pos (in search) | int | Position of the node t; the first node is position 1 |
c (in countNodes) | int | Number of nodes counted |
t (in countNodes) | Node | Moves along the list while counting |
t (in display) | Node | Moves along the list while printing |
args | String[] | Parameter: command line arguments of main |
sc | Scanner | The only Scanner of the program, reads all input |
obj | SinglyList | Object on which the methods are called |
choice | int | Menu choice entered by the user |
v (in main) | int | Value entered by the user |
pos (in main) | int | Position returned by search |
Sample input and output
Sample 1
1. Insert at front
2. Insert at end
3. Delete from front
4. Delete a value
5. Search a value
6. Display
7. Count nodes
8. Exit
Enter your choice: 1
Enter the value to insert: 20
Inserted 20 at the front.
1. Insert at front
2. Insert at end
3. Delete from front
4. Delete a value
5. Search a value
6. Display
7. Count nodes
8. Exit
Enter your choice: 1
Enter the value to insert: 10
Inserted 10 at the front.
1. Insert at front
2. Insert at end
3. Delete from front
4. Delete a value
5. Search a value
6. Display
7. Count nodes
8. Exit
Enter your choice: 2
Enter the value to insert: 30
Inserted 30 at the end.
1. Insert at front
2. Insert at end
3. Delete from front
4. Delete a value
5. Search a value
6. Display
7. Count nodes
8. Exit
Enter your choice: 6
10 -> 20 -> 30 -> null
1. Insert at front
2. Insert at end
3. Delete from front
4. Delete a value
5. Search a value
6. Display
7. Count nodes
8. Exit
Enter your choice: 7
Number of nodes: 3
1. Insert at front
2. Insert at end
3. Delete from front
4. Delete a value
5. Search a value
6. Display
7. Count nodes
8. Exit
Enter your choice: 5
Enter the value to search: 20
20 found at position 2.
1. Insert at front
2. Insert at end
3. Delete from front
4. Delete a value
5. Search a value
6. Display
7. Count nodes
8. Exit
Enter your choice: 3
Deleted 10 from the front.
1. Insert at front
2. Insert at end
3. Delete from front
4. Delete a value
5. Search a value
6. Display
7. Count nodes
8. Exit
Enter your choice: 6
20 -> 30 -> null
1. Insert at front
2. Insert at end
3. Delete from front
4. Delete a value
5. Search a value
6. Display
7. Count nodes
8. Exit
Enter your choice: 8
Program ended.Sample 2
1. Insert at front
2. Insert at end
3. Delete from front
4. Delete a value
5. Search a value
6. Display
7. Count nodes
8. Exit
Enter your choice: 6
The list is empty.
1. Insert at front
2. Insert at end
3. Delete from front
4. Delete a value
5. Search a value
6. Display
7. Count nodes
8. Exit
Enter your choice: 3
The list is empty. Nothing to delete.
1. Insert at front
2. Insert at end
3. Delete from front
4. Delete a value
5. Search a value
6. Display
7. Count nodes
8. Exit
Enter your choice: 7
Number of nodes: 0
1. Insert at front
2. Insert at end
3. Delete from front
4. Delete a value
5. Search a value
6. Display
7. Count nodes
8. Exit
Enter your choice: 4
Enter the value to delete: 10
The list is empty. Nothing to delete.
1. Insert at front
2. Insert at end
3. Delete from front
4. Delete a value
5. Search a value
6. Display
7. Count nodes
8. Exit
Enter your choice: 5
Enter the value to search: 10
10 not found in the list.
1. Insert at front
2. Insert at end
3. Delete from front
4. Delete a value
5. Search a value
6. Display
7. Count nodes
8. Exit
Enter your choice: 9
Invalid choice. Enter 1 to 8.
1. Insert at front
2. Insert at end
3. Delete from front
4. Delete a value
5. Search a value
6. Display
7. Count nodes
8. Exit
Enter your choice: 8
Program ended.Sample 3
1. Insert at front
2. Insert at end
3. Delete from front
4. Delete a value
5. Search a value
6. Display
7. Count nodes
8. Exit
Enter your choice: 2
Enter the value to insert: 10
Inserted 10 at the end.
1. Insert at front
2. Insert at end
3. Delete from front
4. Delete a value
5. Search a value
6. Display
7. Count nodes
8. Exit
Enter your choice: 2
Enter the value to insert: 20
Inserted 20 at the end.
1. Insert at front
2. Insert at end
3. Delete from front
4. Delete a value
5. Search a value
6. Display
7. Count nodes
8. Exit
Enter your choice: 2
Enter the value to insert: 30
Inserted 30 at the end.
1. Insert at front
2. Insert at end
3. Delete from front
4. Delete a value
5. Search a value
6. Display
7. Count nodes
8. Exit
Enter your choice: 4
Enter the value to delete: 20
Deleted 20.
1. Insert at front
2. Insert at end
3. Delete from front
4. Delete a value
5. Search a value
6. Display
7. Count nodes
8. Exit
Enter your choice: 4
Enter the value to delete: 99
99 not found in the list.
1. Insert at front
2. Insert at end
3. Delete from front
4. Delete a value
5. Search a value
6. Display
7. Count nodes
8. Exit
Enter your choice: 4
Enter the value to delete: 10
Deleted 10.
1. Insert at front
2. Insert at end
3. Delete from front
4. Delete a value
5. Search a value
6. Display
7. Count nodes
8. Exit
Enter your choice: 4
Enter the value to delete: 30
Deleted 30.
1. Insert at front
2. Insert at end
3. Delete from front
4. Delete a value
5. Search a value
6. Display
7. Count nodes
8. Exit
Enter your choice: 6
The list is empty.
1. Insert at front
2. Insert at end
3. Delete from front
4. Delete a value
5. Search a value
6. Display
7. Count nodes
8. Exit
Enter your choice: 3
The list is empty. Nothing to delete.
1. Insert at front
2. Insert at end
3. Delete from front
4. Delete a value
5. Search a value
6. Display
7. Count nodes
8. Exit
Enter your choice: 7
Number of nodes: 0
1. Insert at front
2. Insert at end
3. Delete from front
4. Delete a value
5. Search a value
6. Display
7. Count nodes
8. Exit
Enter your choice: 8
Program ended.Sample 4
1. Insert at front
2. Insert at end
3. Delete from front
4. Delete a value
5. Search a value
6. Display
7. Count nodes
8. Exit
Enter your choice: 2
Enter the value to insert: 5
Inserted 5 at the end.
1. Insert at front
2. Insert at end
3. Delete from front
4. Delete a value
5. Search a value
6. Display
7. Count nodes
8. Exit
Enter your choice: 5
Enter the value to search: 7
7 not found in the list.
1. Insert at front
2. Insert at end
3. Delete from front
4. Delete a value
5. Search a value
6. Display
7. Count nodes
8. Exit
Enter your choice: 3
Deleted 5 from the front.
1. Insert at front
2. Insert at end
3. Delete from front
4. Delete a value
5. Search a value
6. Display
7. Count nodes
8. Exit
Enter your choice: 3
The list is empty. Nothing to delete.
1. Insert at front
2. Insert at end
3. Delete from front
4. Delete a value
5. Search a value
6. Display
7. Count nodes
8. Exit
Enter your choice: 3
The list is empty. Nothing to delete.
1. Insert at front
2. Insert at end
3. Delete from front
4. Delete a value
5. Search a value
6. Display
7. Count nodes
8. Exit
Enter your choice: 6
The list is empty.
1. Insert at front
2. Insert at end
3. Delete from front
4. Delete a value
5. Search a value
6. Display
7. Count nodes
8. Exit
Enter your choice: 8
Program ended.Dry run
Take the first sample. Each row shows the list after the operation.
| Operation | Returned | List after the operation | Note |
|---|---|---|---|
| Start | null | head == null | |
insertFront(20) | 20 -> null | New node points to the old head (null) | |
insertFront(10) | 10 -> 20 -> null | p.next = head, then head = p | |
insertEnd(30) | 10 -> 20 -> 30 -> null | t stops at 20, t.next = p | |
display() | 10 -> 20 -> 30 -> null | Printed as is | |
countNodes() | 3 | 10 -> 20 -> 30 -> null | Three nodes walked |
search(20) | 2 | 10 -> 20 -> 30 -> null | 10 is position 1, 20 is position 2 |
deleteFront() | 10 | 20 -> 30 -> null | head = head.next |
display() | 20 -> 30 -> null | Final list |
The final display is 20 -> 30 -> null, which matches the first sample run.
Variations
- Insert at a position "Write a program to insert a node at a given position of a singly linked list."
- Reverse the list "Write a program to reverse a singly linked list by changing the links."
- Delete the last node "Write a method to delete the last node of a singly linked list."