0% found this document useful (0 votes)
25 views22 pages

Daa 1.4

Uploaded by

Nishtha Sharma
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)
25 views22 pages

Daa 1.4

Uploaded by

Nishtha Sharma
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/ 22

DEPARTMENT OF

COMPUTER SCIENCE & ENGINEERING

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.

2. Task to be done/ Which logistics used: Using various methods,


implementation of doubly linked list.

3. Algorithm/Flowchart (For programming based labs):


 Start.
 For insertion in the end if the list is empty start pointer points to
the first node the list.If thelist is non empty previous pointer of M
points to last node, next pointer of M points to firstnode and last
node’s next pointer points to this M node and first node’s previous
pointer points to this M node
 For Insertion at the beginning if the list is empty T next pointer
points to first node of the list, T previous pointer points to last
node the list, last node’s next pointer points to this T node, first
node’s previous pointer also points this T node and shift ‘Start’
pointer to this T node.
 For deletion at the end.
 If the list is empty, simply return it.
 If the list is not empty, then we define two pointers curr and
prev_1 and initialize the pointer curr points to the first node of the
list, and prev_1 = NULL.
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

 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

4. Steps for experiment/practical/Code:

//DOUBLY
package linked.list;

public class DLL {


DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

Node head;
Node tail;
private int size;
public DLL() {
this.size = 0;
}

public void insertFirst(int val) {


Node node = new Node(val);
node.next = head;
node.prev = null;
if(head!= null) {
head.prev = node;
}
head = node;
}

public Node find(int value) {


Node node = head;
while(node!=null) {
if(node.val == value) {
return node;
}
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

node= node.next;
}return null;
}

public void insertLast(int val) {


Node node = new Node(val);
Node last = head;

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

public void insert(int after,int val) {


Node p = find(after);
if(p==null) {
System.out.println("dDoes not exist");
return;
}
Node node = new Node(val);
node.next = p.next;
p.next = node;
node.prev =p;
if(node.next.prev != null) {
node.next.prev = node;
}
}
public int deleteFirst() {
int val = head.value;
head = head.next;
if(head == null) {
tail = null;
}
size--;
return val;
}
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

public int deleteLast() {


if(size<=1) {
return deleteFirst();
}
Node secondLast = get(size-1);
int val = tail.value;
tail = secondLast;
tail.next = null;

return val;
}

public Node get(int index) {


Node node = head;
for(int i =0; i<index; i++) {
node = node.next;
}
return node;
}
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

public void display() {


Node node = head;
Node last = null;
while(node!=null) {
System.out.print(node.val+" -> ");
last = node;
node = node.next;
}
System.out.println("END");

// System.out.println("Print in reverse");
// while(last != null) {
// System.out.print(last.val+" -> ");
// last = last.prev;
// }
//
// System.out.println("Start");
//

private class Node{


DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

public int value;


int val;
Node next;
Node prev;

public Node(int val) {


this.val = val;
}

public Node(int val, Node next, Node prev) {


this.val = val;
this.next = next;
this.prev = prev;
}

}
public class MainDLL {

public static void main(String[] args) {

DLL list = new DLL();


list.insertFirst(6);
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

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 class CLL {

private Node head;


private Node tail;

public CLL() {
this.head = null;
this.tail = null;
}

public void insert(int val) {


Node node = new Node(val);
if(head == null) {
head = node;
tail = node;
return;
}
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

tail.next = node;
node.next = head;
tail = node;
}

public void display() {


Node node = head;
if(head!=null) {
do {
System.out.print(node.val+" -> ");
if(node.next != null) {
node = node.next;
}
}while(node != head);
}System.out.println("Head");
}

public void delete(int val) {


Node node = head;
if(node == null) {
return;
}
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

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

public Node(int val) {


this.val = val;
}
}
public class MainCLL {

public static void main(String[] args) {

CLL list = new CLL();


list.insert(1);
list.insert(4);
list.insert(7);
list.insert(2);
list.insert(9);
list.insert(5);

list.display();
list.delete(5);

list.display();
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

5. Observations/Discussions/ Complexity Analysis:


Time Complexity = O(n).

6. Result/Output/Writing Summary:
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

DOUBLY

CIRCULAR
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

4.2

1. Aim/Overview of the practical:


Code to push & pop and check Isempty, Isfull and Return top element
in stacks using templates.

2. Task to be done/ Which logistics used: Using various methods,


implementation of stack.

3. Algorithm/Flowchart (For programming based labs):


 Start.
 First we will define the size.
 Then we will create a class template called Stack.
 Then we will check the top of stack using - template <class T>
Stack<T>::Stack() { top
= - 1.
 Then we will push elements into the stack using templates.
 Using template we will check whether the stack is empty or is full.
 The we will pop an element of stack using templates.
 We will check the top element using template <class T> T
Stack<T>::topElement()  print the result
 Stop

4. Steps for experiment/practical/Code:

package stack.kunal;

public class CustomStack {

protected int[] data;


DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

private static final int DEFAULT_SIZE =10;

int ptr =-1;

public CustomStack() {
this(DEFAULT_SIZE);
}

public CustomStack(int size) {


this.data = new int[size];
}

public boolean push(int item) {


if(isFull()) {
System.out.println("Stack is full!");
return false;
}
ptr++;
data[ptr] = item;
return true;
}
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

public int pop() throws Exception {


if(isEmpty()) {
throw new Exception("Cannot pop from empty stack");
}
// int removed = data[ptr];
// ptr--;
// return removed;

return data[ptr--];
}

public int peek() throws Exception {


if(isEmpty()) {
throw new Exception("Cannot peek from empty stack");
}
return data[ptr];
}

public boolean isFull() {

return ptr == data.length-1;//data is at last index


DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

private boolean isEmpty() {


return ptr == -1;
}

public class Main {

public static void main(String[] args)throws Exception {

CustomStack stack = new CustomStack(5);

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

5. Observations/Discussions/ Complexity Analysis:


Time Complexity = O(1).

6. Result/Output/Writing Summary:
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

Learning outcomes (What I have learnt):


1. Learn Implementation of linked list and stack.
2. Learnt about optimization of the problem.
3. Learnt about time complexities and space complexities.

Evaluation Grid (To be created as per the SOP and Assessment guidelines by the faculty):
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

Sr. No. Parameters Marks Obtained Maximum Marks


1.
2.
3.

You might also like