Dsa Bim Practical File
Dsa Bim Practical File
SIMALCHAUR,POKHARA
Submitted by Submitted To
Bipin Timilsina Prithivi
Symbol no: something guy
SN TITLE DATE OF
SUBMISSION
SIGNATURE
1.
2.
3.
4.
5.
EXPERIMENT-1
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.
,
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.
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.
We are given the reference to a node, and the new node is inserted after the given
node.
2.4 Traverse
Output :
//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();
} else
System.out.println("invalid");
}
}
OUTPUT:
}
}
} 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{ }