Data Structure2
Data Structure2
(Vehari campus)
A
Assignment no:02
Submitted to:
Sir .Najeeb Ullah
Submitted by:
Iqra Fatima(FA22-BCS-155)
Subject:
Data Structures
Department:
BS-Computer Science
Question no:01
Write a code of doubly link list?
#include <iostream>
using namespace std;
// Define Node class
class Node {
public:
int data;
Node* prev;
Node* next;
};
/Insert at start()
int main()
{
// insert node at the front
void insertFront(struct Node* head, int data){
// allocate memory for newNode
struct Node* newNode = new Node;
newNode->data = data;
newNode->next = (*head);
//insert at end()
current->next = newNode;
}
// Function to print the elements of the linked list
void printList(Node* head) {
// Traverse the list starting from the head
Node* current = head with the head pointer
while (current != NULL) {
cout << current->data << " ";
current = current->next;
}
cout << endl;
}
int main() {
// Initialize an empty linked list (head points to NULL)
Node* head = NULL;
insertAtEnd(head, 5);
insertAtEnd(head, 10);
insertAtEnd(head, 15);
insertAtEnd(head, 20);
//deletion at beginning ()
int main() {
// Example usage
Node* head = new Node(1);
head->next = new Node(2);
head->next->prev = head;
head->next->next = new Node(3);
head->next->next->prev = head->next;
//deletion at end
delete temp->next;
temp->next = nullptr;
}
int main() {
// Example usage
Node* head = new Node(1);
head->next = new Node(2);
head->next->prev = head;
head->next->next = new Node(3);
head->next->next->prev = head->next;
deleteAtEnd(head);
cout << "List after deletion at end: ";
printList(head);
return 0;
}
//searching ()
struct Node {
int data;
Node* prev;
Node* next;
Node(int val) : data(val), prev(nullptr), next(nullptr) {}
};
int main() {
// Example usage
Node* head = new Node(1);
head->next = new Node(2);
head->next->prev = head;
head->next->next = new Node(3);
head->next->next->prev = head->next;
int key = 2;
Node* result = search(head, key);
if (result != nullptr) {
cout << key << " found in the list." << endl;
} else {
cout << key << " not found in the list." << endl;
}
return 0;
}
//sorting ()
struct Node {
int data;
Node* prev;
Node* next;
Node(int val) : data(val), prev(nullptr), next(nullptr) {}
};
void swapData(Node* a, Node* b) {
int temp = a->data;
a->data = b->data;
b->data = temp;
}
void bubbleSort(Node* head) {
if (head == nullptr || head->next == nullptr)
return;
Node* end = nullptr;
bool swapped;
do {
swapped = false;
Node* current = head;
while (current->next != end) {
if (current->data > current->next->data) {
swapData(current, current->next);
swapped = true;
}
current = current->next;
}
end = current;
} while (swapped);
}
void traverse(Node* head) {
cout << "Doubly linked list: ";
while (head != nullptr) {
cout << head->data << " ";
head = head->next;
}
cout << endl;
}
int main() {
// Example usage
Node* head = new Node(3);
head->next = new Node(2);
head->next->prev = head;
head->next->next = new Node(1);
head->next->next->prev = head->next;
// Forward traversal
void forwardTraversal() {
Node* temp = head;
while (temp != nullptr) {
cout << temp->data << " ";
temp = temp->next;
}
cout << endl;
}
// Reverse traversal
void reverseTraversal() {
Node* temp = tail;
while (temp != nullptr)
{
cout << temp->data << " ";
temp = temp->prev;
}
cout << endl;
}
};
int main() {
DoublyLinkedList dll;
dll.insertAtEnd(10);
dll.insertAtEnd(20);
dll.insertAtEnd(30);
return 0;
}