0% found this document useful (0 votes)
18 views21 pages

Program 1

Uploaded by

mahambilal020
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)
18 views21 pages

Program 1

Uploaded by

mahambilal020
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/ 21

PROGRAM 1:

Write C++ code


1. To create and traverse a single linked list.
2. To create a single linked list of n nodes. (Input the number of nodes, n, in
the linked list)
3. To insert a new node at the beginning of the single linked list.
4. To insert a new node at the end of the single linked list.
5. To delete node at the beginning of the single linked list.
6. To delete node at the end of the single linked list.
7. To search an element in the single linked list.
8. To display a single linked list.
INPUT:
#include <iostream>
using namespace std;

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

Node* createNode(int data) {


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

void traverseList(Node* head) {


Node* current = head;
Node* nullptr;
while (current != nullptr) {
cout << current->data << " -> ";
current = current->next;
}
cout << "nullptr" << endl;
}

Node* createLinkedList(int n) {
Node* nullptr;
Node* head = nullptr;
Node* tail = nullptr;

for (int i = 0; i < n; i++) {


int data;
cout << "Enter data for node " << (i + 1) << ": ";
cin >> data;
Node* newNode = createNode(data);
if (head == nullptr) {
head = newNode;
tail = newNode;
} else {
tail->next = newNode;
tail = newNode;
}
}
return head;
}

Node* insertAtBeginning(Node* head, int data) {


Node* newNode = createNode(data);
newNode->next = head;
return newNode;
}

Node* insertAtEnd(Node* head, int data) {


Node* newNode = createNode(data);
Node* nullptr;
if (head == nullptr) {
return newNode;
}
Node* current = head;
while (current->next != nullptr) {
current = current->next;
}
current->next = newNode;
return head;
}

Node* deleteAtBeginning(Node* head) {


Node* nullptr;
if (head == nullptr) {
cout << "List is already empty." << endl;
return nullptr;
}
Node* temp = head;
head = head->next;
delete temp;
return head;
}

Node* deleteAtEnd(Node* head) {


Node* nullptr;
if (head == nullptr) {
cout << "List is already empty." << endl;
return nullptr;
}
if (head->next == nullptr) {
delete head;
return nullptr;
}
Node* current = head;
while (current->next->next != nullptr) {
current = current->next;
}
delete current->next;
current->next = nullptr;
return head;
}

bool searchElement(Node* head, int key) {


Node* current = head;
Node*nullptr;
while (current != nullptr) {
if (current->data == key) {
return true;
}
current = current->next;
}
return false;
}

void displayList(Node* head) {


Node* nullptr;
if (head == nullptr) {
cout << "List is empty." << endl;
return;
}
traverseList(head);
}

int main() {
Node* nullptr;
Node* head = nullptr;
int n;

cout << "Enter the number of nodes for the linked list: ";
cin >> n;
head = createLinkedList(n);

cout << "Initial linked list: ";


displayList(head);

head = insertAtBeginning(head, 10);


cout << "After inserting 10 at the beginning: ";
displayList(head);

head = insertAtEnd(head, 20);


cout << "After inserting 20 at the end: ";
displayList(head);

head = deleteAtBeginning(head);
cout << "After deleting at the beginning: ";
displayList(head);

head = deleteAtEnd(head);
cout << "After deleting at the end: ";
displayList(head);

int key;
cout << "Enter an element to search: ";
cin >> key;
if (searchElement(head, key)) {
cout << key << " is found in the list." << endl;
} else {
cout << key << " is not found in the list." << endl;
}

return 0;
}
OUTPUT:

PROGRAM 2:
Write C++ code
1. To create a double linked list.
2. To traverse a double linked list in forward direction.
3. To create and traverse a double linked list in backward direction.
4. To insert a new node at the beginning of the double linked list.
5. To insert a new node at the end of the double linked list.
6. To delete node at the beginning of the double linked list.
7. To delete node at the end of the double linked list.
8. To search an element in the double linked list.
INPUT:
#include <iostream>
using namespace std;

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

};

Node* createNode(int data) {


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

void traverseForward(Node* head) {


Node* current = head;
Node* nullptr;
while (current != nullptr) {
cout << current->data << " <-> ";
current = current->next;
}
cout << "nullptr" << endl;
}

void traverseBackward(Node* tail) {


Node* current = tail;
Node* nullptr;
while (current != nullptr) {
cout << current->data << " <-> ";
current = current->prev;
}
cout << "nullptr" << endl;
}

Node* createDoublyLinkedList(int n) {
Node* nullptr;
Node* head = nullptr;
Node* tail = nullptr;

for (int i = 0; i < n; i++) {


int data;
cout << "Enter data for node " << (i + 1) << ": ";
cin >> data;
Node* newNode = createNode(data);
if (head == nullptr) {
head = newNode;
tail = newNode;
} else {
tail->next = newNode;
newNode->prev = tail;
tail = newNode;
}
}
return head;
}

Node* insertAtBeginning(Node* head, int data) {


Node* newNode = createNode(data);
Node* nullptr;
if (head != nullptr) {
newNode->next = head;
head->prev = newNode;
}
return newNode;
}

Node* insertAtEnd(Node* head, int data) {


Node* newNode = createNode(data);
Node* nullptr;
if (head == nullptr) {
return newNode;
}
Node* current = head;
while (current->next != nullptr) {
current = current->next;
}
current->next = newNode;
newNode->prev = current;
return head;
}

Node* deleteAtBeginning(Node* head) {


Node* nullptr;
if (head == nullptr) {
cout << "List is already empty." << endl;
return nullptr;
}
Node* temp = head;
head = head->next;
if (head != nullptr) {
head->prev = nullptr;
}
delete temp;
return head;
}

Node* deleteAtEnd(Node* head) {


Node* nullptr;
if (head == nullptr) {
cout << "List is already empty." << endl;
return nullptr;
}
if (head->next == nullptr) {
delete head;
return nullptr;
}
Node* current = head;
while (current->next != nullptr) {
current = current->next;
}
current->prev->next = nullptr;
delete current;
return head;
}

bool searchElement(Node* head, int key) {


Node* current = head;
Node* nullptr;
while (current != nullptr) {
if (current->data == key) {
return true;
}
current = current->next;
}
return false;
}

int main() {
Node* nullptr;
Node* head = nullptr;
int n;

cout << "Enter the number of nodes for the doubly linked list: ";
cin >> n;
head = createDoublyLinkedList(n);

cout << "Doubly linked list in forward direction: ";


traverseForward(head);

Node* tail = head;


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

cout << "Doubly linked list in backward direction: ";


traverseBackward(tail);
head = insertAtBeginning(head, 10);
cout << "After inserting 10 at the beginning: ";
traverseForward(head);

head = insertAtEnd(head, 20);


cout << "After inserting 20 at the end: ";
traverseForward(head);

head = deleteAtBeginning(head);
cout << "After deleting at the beginning: ";
traverseForward(head);

head = deleteAtEnd(head);
cout << "After deleting at the end: ";
traverseForward(head);

int key;
cout << "Enter an element to search: ";
cin >> key;
if (searchElement(head, key)) {
cout << key << " is found in the list." << endl;
} else {
cout << key << " is not found in the list." << endl;
}

return 0;
}
OUTPUT:
PROGRAM3:
Write C++ code
1. To create a circular linked list.
2. To traverse a circular linked list
INPUT:
#include <iostream>
using namespace std;

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

Node* createNode(int data) {


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

Node* createCircularLinkedList(int n) {
Node* nullptr;
if (n <= 0) return nullptr;

Node* head = createNode(1);


Node* last = head;

for (int i = 2; i <= n; i++) {


Node* newNode = createNode(i);
last->next = newNode;
last = newNode;
}

last->next = head;
return head;
}

void traverseCircularList(Node* head) {


Node* nullptr;
if (head == nullptr) {
cout << "The list is empty." << endl;
return;
}

Node* current = head;


do {
cout << current->data << " -> ";
current = current->next;
} while (current != head);
cout << "(back to head: " << head->data << ")" << endl;
}

int main() {
int n;

cout << "Enter the number of nodes for the circular linked list: ";
cin >> n;
Node* head = createCircularLinkedList(n);

cout << "Circular linked list traversal: ";


traverseCircularList(head);

return 0;
}
OUTPUT:
PROGRAM 4
Write C++ code
1. To create a double circular linked list.
2. To traverse a double circular linked list
INPUT:
#include <iostream>
using namespace std;
class Node
{
public:
int val;
Node *next;
Node *prev;
Node(int data)
{
this->val = data;
this->next = NULL;
this->prev = NULL;
}
};
class doubleCircularLinkedList
{
public:
Node *head = NULL;
void insert(int data)
{
Node *new_node = new Node(data);
if (head == NULL)
{
head = new_node;
new_node->next = head;
new_node->prev = head;
}
else
{
Node *last = head->prev;
new_node->next = head;
new_node->prev = last;
last->next = new_node;
head->prev = new_node;
head = new_node;
}
}
void display()
{
if (head == NULL)
{
cout << "List is empty" << endl;
return;
}
Node *temp = head;
do
{
cout << temp->val << " <-> ";
temp = temp->next;
} while (temp != head);
cout << endl;
}
void printCircular()
{
Node *temp = head;
for (int i = 0; i < 10; i++)
{
cout << temp->val << "->";
temp = temp->next;
}
cout << endl;
}
};
int main()
{

doubleCircularLinkedList dcll;
dcll.insert(1);
dcll.insert(2);
dcll.insert(3);
dcll.insert(4);
dcll.insert(5);
dcll.display();
dcll.printCircular();
return 0;
}
OUTPUT:

You might also like