Program 1
Program 1
struct Node {
int data;
Node* next;
Node* nullptr;
};
Node* createLinkedList(int n) {
Node* nullptr;
Node* head = nullptr;
Node* tail = nullptr;
int main() {
Node* nullptr;
Node* head = nullptr;
int n;
cout << "Enter the number of nodes for the linked list: ";
cin >> n;
head = createLinkedList(n);
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* createDoublyLinkedList(int n) {
Node* nullptr;
Node* head = nullptr;
Node* tail = nullptr;
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);
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* createCircularLinkedList(int n) {
Node* nullptr;
if (n <= 0) return nullptr;
last->next = head;
return head;
}
int main() {
int n;
cout << "Enter the number of nodes for the circular linked list: ";
cin >> n;
Node* head = createCircularLinkedList(n);
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: