DSA lab 04
DSA lab 04
Student Details
Name ID
Tayebur Rahman 242034017
Lab Date :
Submission Date : 02/03/2025
Course Teacher Name : ABRAR HASAN
Objectives/Aim
Procedure / Design
Singly Linked List Flowchart:
1. Start
2. Create a node with data and a pointer to the next node.
3. Add, remove, and display elements step by step.
4. End
Implementation
Singly Linked List:
#include <iostream>
using namespace std;
struct Node
{
int data;
Node* next;
};
int main()
{
Node* head = nullptr;
Node* first = new Node();
Node* second = new Node();
Node* third = new Node();
first->data = 1;
first->next = second;
second->data = 2;
second->next = third;
third->data = 3;
third->next = nullptr;
return 0;
}
Doubly Linked List:
#include <iostream>
using namespace std;
struct Node {
int data;
Node* next;
Node* prev;
};
int main() {
Node* head = nullptr;
Node* first = new Node();
Node* second = new Node();
Node* third = new Node();
first->data = 1;
first->next = second;
first->prev = nullptr;
second->data = 2;
second->next = third;
second->prev = first;
third->data = 3;
third->next = nullptr;
third->prev = second;
temp = third;
cout << "\nBackward Traversal: ";
while (temp != nullptr) {
cout << temp->data << " ";
temp = temp->prev;
}
return 0;
}
Output
Output(Singly):
Output(Doubly):
Discussion
1. Dynamic Memory Management:
o Linked lists grow and shrink as needed, unlike arrays.
2. Performance:
o Fast insertions and deletions, but slower access compared to arrays.
3. Key Learnings:
o How pointers work and the flexibility of linked list
Practical Applications:
Mapping of Objectives:
The objectives were achieved by successfully implementing and testing the
linked list operations, analyzing their behaviors, and understanding their
practical applications.