Java exercises and solutions: element of singly linked list

Java OOP: singly linked list

Element of singly linked list

Step 1: Define element of the singly linked list

The linked list element has two parts--data and pointer or link. Therefore, we define the the element of the linked list by using a class that has two members--data and next pointer.

C single linked list

// define list element

class ListNode<T>{ //T is a generic type
ListNode(T elem) { val = elem; next = null; }
public T val; //node data
public ListNode<T> next;//next link

The list elements are chained together in the linked list. Therefore, we need another class called SLinkedList that inherits few useful methods and variables from the abstract class ClsList .

//Abstract class ClsList

abstract class ClsList<T>{
protected ListNode<T> pfirst; //stores the first element
protected ListNode<T> plast; //stores the last element

public void insert(int pos){


public int countitem(){
return 0;

public void delete(int pos){


public void printall(){


To inherit the methods and variables from the ClsList class, you will need the following Java code:

public class SLinkedList<T> extends ClsList<T>{

//implementation of the methods is here


The implementation of the methods are discussed in the next pages.


praveenkumar comment


i could not understand when generic concepts comingg like<T>


This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.