0% found this document useful (0 votes)
64 views10 pages

Assignment # 2 DSA Lab

Uploaded by

fairykomal993
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
64 views10 pages

Assignment # 2 DSA Lab

Uploaded by

fairykomal993
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

COMSATS UNIVERSITY ISLAMABAD, SAHIWAL CAMPUS

Assignment # 2
Submitted to Mr. Bilal Shabbir Qaisar
Submitted by Amina Arshad
Registration no SP23-BCS-190
Subject Data Structures and Algorithms
Course code CSC-211
Question No. 01: Write the computer program to apply the concepts of
circular linked lists. The list will include the following functions:

1) Insert the value at the end of the list


2) Insert the value at the start of the list
3) Insert the value after the specific value
4) Delete the value from the end of the list
5) Delete the value from the start of the list
6) Delete specific values
7) Display the list

#include <iostream>
using namespace std;
struct Node {
int data;
Node* next;
};
void insertAtEnd(Node*& head, int value) {
Node* newNode = new Node();
newNode->data = value;
if (head == NULL) {
head = newNode;
newNode->next = head;
} else {
Node* temp = head;
while (temp->next != head) {
temp = temp->next;
}
temp->next = newNode;
newNode->next = head;
}
}
void insertAtStart(Node*& head, int value) {
Node* newNode = new Node();
newNode->data = value;

if (head == NULL) {
head = newNode;
newNode->next = head;
} else {
Node* temp = head;
while (temp->next != head) {
temp = temp->next;
}
temp->next = newNode;
newNode->next = head;
head = newNode;
}
}
void insertAfterValue(Node*& head, int target, int value) {
if (head == NULL)
{
return;
}
Node* temp = head;
do {
if (temp->data == target) {
Node* newNode = new Node();
newNode->data = value;
newNode->next = temp->next;
temp->next = newNode;
return;
}
temp = temp->next;
} while (temp != head);

cout << "Value " << target << " not found in the list." << endl;
}
void deleteAtEnd(Node*& head) {
if (head == NULL) return;

if (head->next == head) {
delete head;
head = NULL;
} else {
Node* temp = head;
Node* prev = NULL;

while (temp->next != head) {


prev = temp;
temp = temp->next;
}
prev->next = head;
delete temp;
}
}
void deleteAtStart(Node*& head) {
if (head == NULL)
{
return;
}
if (head->next == head) {
delete head;
head = NULL;
} else {
Node* temp = head;
while (temp->next != head) {
temp = temp->next;
}
Node* toDelete = head;
head = head->next;
temp->next = head;
delete toDelete;
}
}
void deleteSpecificValue(Node*& head, int value) {
if (head == NULL)
{
return;
}
Node* temp = head;
Node* prev = NULL;
if (head->data == value) {
deleteAtStart(head);
return;
}

do {
prev = temp;
temp = temp->next;
if (temp->data == value) {
prev->next = temp->next;
delete temp;
return;
}
} while (temp != head);

cout << "Value " << value << " not found in the list." << endl;
}
void displayList(Node* head) {
if (head == NULL)
{
return;
}
Node* temp = head;
do {
cout << temp->data << " ";
temp = temp->next;
} while (temp != head);
cout << endl;
}
int main() {
Node* head = NULL;
insertAtEnd(head, 10);
insertAtEnd(head, 20);
insertAtEnd(head, 30);
cout << "List after inserting values at the end: ";
displayList(head);

insertAtStart(head, 5);
cout << "List after inserting value at the start: ";
displayList(head);

insertAfterValue(head, 10, 15);


cout << "List after inserting value 15 after 10: ";
displayList(head);

deleteAtEnd(head);
cout << "List after deleting the value from the end: ";
displayList(head);

deleteAtStart(head);
cout << "List after deleting the value from the start: ";
displayList(head);

deleteSpecificValue(head, 15);
cout << "List after deleting the value 15: ";
displayList(head);

return 0;
}

Output:

Question No. 02: Implement the Deque in linked list.

#include <iostream>
using namespace std;
struct Node {
int data;
Node* next;
Node* prev;
};

// Deque class with doubly linked list


class Deque {
private:
Node* front;
Node* rear;
public:
// Constructor
Deque() {
front = NULL;
rear = NULL;
}

// Check if the deque is empty


bool isEmpty() {
return (front == NULL);
}

// Insert at the front of the deque


void insertFront(int value) {
Node* newNode = new Node();
newNode->data = value;
newNode->next = front;
newNode->prev = NULL;

if (isEmpty()) {
rear = newNode;
} else {
front->prev = newNode;
}
front = newNode;
}

// Insert at the rear of the deque


void insertRear(int value) {
Node* newNode = new Node();
newNode->data = value;
newNode->next = NULL;
newNode->prev = rear;

if (isEmpty()) {
front = newNode;
} else {
rear->next = newNode;
}
rear = newNode;
}

// Delete from the front of the deque


void deleteFront() {
if (isEmpty()) {
cout << "Deque is empty, cannot delete from front." << endl;
return;
}
Node* temp = front;
front = front->next;

if (front != NULL) {
front->prev = NULL;
} else {
rear = NULL; // If the deque becomes empty
}
delete temp;
}

// Delete from the rear of the deque


void deleteRear() {
if (isEmpty()) {
cout << "Deque is empty, cannot delete from rear." << endl;
return;
}
Node* temp = rear;
rear = rear->prev;

if (rear != NULL) {
rear->next = NULL;
} else {
front = NULL; // If the deque becomes empty
}
delete temp;
}
int getFront() {
if (isEmpty()) {
cout << "Deque is empty." << endl;
return -1;
}
return front->data;
}

// Get the rear value


int getRear() {
if (isEmpty()) {
cout << "Deque is empty." << endl;
return -1;
}
return rear->data;
}

// Display the deque from front to rear


void display() {
if (isEmpty()) {
cout << "Deque is empty." << endl;
return;
}
Node* temp = front;
while (temp != NULL) {
cout << temp->data << " ";
temp = temp->next;
}
cout << endl;
}
};

int main() {
Deque deque;

// Inserting elements at the front and rear


deque.insertFront(10);
deque.insertFront(20);
deque.insertRear(30);
deque.insertRear(40);

// Displaying the deque


cout << "Deque: ";
deque.display();
// Getting front and rear values
cout << "Front element: " << deque.getFront() << endl;
cout << "Rear element: " << deque.getRear() << endl;

// Deleting elements from the front and rear


deque.deleteFront();
cout << "Deque after deleting from front: ";
deque.display();

deque.deleteRear();
cout << "Deque after deleting from rear: ";
deque.display();

return 0;
}

Output:

You might also like