Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

TASK#1

Write and test the following methods:


1. int search (int x) //returns the index of element in the linked list
2. int size () //returns the size of the linked list
3. int sum () //returns the sum of all numbers in the linked list
4. void deleteLast () //deletes the last node in the list
5. LinkedList copy () //returns a new linked list that is the duplicate
of the list this method is called up on.
6. LinkedList subList (int p, int q) //returns a new linked list that contains element from node
p to node q of the list this method is called up on.
7. void append (LinkedList l) //l is appended to the list this method is called on.
8. LinkedList merged (Linked l) // returns a new list that merges l with the list the method is
called on; maintaining the ascending order.

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

public void append(LinkedList list){


Node p = this.head, q = list.head;
while(p.next!=null){
p = p.next;
}
if(p.next == null){
this.insert(list.head.data);
while(q.next!=null){
this.insert(q.next.data);
q = q.next;
}
}
}
public LinkedList subList(int p, int q){
LinkedList newList = new LinkedList();
Node n = head;
int counter = 0;
if(head==null){
System.out.println("Sub lis can not be created because list is
empty !");
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();

Node p = this.head, q = l.head;


if(p!=null){
list.insert(p.data);
}
while(p.next!=null){
list.insert(p.next.data);
p = p.next;
}
if(p.next == null){
list.insert(l.head.data);
while(q.next!=null){
list.insert(q.next.data);
q = q.next;
}
}
return list;
}
public void printList(){
for(Node i=head; i!=null; i=i.next){
System.out.print(i.data + " ");
}
}

public static void main(String[] args) {

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

/** Printing the list **/

System.out.print("List is : ");
list.printList();
System.out.println();

/** Testing merged() method **/

LinkedList b = new LinkedList();


b.insert(333); b.insert(200); b.insert(450); b.insert(600);
LinkedList mergedList = list.merged(b);
System.out.print("Merged List : ");
mergedList.printList();
System.out.println();

/** Test append() method **/

LinkedList a = new LinkedList();


a.insert(10); a.insert(55); a.insert(30);
list.append(a);
System.out.print("Appended List : ");
list.printList();
System.out.println();

/** Testing search() method to find the index of an specified element */


System.out.println("Index of 32 in the list is : " + list.search(32));

/** 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());

/** Testing subList() method on list */


LinkedList subList = list.subList(1, 7);
System.out.print("Sub list : ");
subList.printList();
System.out.println();

/** deleteLast() method on list */


list.deleteLast();
System.out.print("After list.deleteLast(), list.printList() = ");
list.printList();
System.out.println();

/** 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();

/** Testing copy() method */


LinkedList copiedList = list.copy();
System.out.print("Copied List : ");
copiedList.printList();
System.out.println();
}
}
OUTPUT:

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 {

String name, roll_num;


public static void main(String args[]){
List list = new List();

Student s1 = new Student();


Student s2 = new Student();
Student s3 = new Student();
Student s4 = new Student();
Student s5 = new Student();
Student s6 = new Student();
s1.name="Amber"; s1.roll_num="19SW33";
s2.name="Malika"; s2.roll_num="19SW34";
s3.name="Suman"; s3.roll_num="19SW35";
s4.name="Rumaisa"; s4.roll_num="19SW38";
s5.name="Gulwish"; s5.roll_num="19SW39";
s6.name="Fiza"; s6.roll_num="19SW348";

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

LinkedList<String> list = new LinkedList<String>();

/** Method 1: add() **/

list.add("Chocolate");
list.add("Biscuits");
list.add("Lays");
list.add("Coca Cola");
list.add("Peanuts");
System.out.println("0. List : " + list);

/** Method 2: getFirst() **/

System.out.println("1. First element of the list : " + list.getFirst());

/** Method 3: getLast() */

System.out.println("2. Last element of the list : " + list.getLast());

/** Method 4: remove() **/

System.out.println("3. Remove first element of list: " + list.remove());

System.out.println("4. List after list.remove() is : " + list);

/** Method 5: removeLast() **/

System.out.println("5. Remove last element of list: " + list.removeLast());

System.out.println("6. List after list.removeLast() is : " + list);

/** SIZE METHOD **/


System.out.println("7. Size of the list is: " +list.size());

}
}

OUTPUT:

You might also like