You are on page 1of 7

Lab # 8: Doubly Linked List Implementation SSUET/QR/114

LAB 8
OBJECT:

Implementing Doubly linked list and associated operations.

Lab Task

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.

SOURCE CODE:

public class lab8_1 {


public static void main(String[] args) {
lab8_1 dll = new lab8_1();
dll.addNode("Murtaza");
dll.addNode("SE20F-177");
dll.addNode("SOftware Engineering");
dll.print();
System.out.println("\nInsertion\n=========");
dll.insertAt(3,"SSUET");
dll.print(); System.out.println("\nSearching\
n========="); dll.search("SOftware
Engineering");
}

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);

SWE-203L: Data Structures and Algorithms Page 1


Lab # 8: Doubly Linked List Implementation SSUET/QR/114

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;
}
}

SWE-203L: Data Structures and Algorithms Page 2


Lab # 8: Doubly Linked List Implementation SSUET/QR/114

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!");
}
}
}

OUTPUT:

SWE-203L: Data Structures and Algorithms Page 3


Lab # 8: Doubly Linked List Implementation SSUET/QR/114

Home 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

SOURCE CODE:

public class lab8_2 {


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) {

SWE-203L: Data Structures and Algorithms Page 4


Lab # 8: Doubly Linked List Implementation SSUET/QR/114

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++;
}

SWE-203L: Data Structures and Algorithms Page 5


Lab # 8: Doubly Linked List Implementation SSUET/QR/114

if (pos != -1) {
System.out.println(data + " Found at position: " + pos);
} else {
System.out.println("Item not Found!");
}
}
}
}

public class Demo {


public static void main(String[] args) {
lab8_2.DoublyLinkedList unSorted = new lab8_2.DoublyLinkedList();
unSorted.addNode(6);
unSorted.addNode(3);
unSorted.addNode(1);
lab8_2.DoublyLinkedList sorted = new lab8_2.DoublyLinkedList();
sorted.addNode(1);
sorted.addNode(2);
sorted.addNode(3);
System.out.println("Unsorted Linked List:");
unSorted.print(); System.out.println("\
nSorted Linked List:"); sorted.print();
}
}

public class MainMethod {


public static void main(String[] args) {
lab8_2.DoublyLinkedList dll = new lab8_2.DoublyLinkedList();
dll.addNode(1);
dll.addNode(2);
dll.addNode(3);
dll.print();
System.out.println();
dll.insertAt(3,4);
dll.print();
System.out.println();
dll.search(1);
}
}

SWE-203L: Data Structures and Algorithms Page 6


Lab # 8: Doubly Linked List Implementation SSUET/QR/114

OUTPUT:

SWE-203L: Data Structures and Algorithms Page 7

You might also like