0% found this document useful (0 votes)
34 views12 pages

Dsa Bim Practical File

The document outlines a Java program for operations on a singly linked list, including insertion, deletion, searching, and traversal. It provides detailed objectives and theoretical explanations of linked list operations, along with code implementations for various scenarios. The document serves as a practical guide for understanding and manipulating singly linked lists in Java.

Uploaded by

Ranga Timilsina
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views12 pages

Dsa Bim Practical File

The document outlines a Java program for operations on a singly linked list, including insertion, deletion, searching, and traversal. It provides detailed objectives and theoretical explanations of linked list operations, along with code implementations for various scenarios. The document serves as a practical guide for understanding and manipulating singly linked lists in Java.

Uploaded by

Ranga Timilsina
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

JANAPRIYA MULTIPLE CAMPUS

SIMALCHAUR,POKHARA

BACHELOR IN INFORMATION MANAGEMENT

Submitted by Submitted To
Bipin Timilsina Prithivi
Symbol no: something guy
SN TITLE DATE OF
SUBMISSION
SIGNATURE

1.

2.

3.

4.

5.
EXPERIMENT-1

TITLE-A JAVA PROGRAM TO RUN OPERATIONS OF SINGLY LINKED


LIST.

1.OBJECTIVE-
 To insert a node at beginning, at last and at certain position
and display it .
 To delete a node from the beginning from the last and from
certain position.
 To search a node from singly linked list.

2.THEORY :
A singly linked list is is a type of list that is unidirectional, that is, it
can be traversed in only one direction from head to the last node
(tail). Each element in a linked list is called a node. A single node
contains data and a pointer to the next node which helps in
maintaining the structure of the list.
,

The various operations of singly linked list are described below:

2.1. Linked List node Insertion


There can be three cases that will occur when we are inserting a node in a
linked list.

 Insertion at the beginning


 Insertion at the end. (Append)
 Insertion after a given node
2.1.1 Insertion at the beginning

Since there is no need to find the end of the list. If the list is empty, we make the
new node as the head of the list. Otherwise, we we have to connect the new node
to the current head of the list and make the new node, the head of the list.

2.1.2 Insertion at end

We will traverse the list until we find the last node. Then we insert the new node to
the end of the list. Note that we have to consider special cases such as list being
empty.

In case of a list being empty, we will return the updated head of the linked list
because in this case, the inserted node is the first as well as the last node of the
linked list.

2.1.3 Insertion after a given node

We are given the reference to a node, and the new node is inserted after the given
node.

2.2 Linked List node Deletion


To delete a node from a linked list, we need to do these steps

 Find the previous node of the node to be deleted.


 Change the next pointer of the previous node
 Free the memory of the deleted node.
In the deletion, there is a special case in which the first node is deleted. In this, we
need to update the head of the linked list.

2.3 Linked List node Searching


To search any value in the linked list, we can traverse the linked list and compares
the value present in the node.

2.4 Traverse

The traverse operation helps to display the content of a singly linked


list. Here, to run this operation ,the temp node is kept moving to the
next one and the contents of the node is displayed.

SINGLY LINKED LIST IMPLEMENTATION IN JAVA

PROGRAM 1: INSERT A NODE AT THE BEGINNING OF THE


LINKED LIST:
public class SllInsertAtBeg {
class Node { }
int data; System.out.print(temp);
Node next; }

