Daa 4
Daa 4
Experiment 4
Student Name: Sumit Kumar UID: 22BCS16614
Branch: BE-CSE Section/Group: 902 -A
Semester: 5th Date of Performance: 20/08/24
Subject Name: DAA Subject Code: 22CSH311
1. Aim: Apply the concept of Linked list and write code to Insert and Delete an
element at the beginning and attend in Doubly and Circular Linked List.
4. Code:
#include <iostream>
struct DoublyNode {
int data;
DoublyNode* prev;
DoublyNode* next;
};
struct CircularNode {
int data;
CircularNode* next;
};
head = newNode;
if (!head) {
head = newNode;
return;
temp->next = newNode;
newNode->prev = temp;
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
head = head->next;
delete temp;
temp->prev->next = nullptr;
delete temp;
if (!head) {
newNode->next = newNode;
head = newNode;
return;
temp->next = newNode;
head = newNode;
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
if (!head) {
newNode->next = newNode;
head = newNode;
return;
temp->next = newNode;
newNode->next = head;
if (head->next == head) {
delete head;
head = nullptr;
return;
head = head->next;
last->next = head;
delete temp;
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
prev = temp;
temp = temp->next;
prev->next = head;
delete temp;
if (!head) return;
do {
temp = temp->next;
}
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
int main() {
insertAtBeginningDoubly(doublyHead, 10);
insertAtEndDoubly(doublyHead, 20);
insertAtEndDoubly(doublyHead, 30);
printDoublyList(doublyHead);
deleteFromBeginningDoubly(doublyHead);
printDoublyList(doublyHead);
deleteFromEndDoubly(doublyHead);
printDoublyList(doublyHead);
insertAtBeginningCircular(circularHead, 1);
insertAtEndCircular(circularHead, 2);
insertAtEndCircular(circularHead, 3);
printCircularList(circularHead);
deleteFromBeginningCircular(circularHead);
printCircularList(circularHead);
deleteFromEndCircular(circularHead);
printCircularList(circularHead);
return 0;
5. Output:
7. Learning Outcome:
Understand the structure of a doubly linked list, including the use of prev
and next pointers. Learn how these pointers help in bidirectional traversal.
Learn how to insert nodes at the beginning of the list by adjusting the head
pointer and prev links. Practice inserting nodes at the end by traversing to
the last node.
Practice deleting nodes from the beginning by updating the head and prev
pointer. Understand how to delete the last node by traversing and unlinking
it.
Gain experience in traversing the list from the head to the last node. Learn
how to display the data stored in each node sequentially.
Analyze the time complexity of insertion, deletion, and traversal operations.
Recognize that operations at the list ends have different time complexities.