You are on page 1of 7

DATA STRUCTURES AND ALGORITHMS LAB

Mahnoor Iftikhar
Home Assignment 2
SINGLY LINKED LIST IMPLEMENTATION

Task # 1:

Implement the isEmpty() method in the Linked List

// isEmpty() method in List interface

import java.util.*;

public class GFG {

public static void main(String[] args)

// creating an Null Integer List

List<Integer> arr = new ArrayList<Integer>(10);

// check if the list is empty or not

// using isEmpty() fucntion

boolean ans = arr.isEmpty();

if (ans == true)

System.out.println("List is completely utter empty empty");

else

System.out.println(" List is not seems to be empty");

// addition of a element to

// the List
arr.add(1);

// check if the list is empty or not

// after adding an element

ans = arr.isEmpty();

if (ans == true)

System.out.println("The List is empty");

else

System.out.println("The List is not empty");

Task # 2:
Implement the getSize() method in the Linked List

import java.io.*;

import java.util.LinkedList;

public class LinkedListDemo {

public static void main(String args[])

// Creating an empty LinkedList

LinkedList<String> list = new LinkedList<String>();

// Using add() method to add elements in the list

list.add("Geeks");

list.add("for");
list.add("Geeks");

list.add("10");

list.add("20");

// Displaying the linkedlist

System.out.println("LinkedList:" + list);

// Displaying the size of the list

System.out.println("The size of the linked list is: "

+ list.size());

Task#3

Implement the deleteAtPosition() method in the Linked


List

public static LinkedList deleteAtPosition(LinkedList list, int index)

// Store head node

Node currNode = list.head, prev = null;

//

// CASE 1:

// If index is 0, then head node itself is to be deleted

if (index == 0 && currNode != null) {

list.head = currNode.next; // Changed head


// Display the message

System.out.println(index + " position element deleted");

// Return the updated List

return list;

//

// CASE 2:

// If the index is greater than 0 but less than the size of LinkedList

//

// The counter

int counter = 0;

// Count for the index to be deleted,

// keep track of the previous node

// as it is needed to change currNode.next

while (currNode != null) {

if (counter == index) {

// Since the currNode is the required position

// Unlink currNode from linked list

prev.next = currNode.next;

// Display the message

System.out.println(index + " position element deleted");

break;

else {
// If current position is not the index

// continue to next node

prev = currNode;

currNode = currNode.next;

counter++;

// If the position element was found, it should be at currNode

// Therefore the currNode shall not be null

//

// CASE 3: The index is greater than the size of the LinkedList

//

// In this case, the currNode should be null

if (currNode == null) {

// Display the message

System.out.println(index + " position element not found");

// return the List

return list;

Task # 4:

Implement the insertAtLast() method in the Linked List

public static void insertAtLast(Node head,int element)

{
Node newNode=new Node(0,null);

newNode.data = element;

newNode.next = null;

Node temp = head;

while(temp.next != null)

temp = temp.next;

temp.next = newNode;

//return head;

Task#5

Implement the deleteFirst() method in the Linked List

public class DeleteStart {

//Represent a node of the singly linked list

class Node{

int data;

Node next;

public Node(int data) {

this.data = data;

this.next = null;

//Represent the head and tail of the singly linked list


public Node head = null;

public Node tail = null;

//addNode() will add a new node to the list

public void addNode(int data) {

//Create a new node

Node newNode = new Node(data);

//Checks if the list is empty

if(head == null) {

//If list is empty, both head and tail will point to new node

head = newNode;

tail = newNode;

else {

//newNode will be added after tail such that tail's next will point to newNode

tail.next = newNode;

//newNode will become new tail of the list

tail = newNode;

Task#6

Tried hard. ! Can’t do 

You might also like