Professional Documents
Culture Documents
1- Write a program to implement insert and delete nodes using singly linked list.
//data
int i;
Node next;
public class LinkedList {// create public class from type linked list to describe properties of node
public LinkedList(){
head = null;
tail = null;
}
public boolean isEmpty(){//function to check if linked list is empty
if(isEmpty()){
tail = newNode;
head = newNode;
size++;
if(isEmpty()){
head = newNode;
}else{
tail.next = newNode;
tail = newNode;
size++;
throw new IndexOutOfBoundsException("Index " + index +" not valid for linked list of size
" + size);
if(index == 0){
insertFirst(i);
// insert at last
insertLast(i);
temp = current;
temp.next = newNode;
throw new IndexOutOfBoundsException("Index " + index +" not valid for linked list of size
" + size);
current = current.next;
return current;
while(current != null){
current.displayData();
current = current.next;
System.out.println("");
if(head == null){
if(head.next == null){
tail = null;
}
head = head.next;
size--;
if(tail == null){
if(head.next == null){
head = null;
while(current != tail){
temp = current;
current = current.next;
tail = temp;
tail.next = null;
size--;
if(!isValidIndex(index +1)){
throw new IndexOutOfBoundsException("Index " + index +" not valid for linked list of size
" + size);
if(index == 0){
removeFirst();
// remove at last
removeLast();
}else{
temp = current;
current = current.next;
temp.next = current.next;
current.next = null;
size--;
}
public static void main(String[] args) {
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.displayData();
}
}
OUTPUT:
/*
*/
package linearsearch;
import java.util.Scanner;
int flag = 0;
int[] a = {10,15,19,45};
if (key == a[i])
flag = i;
break;
}
}
if (flag == 0)
else
} }