Professional Documents
Culture Documents
19sw33 Lab 5dsa
19sw33 Lab 5dsa
CODE:
package LABNO5_TASKS;
class LinkedList{
Node head;
class Node{
private int data;
private Node next;
Node(int data){
this.data = data;
}
Node(int data, Node next){
this.data = data;
this.next = next;
}
}
public Node insert(int x){
Node p = head;
if(head == null || head.data > x){
head = new Node(x, head);
return head;
}
else{
while(p.next!=null){
if(p.next.data > x) break;
p = p.next;
}
}
p.next = new Node(x,p.next);
return head;
}
public Node delete(int x){
Boolean targetFound = false;
Node p = head;
if(head == null || head.data>x){
System.out.println(x+ " list doesn't exist!");
return head;
}
if(head != null && head.next==null){
if(head.data==x){
System.out.println(x+ " list has been deleted!");
head = head.next;
return head;
}
else{
System.out.println(x+ " doesn't exist in the list!");
return head;
}
}
else{
while(p.next!=null)
{
if(p.next.data>x) break;
if(p.next.data == x){
System.out.println(x+ " has been deleted!");
targetFound = true;
p.next = p.next.next;
return head;
}
if(head.next.data!=x){
targetFound = false;
}
p = p.next;
}
}
if(targetFound==false)
{
System.out.println(x + " doesn't exist in list to delete!");
return head;
}
return head;
}
public int search(int x){
int index = 0;
Boolean isPresent = false;
Node p = head;
if(head != null){
if(head.data==x)return 1;
else{
++index;
}
}
while(p.next!=null)
{
++index;
if(p.next.data>x)return 0;
if(p.next.data==x) {
isPresent = true;
return index;
}
else{
isPresent = false;
}
p = p.next;
}
if(isPresent==false)return 0;
return index+1;
}
public int size(){
int size=0;
if(head!=null) size=1;
if(head==null) return 0;
Node p = head;
while(p.next!=null){
++size;
p=p.next;
}
return size;
}
public int sum(){
int sum=0;
if(head==null) return 0;
if(head!=null) sum += head.data;
Node p = head;
while(p.next!=null){
sum += p.next.data;
p = p.next;
}
return sum;
}
public void deleteLast(){
int size = this.size();
int count=0;
if(head==null){
System.out.println("Your list is empty you can not delete.");
return;
}
if(head!=null && head.next==null){
head = null;
return;
}
for(Node p=head; p!=null; p=p.next){
++count;
if(count==size-1){
p.next=null;
}
}
}
public LinkedList copy(){
LinkedList newList = new LinkedList();
Node p = head;
if(head==null){
return newList;
}
if(head!=null){
newList.insert(head.data);
}
else if(head!=null && head.next==null){
newList.insert(head.data);
return newList;
}
while(p.next!=null){
newList.insert(p.next.data);
p = p.next;
}
return newList;
}
if(head!=null){
counter++;
if(counter>=p){
newList.insert(head.data);
}
}
while(n.next!=null)
{
counter++;
if(counter>=p){
newList.insert(n.next.data);
}
if(counter==q){
break;
}
n = n.next;
}
return newList;
}
public LinkedList merged(LinkedList l){
LinkedList list = new LinkedList();
/*** For testing the insertion method we'll insert the elements in the
list ***/
list.insert(7);
list.insert(2);
list.insert(15);
list.insert(20);
list.insert(45);
list.insert(32);
System.out.print("List is : ");
list.printList();
System.out.println();
/** Testing size() method to find the size of the linked list */
System.out.println("list.size() : " + list.size());
/** Testing sum() method to find the size of the linked list */
System.out.println("list.sum() : " + list.sum());
/** Now deleting elements in the list to test the insert method */
System.out.print("list.delete(3) : ");
list.delete(5);
/** Printing the list of nodes containing different nodes and values */
System.out.print("Now, list is : ");
list.printList();
System.out.println();
TASK#2
Implement a linked list for Student class. Student class
should have roll_num and name as instance variables. The linked
list should have the following operations:
◦ insert (Student s)
◦ delete (Student s)
◦ printList () // print the roll numbers and names of every
student in the list
CODE:
package LABNO5_TASKS;
class List {
Node head;
class Node{
String name, roll_num;
Node next;
Node(String name, String roll_num){
this.name = name;
this.roll_num = roll_num;
}
Node(String name, String roll_num, Node next){
this.name = name;
this.roll_num = roll_num;
this.next = next;
}
}
public void insert(Student s){
if(head==null){
head = new Node(s.name, s.roll_num, head);
}
head.next = new Node(s.name,s.roll_num,head.next);
}
public void delete(Student s){
if(head==null){
System.out.println("List is empty you can not delete anything!");
return;
}
else
if(head!=null&&head.name.equals(s.name)&&head.roll_num.equals(s.roll_num)){
head = head.next;
}
Node p = head;
while(p.next!=null)
{
if(p.next.name.equals(s.name)&&p.next.roll_num.equals(s.roll_num)){
p.next = p.next.next;
}
p = p.next;
}
}
public void printList(){
Node p = head;
while(p.next!=null){
System.out.print(p.next.name + "-" + p.next.roll_num+" ");
p = p.next;
}
}
}
public class Student {
list.insert(s1);
list.insert(s2);
list.insert(s3);
list.insert(s4);
list.insert(s5);
list.insert(s6);
System.out.println("Student list with respective roll numbers : ");
list.printList();
System.out.println();
list.delete(s3);
System.out.println("After deletion of Mark from List : ");
list.printList();
}
}
OUTPUT:
TASK#3
Explore java. util.LinkedList class; Create a linked list of
type String using this class and apply any 5 of its methods
CODE:
package LABNO5_TASKS;
import java.util.LinkedList;
public class Task3 {
public static void main(String mylist[]){
list.add("Chocolate");
list.add("Biscuits");
list.add("Lays");
list.add("Coca Cola");
list.add("Peanuts");
System.out.println("0. List : " + list);
}
}
OUTPUT: