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

Assignment 1 DSA

The document presents an assignment by Muhammad Ismail on Data Structures and Algorithms, focusing on the implementation of singly and doubly linked lists in C++. It includes code for creating nodes, inserting, deleting, and displaying the lists, along with test cases demonstrating functionality. The assignment is submitted to Sir Dr Shahzad Akbar.

Uploaded by

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

Assignment 1 DSA

The document presents an assignment by Muhammad Ismail on Data Structures and Algorithms, focusing on the implementation of singly and doubly linked lists in C++. It includes code for creating nodes, inserting, deleting, and displaying the lists, along with test cases demonstrating functionality. The assignment is submitted to Sir Dr Shahzad Akbar.

Uploaded by

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

ASSIGNMENT #1

Name : Muhammad Ismail

Roll no : 30220

Dept. : BS-SE (3rd) A

Subject : Data Structure & Algorithm

Submitted to : Sir Dr Shahzad Akbar

.SINGLE WAY LINK LIST


#include <iostream>
using namespace std;

// Node structure for singly linked list


struct Node {
int data;
Node* next;
};

// Function to create a new node


Node* createNode(int data) {
Node* newNode = new Node();
newNode->data = data;
newNode->next = nullptr;
return newNode;
}

// Function to insert a node at the end of the list


void insertEnd(Node*& head, int data) {
Node* newNode = createNode(data);
if (head == nullptr) {
head = newNode;
} else {
Node* temp = head;
while (temp->next != nullptr) {
temp = temp->next;
}
temp->next = newNode;
}
}

// Function to delete a node by value


void deleteNode(Node*& head, int data) {
if (head == nullptr) {
cout << "List is empty.\n";
return;
}
if (head->data == data) {
Node* temp = head;
head = head->next;
delete temp;
return;
}

Node* temp = head;


while (temp->next != nullptr && temp->next->data != data) {
temp = temp->next;
}

if (temp->next == nullptr) {
cout << "Node with value " << data << " not found.\n";
} else {
Node* toDelete = temp->next;
temp->next = temp->next->next;
delete toDelete;
}
}

// Function to display the list


void displayList(Node* head) {
if (head == nullptr) {
cout << "List is empty.\n";
return;
}
Node* temp = head;
while (temp != nullptr) {
cout << temp->data << " ";
temp = temp->next;
}
cout << endl;
}

// Main function to test the singly linked list


int main() {
Node* head = nullptr;

// Inserting nodes into the list


insertEnd(head, 10);
insertEnd(head, 20);
insertEnd(head, 30);
insertEnd(head, 40);

// Display the list


cout << "Singly Linked List: ";
displayList(head);

// Deleting a node
deleteNode(head, 20);
cout << "After deleting 20: ";
displayList(head);

deleteNode(head, 50); // Node not found


cout << "After attempting to delete 50: ";
displayList(head);

return 0;
}
2. Doubly Linked List (Two Way Linked List)

cpp
Copy
#include <iostream>
using namespace std;

// Node structure for doubly linked list


struct Node {
int data;
Node* next;
Node* prev;
};

// Function to create a new node


Node* createNode(int data) {
Node* newNode = new Node();

1. Singly Linked List (One Way Linked List)

cpp
Copy
#include <iostream>
using namespace std;

// Node structure for singly linked list


struct Node {
int data;
Node* next;
};

// Function to create a new node


Node* createNode(int data) {
Node* newNode = new Node();
newNode->data = data;
newNode->next = nullptr;
return newNode;
}
// Function to insert a node at the end of the list
void insertEnd(Node*& head, int data) {
Node* newNode = createNode(data);
if (head == nullptr) {
head = newNode;
} else {
Node* temp = head;
while (temp->next != nullptr) {
temp = temp->next;
}
temp->next = newNode;
}
}

// Function to delete a node by value


void deleteNode(Node*& head, int data) {
if (head == nullptr) {
cout << "List is empty.\n";
return;
}
if (head->data == data) {
Node* temp = head;
head = head->next;
delete temp;
return;
}

Node* temp = head;


while (temp->next != nullptr && temp->next->data != data) {
temp = temp->next;
}

if (temp->next == nullptr) {
cout << "Node with value " << data << " not found.\n";
} else {
Node* toDelete = temp->next;
temp->next = temp->next->next;
delete toDelete;
}
}

// Function to display the list


void displayList(Node* head) {
if (head == nullptr) {
cout << "List is empty.\n";
return;
}
Node* temp = head;
while (temp != nullptr) {
cout << temp->data << " ";
temp = temp->next;
}
cout << endl;
}

// Main function to test the singly linked list


int main() {
Node* head = nullptr;

// Inserting nodes into the list


insertEnd(head, 10);
insertEnd(head, 20);
insertEnd(head, 30);
insertEnd(head, 40);

// Display the list


cout << "Singly Linked List: ";
displayList(head);

// Deleting a node
deleteNode(head, 20);
cout << "After deleting 20: ";
displayList(head);

deleteNode(head, 50); // Node not found


cout << "After attempting to delete 50: ";
displayList(head);
return 0;
}

2. Doubly Linked List (Two Way Linked List)

cpp
Copy
#include <iostream>
using namespace std;

// Node structure for doubly linked list


struct Node {
int data;
Node* next;
Node* prev;
};

// Function to create a new node


Node* createNode(int data) {
Node* newNode = new Node();
newNode->data = data;
newNode->next = nullptr;
newNode->prev = nullptr;
return newNode;
}

// Function to insert a node at the end of the list


void insertEnd(Node*& head, int data) {
Node* newNode = createNode(data);
if (head == nullptr) {
head = newNode;
} else {
Node* temp = head;
while (temp->next != nullptr) {
temp = temp->next;
}
temp->next = newNode;
newNode->prev = temp;
}
}

// Function to delete a node by value


void deleteNode(Node*& head, int data) {
if (head == nullptr) {
cout << "List is empty.\n";
return;
}

Node* temp = head;

// If the node to be deleted is the head


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

// Traverse to find the node to delete


while (temp != nullptr && temp->data != data) {
temp = temp->next;
}

if (temp == nullptr) {
cout << "Node with value " << data << " not found.\n";
return;
}

// Update the pointers


if (temp->next != nullptr) {
temp->next->prev = temp->prev;
}
if (temp->prev != nullptr) {
temp->prev->next = temp->next;
}

delete temp;
}

// Function to display the list from head to tail


void displayList(Node* head) {
if (head == nullptr) {
cout << "List is empty.\n";
return;
}
Node* temp = head;
while (temp != nullptr) {
cout << temp->data << " ";
temp = temp->next;
}
cout << endl;
}

// Function to display the list from tail to head


void displayReverse(Node* head) {
if (head == nullptr) {
cout << "List is empty.\n";
return;
}

Node* temp = head;


while (temp->next != nullptr) {
temp = temp->next;
}

// Traverse the list backward


while (temp != nullptr) {
cout << temp->data << " ";
temp = temp->prev;
}
cout << endl;
}
// Main function to test the doubly linked list
int main() {
Node* head = nullptr;

// Inserting nodes into the list


insertEnd(head, 10);
insertEnd(head, 20);
insertEnd(head, 30);
insertEnd(head, 40);

// Display the list


cout << "Doubly Linked List: ";
displayList(head);

// Display list in reverse order


cout << "Doubly Linked List (Reversed): ";
displayReverse(head);

// Deleting a node
deleteNode(head, 20);
cout << "After deleting 20: ";
displayList(head);

deleteNode(head, 50); // Node not found


cout << "After attempting to delete 50: ";
displayList(head);

return 0;
}

You might also like