Data Structures and Algorithms Lab Session 05 Mohammad Ali Jinnah University Name: M BILAL KHAN Student ID: FA18-BECE-0010 Lab 05 Linked List Implementation OBJECTIVE: To understand the basic concepts of the Implementation of Linked List and its searching. EXAMPLES: 1. EXERCISES: 2. 1) CODE: class LinkedList{ Node head; LinkedList(){ this.head=null; } } class Node{ int data; Node next; Node(int d){ data=d; next=null; } } public class Main{ public static void main(String[] args) { Data Structures and Algorithms Mohammad Ali Jinnah University LinkedList l1 = new LinkedList(); Node n1 = new Node (20); Node n2 = new Node (100); Node n3 = new Node (13); Node n4 = new Node (140); Node n5 = new Node (1050); l1.head = n1; n1.next = n2; n2.next = n3; n3.next = n4; n4.next = n5; Node temp = l1.head; while(temp != null){ System.out.println(temp.data); temp = temp.next; } } } OUTPUT: 2) CODE: class LinkedList { Node head; class Node Lab Session 05 Data Structures and Algorithms Mohammad Ali Jinnah University { int data; Node next; Node(int d) {data = d; next = null; } } public void push(int new_data) { Node new_node = new Node(new_data); new_node.next = head; head = new_node; } public void insertAfter(Node prev_node, int new_data) { if (prev_node == null) { System.out.println("The given previous node cannot be null"); return; } Node new_node = new Node(new_data); new_node.next = prev_node.next; prev_node.next = new_node; } public void append(int new_data) Lab Session 05 Data Structures and Algorithms Mohammad Ali Jinnah University { Node new_node = new Node(new_data); if (head == null) { head = new Node(new_data); return; } new_node.next = null; Node last = head; while (last.next != null) last = last.next; last.next = new_node; return; } public void DisplayList() { Node tnode = head; while (tnode != null) { System.out.print(tnode.data+" "); tnode = tnode.next; } } public class HelloWorld{ public void main(String[] args) { LinkedList llist = new LinkedList(); Lab Session 05 Data Structures and Algorithms Mohammad Ali Jinnah University llist.append(6); llist.push(7); llist.insertAfter(llist.head.next, 8); System.out.println("\nCreated Linked list is: "); llist.DisplayList(); } } } OUTPUT: The program run successfully but there was not output don’t know why 3) CODE: import java.util.Scanner; class Node { int data; Node next; Node(int d) { data = d; next = null; } } //Linked list class class LinkedList { Node head; public void push(int new_data) { Lab Session 05 Data Structures and Algorithms Mohammad Ali Jinnah University Node new_node = new Node(new_data); new_node.next = head; head = new_node; } public boolean search(Node head, int x) { Node current = head; //Initialize current while (current != null) { if (current.data == x) return true; //data found current = current.next; } return false; //data not found } public class main{ public static void main(String args[]) { LinkedList llist = new LinkedList(); llist.push(10); llist.push(30); llist.push(11); llist.push(21); llist.push(14); Lab Session 05 Data Structures and Algorithms Mohammad Ali Jinnah University Scanner input = new Scanner(System.in); System.out.print("Enter an integer: "); int number = input.nextInt(); if (llist.search(llist.head, number)) System.out.println("Yes"); else System.out.println("No"); } } } OUTPUT: Lab Session 05