Node(int data) { public static void main(String[] args) {


this.data = data; SllInsertAtBeg s = new SllInsertAtBeg();
this.next = null; System.out.println("linked list when there
} are no values:");
} s.display();
System.out.println("\n\nNow inserting '29'
public Node head = null; at beginning");
s.insertatbeg(29);
public void insertatbeg(int item) { s.display();
Node newnode = new Node(item); System.out.println("\n\nNow inserting 30
newnode.next = head; at the beginning of the linked list");
head = newnode; s.insertatbeg(30);
} s.display();
}
public void display() { }
Node temp = head;
while (temp != null) {
System.out.print(temp.data + "->");
temp = temp.next;

Output :

PROGRAM 2: INSERTING AN NODE AT SPECIFIC POSITION

//insert at pos
public class SllInsertAtPos{
class Node { public void display() {
int data; Node temp = head;
Node next; while (temp != null) {
System.out.print(temp.data + "->");
Node(int data) { temp = temp.next;
this.data = data; }
this.next = null; System.out.print(temp);
} }
}
public static void main(String[] args) {
public Node head = null; SllInsertAtPos s = new SllInsertAtPos();

public void insertAtPos(int x, int pos) { System.out.println("Inserting 1 at first


Node newnode = new Node(x); position");
if (pos < 1) { s.insertAtPos(1,1);
System.out.println("invalid insertion"); s.display();
} else if (pos == 1) { System.out.println("\n\nInserting 2 at
newnode.next = head; position second");
head = newnode; s.insertAtPos(2,2 );
s.display();
} else { System.out.println("\n\nInserting 3 at
Node temp = head; position third");
for (int i = 1; i < pos - 1; i++) { s.insertAtPos(3, 3);
temp = temp.next; s.display();
}
} }
if (temp != null) {
newnode.next = temp.next;
temp.next = newnode;

} else
System.out.println("invalid");
}
}

PROGRAM 3: INSERTING AT END

public class SllInsertAtEnd { public void display() {


class Node { Node temp = head;
int data; while (temp != null) {
Node next; System.out.print(temp.data + "->");
temp = temp.next;
Node(int data) { }
this.data = data; System.out.print(temp);
this.next = null; }
}
} public static void main(String[] args) {
SllInsertAtEnd s = new SllInsertAtEnd();
public Node head = null;
s.insertatEnd(49);
public void insertatEnd(int item) { System.out.println("Inserting 49:");
Node newnode=new Node(item); s.display();
Node temp=head; s.insertatEnd(33);
if(head==null){ System.out.println("\nInserting 33 at the end:");
head=newnode; s.display();
} }
else{ }
while(temp.next!=null){
temp=temp.next;
}
temp.next=newnode;
}}

OUTPUT:

PROGRAM 4:DELETING A ELEMENT AT END AND BEGINNING


OPERATION

//delete at end and Begining public void deleteatend() {


if (head == null) {
public class SllDelAtEndBeg { System.out.println("list is empty");
class Node { } else if (head.next == null) {
int data; head = null;
Node next;
} else {
Node(int data) { Node temp = head;
this.data = data; while (temp.next.next != null) {
this.next = null; temp = temp.next;
}
} }
temp.next = null;
public Node head = null; }
}
public void insertatbeg(int item) {
Node newnode = new Node(item); public void display() {
newnode.next = head; Node temp = head;
head = newnode; while (temp != null) {
System.out.print(temp.data + "->");
} temp = temp.next;
public void deleteAtBeg() { }
if(head==null){ System.out.print(temp);
System.out.println("The list is empty"); }
}
else public static void main(String[] args) {
{ SllDelAtEndBeg s = new SllDelAtEndBeg();
head=head.next; s.insertatbeg(29);
} s.insertatbeg(30);
s.insertatbeg(55);
} System.out.println("The linked list before
deleteAtEnd operation is performed ");
s.display();
System.out.println("\n");
s.deleteatend();
System.out.println("The linked list after the
deleteAtEnd operation is performed");
s.display();
System.out.println("\n");
s.deleteAtBeg();
System.out.println("The linked list after the
deleteAtBeg operation is performed");
s.display();

}
}

PROGRAM 5: DELETING A ELEMENT AT SPECIFIC


POSITION
public class SllDelAtPos{ System.out.println("invalid");
class Node {
int data; }}}
Node next;
public void display() {
Node(int data) { Node temp = head;
this.data = data;
this.next = null; while (temp != null) {
} System.out.print(temp.data + "->");
} temp = temp.next;
}
public Node head = null; System.out.print(temp);
}
public void insertatbeg(int item) {
Node newnode = new Node(item); public static void main(String[] args) {
newnode.next = head; SllDelAtPos s = new SllDelAtPos();
head = newnode; s.insertatbeg(29);
s.insertatbeg(30);
} System.out.println("Before deletion: ");
s.display();
public void deleteAtPos(int pos) { System.out.println("\n\nAfter Deletion of node at
if (pos < 1) specific position or second position: ");
{ System.out.println("invalid"); s.deleteAtPos(2);
}else if(pos==1)
s.display();
{ }
if (head == null) { }
System.out.println("empty list");

} else {
head = head.next;
}
}else
{
Node temp = head;
for (int i = 1; i < pos - 1 && temp != null;
i++) {
temp = temp.next;
}
if (temp != null) {
temp.next = temp.next.next;
} else {

OUTPUT:
PROGRAM 6: SEARCHING AN ELEMENT IN THE
LINKED LIST
public class SllSearching1 { if(head == null) {
class Node { System.out.println("List is empty");
int data; }
Node next; else {
while(temp != null) {
Node(int data) {
this.data = data; if(temp.data == item) {
this.next = null; flag = true;
} break;
} }
i++;
public Node head = null; temp = temp.next;
}
}
public void insertatEnd(int item){ if(flag)
Node temp=head; System.out.println("!!FOUND!! Element is
Node newnode=new Node(item); present in the list at the position : " + i);
if (head==null){ else
head=newnode; System.out.println("Element is not present in
} the list");
else{ }

while(temp.next!=null) public static void main(String[] args) {


{ SllSearching1 s = new SllSearching1();
temp=temp.next; s.insertatEnd(5);
} s.insertatEnd(33);
temp.next=newnode; s.insertatEnd(2);
} System.out.println("The linked list is as follows:");
} s.display();
System.out.println("\n");
public void display() { System.out.println("Searching for '2' in the linked
Node temp = head; list");
while (temp != null) { s.searchNode(2);
System.out.print(temp.data + "->"); System.out.println("\nSearching for '7' in the
temp = temp.next; linked list");
} s.searchNode(7);
System.out.print(temp); }}
}

public void searchNode(int item) {


Node temp = head;
int i = 1;
boolean flag = false;
OUTPUT:

You might also like