You are on page 1of 5

Experiment Title: 4.

Student Name: Ayush Tiwari UID: 20BCS7611


Branch: BE-CSE Section/Group: 20BCS_WM_905-B
Semester: 5th Date of Performance:
Subject Name: DAA Lab Subject Code: 20CSP-312

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: The task is to create a function which will insert and
delete elements at the beginning and end in doubly and circular linked list and return head of
that linked list.

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


• Insertion at beginning.
o Create a new node.
o If the list is empty, then insert the node and set next and previous pointer as NULL.
o If the list is not empty, insert the data and set next and previous pointer and update
them.
• Insertion at end.
o If the list is empty create a node as circular doubly list.
o Find last node.
o Create node dynamically.
o Start going to be the next of new node.
o Make new node as previous node.
o Make last previous of new node.
o Make new node next of old last.
4. Steps for experiment/practical/Code:

#include<bits/stdc++.h>
using namespace std;

class Node{
public:
int data;
Node *next;
Node *prev;
Node(int data){
this->data = data;
this->next = NULL;
this->prev = NULL;
}
};

void insertAtBeg(Node *&head, int data){


Node *newNode = new Node(data);
if(head == NULL){
head = newNode;
head->next = head;
head->prev = head;
}
else{
newNode->next = head;
newNode->prev = head->prev;
head->prev->next = newNode;
head->prev = newNode;
head = newNode;
}
}

void insertAtEnd(Node *&head, int data){


Node *newNode = new Node(data);
if(head == NULL){
head = newNode;
head->next = head;
head->prev = head;
}
else{
newNode->next = head;
newNode->prev = head->prev;
head->prev->next = newNode;
head->prev = newNode;
}
}
void deleteAtBeg(Node *&head){
if(head == NULL){
cout<<"List is empty"<<endl;
return;
}
else if(head->next == head){
delete head;
head = NULL;
}
else{
Node *temp = head;
head = head->next;
head->prev = temp->prev;
temp->prev->next = head;
delete temp;
}
}

void deleteAtEnd(Node *&head){


if(head == NULL){
cout<<"List is empty"<<endl;
return;
}
else if(head->next == head){
delete head;
head = NULL;
}
else{
Node *temp = head->prev;
head->prev=temp->prev;
temp->prev->next=head;
delete temp;
}
}

void display(Node *head){


Node *temp = head;
if(head == NULL){
cout<<"List is empty"<<endl;
return;
}
while(temp->next != head){
cout<<temp->data<<" ";
temp = temp->next;
}
cout<<temp->data<<endl;
}

int main(){
Node *head = NULL;
insertAtBeg(head, 10);
insertAtBeg(head, 20);
insertAtBeg(head, 30);
insertAtBeg(head, 40);
insertAtBeg(head, 50);
insertAtEnd(head, 60);
insertAtEnd(head, 70);
insertAtEnd(head, 80);
insertAtEnd(head, 90);
insertAtEnd(head, 100);
display(head);
deleteAtEnd(head);
display(head);
deleteAtBeg(head);
display(head);
return 0;
}
5. Observations/Discussions/ Complexity Analysis:

• Time Complexity
o Insertion at beginning: O(1)
o Insertion at end: O(n)
o Deletion at beginning: O(1)
o Deletion at end: O(n)
• Space Complexity: O(N)

6. Result/Output/Writing Summary:

Output

Learning outcomes (What I have learnt):

1. Learnt about doubly and circular linked list.

2. Learnt how to insert element at beginning and at end position.

3. Learnt how to delete element at beginning and at end position.

4. Learnt how to analyze time and space complexity.

You might also like