You are on page 1of 6

LAB # 08

Doubly Linked List implementation of the list ADT


OBJETIVE:
Implementing doubly linked list and associated operations.

LAB TASK#1:
Write a program that can insert the records of employees in a link list. The record includes
employees’ name, designation, department and company name.
a. The program should be able to insert the record as first, last and as middle node in the
list.
b. The program should be able to search any record.

CODE:

package doublylinkedlist;
public class DOUBLYLINKEDLIST {
class Link {
String item;
Link previous;
Link next;
public Link(String item) {
this.item = item;
}
}
Link head, tail = null;
public void addNode(String data) {
Link newLink = new Link(data);
if(head == null) {
head = tail = newLink;
head.previous = null;
tail.next = null;
}
else {
tail.next = newLink;
newLink.previous = tail;
tail = newLink;
tail.next = null;
}
}
public void insertAt(int index, String data){
int counter = 0;
Link newLink = new Link(data);
Link link = head;
if(head == null){
head = newLink;
tail = newLink;
return;
}
while(counter<index-1){
link = link.next;
counter++;
}
Link temp = link.next;
link.next = newLink;
link.next.next = temp;
} public void print() {
Link current = head;
if(head == null) {
System.out.println("Doubly linked list is empty");
return; }
while(current != null) {
System.out.print(current.item + " ");
current = current.next;
}
}
public void search(String data){
if(head == null){
System.out.println("List is empty.");
return;
}
int counter = 0;
Link link = head;
int pos = -1;
while(link != null && pos == -1){
if(data == link.item){
pos = counter+1;
break;
}
link = link.next;
counter++;
}
if(pos != -1){
System.out.println(data+" Found at position: "+pos);
}
else{
System.out.println("Item not Found!");
}

}
public static void main(String[] args) {
DOUBLYLINKEDLIST dll = new DOUBLYLINKEDLIST();
dll.addNode("BABER AZAM,");
dll.addNode("MANAGER,");
dll.addNode("UNI LIVER");
dll.print();
System.out.println("\nInsertion\n=========");
dll.insertAt(2,"HR");
dll.print();
System.out.println("\nSearching\n=========");
dll.search("BABER AZAM,");
}
}
OUTPUT:
HOME TASK
TASK:
Write a program which includes these 3 classes
a) Create a class DNode with all the methods defined above.
b) Create a class DoublyLL to implement all the given operations.
c) Write a Demo class to create
o Unsorted Linked List
o Sorted linked list
STEP 1:
Create a class DNode with all the methods defined above.
CODE:
public class DNode {
static class DoublyLinkedList {
class Link {
int item;
Link previous;
Link next;
public Link(int item) {
this.item = item;
}
}
Link head, tail = null;
public void addNode(int data) {
Link newLink = new Link(data);
if (head == null) {
head = tail = newLink;
head.previous = null;

tail.next = null;
}
else {
tail.next = newLink;
newLink.previous = tail;
tail = newLink;
tail.next = null;
}
}
public void insertAt(int index, int data) {
int counter = 0;
Link newLink = new Link(data);
Link link = head;
if (head == null) {
head = newLink;
tail = newLink;
return;
}
while (counter < index - 1) {
link = link.next;
counter++;
}
Link temp = link.next;
link.next = newLink;
link.next.next = temp;
}
public void print() {
Link current = head;
if (head == null) {
System.out.println("Doubly linked list is empty");
return;
}
System.out.println("Nodes of doubly linked list: ");
while (current != null) {
System.out.print(current.item + " ");
current = current.next;
}
}
public void search(int data) {
if (head == null) {
System.out.println("List is empty.");
return;
}
int counter = 0;
Link link = head;
int pos = -1;
while (link != null && pos == -1) {
if (data == link.item) {
pos = counter + 1;
break;
}
link = link.next;
counter++;
}
if (pos != -1) {
System.out.println(data + " Found at position: " + pos);
}
else {
System.out.println("Item not Found!");
}
}
}
}

STEP 2:
Create a class DoublyLL to implement all the given operations.
SOURCE CODE:
public class Main {
public static void main(String[] args) {
DNode.DoublyLinkedList dll = new DNode.DoublyLinkedList();
dll.addNode(2);
dll.addNode(3);
dll.addNode(9);
dll.print();
System.out.println();
dll.insertAt(2,5);
dll.print();
System.out.println();
dll.search(9);
}
}

STEP 3:
Write a Demo class to create
o Unsorted Linked List
o Sorted linked list
SOURCE CODE:
public class Demo {
public static void main(String[] args) {
DNode.DoublyLinkedList unSorted = new DNode.DoublyLinkedList();
unSorted.addNode(9);
unSorted.addNode(3);
unSorted.addNode(2);
DNode.DoublyLinkedList sorted = new DNode.DoublyLinkedList();
sorted.addNode(2);
sorted.addNode(3);
sorted.addNode(9);
System.out.println("Unsorted Linked List:");
unSorted.print();
System.out.println("\nSorted Linked List:");
sorted.print();
}
}

OUTPUT:

You might also like