Daa 1.4
Daa 1.4
Experiment 4
Student Name: NISHTHA SHARMA UID: 20BCS5674
Branch: CSE Section/Group: 609 B
Semester: 5th Date: 16/09/2022
Subject Name: DAA Lab Subject Code: 20CSP-312
4.1
1. Aim/Overview of the practical:
Code to Insert and Delete an element at the beginning and at end in
Doubly and Circular Linked List.
Traverse the list using the curr pointer to find the node to be
deleted and before moving from curr to the next node, every time
set prev_1 = curr.
If the node is found, check if it is the only node in the list. If yes,
set start = NULL and free the node pointing by curr.
If the list has more than one node, check if it is the first node of
the list. The condition to check this is (curr == start). If yes, then
move prev_1 to the last node(prev_1 = start -> prev).
After prev_1 reaches the last node, set start = start -> next and
prev_1 -> next = start and start ->prev = prev_1. Free the node
pointing by curr.
If curr is not the first node, we check if it is the last node in the
list. The condition to check this is (curr -> next == start). If yes,
set prev_1 -> next = start and start -> prev = prev_1. Free the node
pointing by curr.
If the node to be deleted is neither the first node nor the last node,
declare one more pointer temp and initialize the pointer temp
points to the next of curr pointer (temp = curr>next). Now set,
prev_1 -> next = temp and temp ->prev = prev_1. Free the node
pointing by curr. 8. print the result
Stop
//DOUBLY
package linked.list;
Node head;
Node tail;
private int size;
public DLL() {
this.size = 0;
}
node= node.next;
}return null;
}
node.next = null;
if(head == null) {
node.prev =null;
head = node;
return;
}
while(last.next!=null) {
last = last.next;
}
last.next = node;
node.prev = last;
}
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
return val;
}
// System.out.println("Print in reverse");
// while(last != null) {
// System.out.print(last.val+" -> ");
// last = last.prev;
// }
//
// System.out.println("Start");
//
}
public class MainDLL {
list.insertFirst(5);
list.insertFirst(4);
list.insertFirst(3);
list.insertFirst(2);
list.insertFirst(1);
list.insertLast(7);
list.display();
list.deleteFirst();
list.display();
}
}
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
//CIRCULAR
package linked.list;
import org.w3c.dom.Node;
public CLL() {
this.head = null;
this.tail = null;
}
tail.next = node;
node.next = head;
tail = node;
}
if(head==tail) {
head = null;
tail = null;
return;
}
if(node.val==val) {
head = head.next;
tail.next = head;
return;
}
do {
Node n = node.next;
if(n.val == val) {
node.next = n.next;
break;
}
node = node.next;
}while(node!=head);
}
private class Node{
int val;
Node next;
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
list.display();
list.delete(5);
list.display();
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
6. Result/Output/Writing Summary:
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
DOUBLY
CIRCULAR
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
4.2
package stack.kunal;
public CustomStack() {
this(DEFAULT_SIZE);
}
return data[ptr--];
}
stack.push(5);
stack.push(18);
stack.push(45);
stack.push(2);
stack.push(34);
stack.push(8);
System.out.println(stack.pop());
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
6. Result/Output/Writing Summary:
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
Evaluation Grid (To be created as per the SOP and Assessment guidelines by the faculty):
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING