LAB FINAL
Name: Muhammad Umer Farooq malik
Reg-No:SP19-BCS-036
Section: 4B
Singly Linked List
Program 1:
public class LinkedListProgram1<T> {
private LinkedListNode<T> first = null;
//Insert at the front of the list
public void insert(LinkedListNode<T> node) {
node.setNext(first);
first = node;
//Remove from the front of the list
public void remove(){
if(first.getNext()!=null)
first = first.getNext();
else first = null;
//Recursively traverse this list and print the node value
private void printList(LinkedListNode<T> node) {
System.out.println("Node is " + node.getValue());
if(node.getNext()!=null) printList(node.getNext());
public void print(){
printList(first);
}
public static void main(String[] args) {
LinkedList<String> list = new LinkedList<String>();
list.insert(new LinkedListNode<String>("Manish"));
list.insert(new LinkedListNode<String>("Pandit"));
list.insert(new LinkedListNode<String>("Tanvi"));
list.insert(new LinkedListNode<String>("Monika"));
list.print();
list.remove();
System.out.println("After removing the head..");
list.print();
class LinkedListNode<T> {
private T value;
private LinkedListNode<T> next;
public LinkedListNode(T value) {
this.value = value;
public void setNext(LinkedListNode<T> next) {
this.next = next;
}
public LinkedListNode<T> getNext() {
return next;
public T getValue() {
return value;
Program 2:
public class SinglyLinkedListProgram2 {
private ObjectNode head_ptr;
private ObjectNode tail_ptr;
private ObjectNode iter;
private int countNodes;
public static void main(String[] args) {
SinglyLinkedList s = new SinglyLinkedList();
s.addAtFrontNode("c");
s.addAtEndNode("x");
s.addAtFrontNode("b");
s.addAtEndNode("y");
s.addAtFrontNode("a");
s.addAtEndNode("z");
s.reset();
System.out.println("The String is: ");
while (s.hasNext()) {
System.out.print(s.next());
System.out.println();
System.out.println("Number of nodes is: ");
System.out.println(s.countNodes());
System.out.println("The object at position 3 (String starts from position 0: ");
Object check = s.getObjectAt(3);
System.out.println(check.toString());
/* Constructor Method to initialise the list
* Big-theta value =(1),where n=length of the list
*/
public SinglyLinkedList() {
head_ptr = null;
tail_ptr = null;
countNodes = 0;
iter = null;
/* Method to add a node at the tail of the list
* Big-theta value =(1),where n=length of the list
*/
public void addAtEndNode(Object data) {
ObjectNode head = new ObjectNode(data, null);
countNodes++;
if (head_ptr == null) {
head_ptr = head;
tail_ptr = head_ptr;
else {
tail_ptr.setLink(head);
tail_ptr = head;
/* Method to add a node at the front of the list
* Big-theta value =(1),where n=length of the list
*/
public void addAtFrontNode(Object data) {
ObjectNode head = new ObjectNode(data, null);
countNodes++;
if (head_ptr == null) {
head_ptr = head;
tail_ptr = head_ptr;
} else {
head.setLink(head_ptr);
head_ptr = head;
/* Overridden Method toString to display the list
* Big-theta value =(n),where n=length of the list
*/
public String toString() {
ObjectNode cursor = head_ptr;
String display = "";
for (cursor = head_ptr.link; cursor != null; cursor = cursor.link) {
display = cursor.data.toString();
System.out.print(display);
}
return display;
/* Method to get the object at a particular position of the node
* Big-theta value =(n),where n=position
*/
public Object getObjectAt(int i) {
ObjectNode o = null;
int count = 0;
if (i < countNodes()) {
o = head_ptr;
if (i == 0) {
return o.getData();
} else {
while (count != i) {
o = o.link;
count++;
return o.getData();
}
/* Method to set the iterator at the head pointer of the list
* Big-theta value =(1)
*/
public void reset() {
iter = head_ptr;
/* Returns the boolean value based on whether the next node exists
* Big-theta value =(n)
*/
public boolean hasNext() {
boolean b = iter != null;
return b;
/*
* Method to return the Object of the next node
*/
public Object next() {
Object nextNodeData;
if (iter == null)
iter = head_ptr;
nextNodeData = iter.data;
iter = iter.link;
return nextNodeData;
/*
* Method that returns the size of the list
*/
public int countNodes() {
return countNodes;
/*
* Method that returns the last node of the list
*/
public ObjectNode getLast() {
return tail_ptr;