You are on page 1of 11

Answer the Question which carries Five Marks

1- Write a program to implement insert and delete nodes using singly linked list.

class Node{// create class from type node

//data

int i;

// Reference to next node

Node next;

public Node(int i){ //function to insert data to node

this.i = i;// use this to load from same variable

this.next = null;// to consider next current equal null

public void displayData(){//function to display data

System.out.print(" " + i);

public class LinkedList {// create public class from type linked list to describe properties of node

private Node head;

private Node tail;

private int size = 0;//counter of node

public LinkedList(){

head = null;

tail = null;

}
public boolean isEmpty(){//function to check if linked list is empty

return head == null;

public void insertFirst(int i){//function to insert value to node

//Create a new node

Node newNode = new Node(i);// create new node

if(isEmpty()){

tail = newNode;

newNode.next = head;//if not empty

head = newNode;

size++;

public void insertLast(int i){// function to insert node to last

Node newNode = new Node(i);// create new node

if(isEmpty()){

head = newNode;

}else{

tail.next = newNode;

tail = newNode;

size++;

public void insertAtIndex(int i, int index){//function to insert node in determine in place by


index
if(!isValidIndex(index)){//function to check index contain in linked list

throw new IndexOutOfBoundsException("Index " + index +" not valid for linked list of size
" + size);

Node newNode = new Node(i);

Node current = head;//consider node current equal to head

Node temp = head;//consider tempt to node equal heat

//insert at the start

if(index == 0){

insertFirst(i);

// insert at last

else if(index == size){

insertLast(i);

}else{// this operation to search on place node in linked list

for(int j = 0; j < index && current.next != null; j++){

temp = current;

current = current.next; temp current current.next

newNode.next = current;// this add node

temp.next = newNode;

size++; //increase counter

public Node get(int index){//get data node


if(!isValidIndex(index)){

throw new IndexOutOfBoundsException("Index " + index +" not valid for linked list of size
" + size);

Node current = head;

for(int j = 0; j < index; j++){

current = current.next;

return current;

} // Method to traverse and display all nodes

public void displayList(){//display data of node

Node current = head;

while(current != null){

current.displayData();

current = current.next;

System.out.println("");

public void removeFirst(){

if(head == null){

throw new RuntimeException("List is empty..");

// if there is only one node

if(head.next == null){

tail = null;
}

head = head.next;

size--;

public void removeLast(){

if(tail == null){

throw new RuntimeException("List is empty..");

Node current = head;

Node temp = head;

// if there is only one node

if(head.next == null){

head = null;

while(current != tail){

temp = current;

current = current.next;

tail = temp;

tail.next = null;

size--;

public void removeAtIndex(int index){

if(!isValidIndex(index +1)){
throw new IndexOutOfBoundsException("Index " + index +" not valid for linked list of size
" + size);

Node current = head;

Node temp = head;

//remove at the start

if(index == 0){

removeFirst();

// remove at last

else if(index == size - 1){

removeLast();

}else{

for(int j = 0; j < index && current.next != null; j++){

temp = current;

current = current.next;

temp.next = current.next;

current.next = null;

size--;

private boolean isValidIndex(int index){

return index >= 0 && index <= size;

}
public static void main(String[] args) {

LinkedList list = new LinkedList();

list.insertFirst(1);

list.insertLast(2);

list.insertLast(3);

list.insertLast(4);

list.insertLast(5);

System.out.println("After insertions--");

list.displayList();

list.removeLast();

System.out.println("After removal--");

list.displayList();

list.removeAtIndex(1);

System.out.println("After removal--");

list.displayList();

System.out.println("Get Node--");

Node node = list.get(1);

node.displayData();

}
}
OUTPUT:

2- Write a program to implement Linear Search using array with size 4.


import java.util.Scanner;

/*

* To change this license header, choose License Headers in Project Properties.

* To change this template file, choose Tools | Templates

* and open the template in the editor.

*/

package linearsearch;

import java.util.Scanner;

public class LinearSearch

public static void main(String[] args)

Scanner scan = new Scanner(System.in);

//System.out.println("Enter the number to search");

int key = 45;

int flag = 0;

int[] a = {10,15,19,45};

//Searching num using loop

for (int i = 0; i < a.length; i++)

if (key == a[i])

flag = i;

break;

}
}

if (flag == 0)

System.out.println(key+" is not present");

else

System.out.println(key+" is prestent at the index "+flag);

}   }

You might also like