7.
C++ Program to Delete the First Node in a given Singly Linked List
Algorithm:
1. Start the program
2. A function removeFirstNode (node*&head) takes the pointer to the head and deletes the first
node of the linked list.
3. Create a temporary pointer that initially points to the head.
4. Head moves to the next node.
5. Delete the temporary pointer.
6. Return the linked list.
7. Stop the program.
Program:
#include <iostream>
using namespace std;
struct Node {
int data;
struct Node* next;
};
Node* removeFirstNode(struct Node* head)
{
if (head == NULL)
return NULL;
Node* temp = head;
head = head->next;
delete temp;
return head;
}
void push(struct Node** head_ref, int new_data)
{
struct Node* new_node = new Node;
new_node->data = new_data;
new_node->next = (*head_ref);
(*head_ref) = new_node;
}
int main()
{
Node* head = NULL;
push(&head, 12);
push(&head, 29);
push(&head, 11);
push(&head, 23);
push(&head, 8);
head = removeFirstNode(head);
for (Node* temp = head; temp != NULL; temp = temp->next)
cout << temp->data << " ";
return 0;
}
Output:
23 11 29 12
8.C++ program to delete a linked list
Algorithm:
1. Start the program.
2. Check if the Linked List is empty as we cannot delete from an empty Linked List.
3. Check if the Linked List has only one Node.
4. In this case, just point the head to NULL and free memory for the existing node.
5. Otherwise, if the linked list has more than one node, traverse to the end of the Linked
List.
6. Point next of 2nd Last node to NULL.
7. Free the memory for the last node.
8. Stop the program.
Program:
#include <bits/stdc++.h>
using namespace std;
class Node {
public:
int data;
Node* next;
};
void deleteList(Node** head_ref)
{
Node* current = *head_ref;
Node* next = NULL;
while (current != NULL)
{
next = current->next;
free(current);
current = next;
}
*head_ref = NULL;
}
void push(Node** head_ref, int new_data)
{
Node* new_node = new Node();
new_node->data = new_data;
new_node->next = (*head_ref);
(*head_ref) = new_node;
}
int main()
{
Node* head = NULL;
push(&head, 1);
push(&head, 4);
push(&head, 1);
push(&head, 12);
push(&head, 1);
cout << "Deleting linked list";
deleteList(&head);
cout << "\nLinked list deleted";
}
Output:
Deleting linked list
Linked list deleted
9. Program to eliminate duplicates from linked list.
Algorithm:
1. Start the program.
2. Create a class Node which has two attributes: data and next. Next is a pointer to the next
node in the list.
3. Create another class removeDuplicate which has two attributes: head and tail.
4. push() will add a new node to the list:
1. Create a new node.
2. It first checks, whether the head is equal to null which means the list is empty.
3. If the list is empty, both head and tail will point to a newly added node.
4. If the list is not empty, the new node will be added to end of the list such that tail's
next will point to a newly added node. This new node will become the new tail of the
list.
5. removeDuplicate() will remove duplicate nodes from the list.
1. Define a new node current which will initially point to head.
2. Node temp will point to current and index will always point to node next to current.
3. Loop through the list till current points to null.
4. Check whether current?s data is equal to index's data that means index is duplicate of
current.
5. Since index points to duplicate node so skip it by making node next to temp to will
point to node next to index, i.e. temp.next = index.next.
6. printList() will display the nodes present in the list:
1. Define a node current which will initially point to the head of the list.
2. Traverse through the list till current points to null.
3. Display each node by making current to point to node next to it in each iteration.
7. Stop the program.
Program:
#include <bits/stdc++.h>
using namespace std;
class Node
{
public:
int data;
Node* next;
};
void removeDuplicates(Node* head)
{
Node* current = head;
Node* next_next;
if (current == NULL)
return;
while (current->next != NULL)
{
if (current->data == current->next->data)
{
next_next = current->next->next;
free(current->next);
current->next = next_next;
}
else {
current = current->next;
}
}
}
void push(Node** head_ref, int new_data)
{
Node* new_node = new Node();
new_node->data = new_data;
new_node->next = (*head_ref);
(*head_ref) = new_node;
}
void printList(Node *node)
{
while (node!=NULL)
{
cout<<" "<<node->data;
node = node->next;
}
}
int main()
{
Node* head = NULL;
push(&head, 20);
push(&head, 13);
push(&head, 13);
push(&head, 11);
push(&head, 11);
push(&head, 11);
cout<<"Linked list before duplicate removal ";
printList(head);
removeDuplicates(head);
cout<<"\nLinked list after duplicate removal ";
printList(head);
return 0;
}
Output
Linked list before duplicate removal 11 11 11 13 13 20
Linked list after duplicate removal 11 13 20
10. Program to implement all the functions of a dictionary(ADT) using hashing.
Algorithm:
1. Start the program.
2. Hash function that maps keys to some values.
3. But these hashing function may lead to collision that is two or more keys are mapped to
same value.
4. To make each cell of hash table point to a linked list of records that have same hash function
value.
5.
Create a hash function, such that our hash table has ‘N’ number of buckets.
6. To insert a node into the hash table, we need to find the hash index for the given key. And it
could be calculated using the hash function as hashIndex = key % noOfBuckets.
Insert: Move to the bucket corresponds to the above calculated hash index and insert
the new node at the end of the list.
Delete: To delete a node from hash table, calculate the hash index for the key, move
to the bucket corresponds to the calculated hash index, search the list in the current
bucket to find and remove the node with the given key (if found).
7. Stop the program.
Program:
#include<bits/stdc++.h>
using namespace std;
class Hash
{
int BUCKET;
list<int> *table;
public:
Hash(int V);
void insertItem(int x);
void deleteItem(int key);
int hashFunction(int x) {
return (x % BUCKET);
}
void displayHash();
};
Hash::Hash(int b)
{
this->BUCKET = b;
table = new list<int>[BUCKET];
}
void Hash::insertItem(int key)
{
int index = hashFunction(key);
table[index].push_back(key);
}
void Hash::deleteItem(int key)
{
int index = hashFunction(key);
list <int> :: iterator i;
for (i = table[index].begin();
i != table[index].end(); i++) {
if (*i == key)
break;
}
if (i != table[index].end())
table[index].erase(i);
}
void Hash::displayHash() {
for (int i = 0; i < BUCKET; i++) {
cout << i;
for (auto x : table[i])
cout << " --> " << x;
cout << endl;
}
}
int main()
{
int a[] = {15, 11, 27, 8, 12};
int n = sizeof(a)/sizeof(a[0]);
Hash h(7);
for (int i = 0; i < n; i++)
h.insertItem(a[i]);
h.deleteItem(12);
h.displayHash();
return 0;
}
Output:
0
1 --> 15 --> 8
2
3
4 --> 11
5
6 --> 27
11.C++ implementation of Deque using doubly linked list
Algorithm:
1. Start the program.
2. Create a Node structure to represent a node in the doubly-linked list.
3. Create a class Deque to represent the deque data structure to be implemented.
4. Create front and rear pointers to keep track of the beginning and end of the deque and ini-
tialize the size variable with zero.
5. Create the following functions as part of the class Deque.
6. Stop the program.
Program:
#include <bits/stdc++.h>
using namespace std;
struct Node
{
int data;
Node *prev, *next;
static Node* getnode(int data)
{
Node* newNode = (Node*)malloc(sizeof(Node));
newNode->data = data;
newNode->prev = newNode->next = NULL;
return newNode;
}
};
class Deque
{
Node* front;
Node* rear;
int Size;
public:
Deque()
{
front = rear = NULL;
Size = 0;
}
void insertFront(int data);
void insertRear(int data);
void deleteFront();
void deleteRear();
int getFront();
int getRear();
int size();
bool isEmpty();
void erase();
};
bool Deque::isEmpty()
{
return (front == NULL);
}
int Deque::size()
{
return Size;
}
void Deque::insertFront(int data)
{
Node* newNode = Node::getnode(data);
if (newNode == NULL)
cout << "OverFlow\n";
else
{
if (front == NULL)
rear = front = newNode;
else
{
newNode->next = front;
front->prev = newNode;
front = newNode;
}
Size++;
}
}
void Deque::insertRear(int data)
{
Node* newNode = Node::getnode(data);
if (newNode == NULL)
cout << "OverFlow\n";
else
{
if (rear == NULL)
front = rear = newNode;
else
{
newNode->prev = rear;
rear->next = newNode;
rear = newNode;
}
Size++;
}
}
void Deque::deleteFront()
{
if (isEmpty())
cout << "UnderFlow\n";
else
{
Node* temp = front;
front = front->next;
if (front == NULL)
rear = NULL;
else
front->prev = NULL;
free(temp);
Size--;
}
}
void Deque::deleteRear()
{
if (isEmpty())
cout << "UnderFlow\n";
else
{
Node* temp = rear;
rear = rear->prev;
if (rear == NULL)
front = NULL;
else
rear->next = NULL;
free(temp);
Size--;
}
}
int Deque::getFront()
{
if (isEmpty())
return -1;
return front->data;
}
int Deque::getRear()
{
if (isEmpty())
return -1;
return rear->data;
}
void Deque::erase()
{
rear = NULL;
while (front != NULL)
{
Node* temp = front;
front = front->next;
free(temp);
}
Size = 0;
}
int main()
{
Deque dq;
cout << "Insert element '5' at rear end\n";
dq.insertRear(5);
cout << "Insert element '10' at rear end\n";
dq.insertRear(10);
cout << "Rear end element: "<< dq.getRear() << endl;
dq.deleteRear();
cout << "After deleting rear element new rear"<< " is: " << dq.getRear() << endl;
cout << "Inserting element '15' at front end \n";
dq.insertFront(15);
cout << "Front end element: "<< dq.getFront() << endl;
cout << "Number of elements in Deque: "<< dq.size() << endl;
dq.deleteFront();
cout << "After deleting front element new "<< "front is: " << dq.getFront() << endl;
return 0;
}
Output:
Insert element '5' at rear end
Insert element '10' at rear end
Rear end element: 10
After deleting rear element new rear is: 5
Inserting element '15' at front end
Front end element: 15
Number of elements in Deque: 2
After deleting front element new front is: 5
12.Program to create a doubly linked list.
Algorithm:
1. Start the program.
2. Define a Node class which represents a node in the list. It will have three properties: data,
previous which will point to the previous node and next which will point to the next node.
3. Define another class for creating a doubly linked list, and it has two nodes: head and tail.
Initially, head and tail will point to null.
4. push() will add node to the list:
1. It first checks whether the head is null, then it will insert the node as the head.
2. Both head and tail will point to a newly added node.
3. Head's previous pointer will point to null and tail's next pointer will point to null.
4. If the head is not null, the new node will be inserted at the end of the list such that
new node's previous pointer will point to tail.
5. The new node will become the new tail. Tail's next pointer will point to null.
5. printList() will show all the nodes present in the list.
1. Define a new node 'current' that will point to the head.
2. Print current.data till current points to null.
3. Current will point to the next node in the list in each iteration.
6. Stop the program.
Program:
#include <bits/stdc++.h>
using namespace std;
class Node
{
public:
int data;
Node* next;
Node* prev;
};
void push(Node** head_ref, int new_data)
{
Node* new_node = new Node();
new_node->data = new_data;
new_node->next = (*head_ref);
new_node->prev = NULL;
if ((*head_ref) != NULL)
(*head_ref)->prev = new_node;
(*head_ref) = new_node;
}
void insertAfter(Node* prev_node, int new_data)
{
if (prev_node == NULL)
{
cout<<"the given previous node cannot be NULL";
return;
}
Node* new_node = new Node();
new_node->data = new_data;
new_node->next = prev_node->next;
prev_node->next = new_node;
new_node->prev = prev_node;
if (new_node->next != NULL)
new_node->next->prev = new_node;
}
void append(Node** head_ref, int new_data)
{
Node* new_node = new Node();
Node* last = *head_ref;
new_node->data = new_data;
new_node->next = NULL;
if (*head_ref == NULL)
{
new_node->prev = NULL;
*head_ref = new_node;
return;
}
while (last->next != NULL)
last = last->next;
last->next = new_node;
new_node->prev = last;
return;
}
void printList(Node* node)
{
Node* last;
cout<<"\nTraversal in forward direction \n";
while (node != NULL)
{
cout<<" "<<node->data<<" ";
last = node;
node = node->next;
}
cout<<"\nTraversal in reverse direction \n";
while (last != NULL)
{
cout<<" "<<last->data<<" ";
last = last->prev;
}
}
int main()
{
Node* head = NULL;
append(&head, 6);
push(&head, 7);
push(&head, 1);
append(&head, 4);
insertAfter(head->next, 8);
cout << "Created DLL is: ";
printList(head);
return 0;
}
Output:
Created DLL is:
Traversal in forward Direction
175864
Traversal in reverse direction
468571
13. Program to Delete a given element from the above doubly linked list.
Algorithm:
1. Create a function which takes a linked list and node that had to be deleted as arguments
and delete the node.
2. If you want to delete a head node.
a) Change the head pointer to next of current node (head here).
b) Change the previous pointer of next node to current node previous.
3. If you want to delete middle node.
a) Change the previous pointer of next node to current node previous
b) Change the previous node next pointer to next of current node.
c) Delete the node. (Current node)
d) If previous or next is NULL you need not delete them. (For deleting last node)
4. On the given linked list, call the function and give which node you want to delete.
Program:
#include <bits/stdc++.h>
using namespace std;
struct Node {
int data;
Node *prev, *next;
};
void deleteNode(Node** head_ref, Node* del) {
if (*head_ref == NULL || del == NULL) {
return;
}
if (*head_ref == del) {
*head_ref = del->next;
}
if (del->next != NULL) {
del->next->prev = del->prev;
}
if (del->prev != NULL) {
del->prev->next = del->next;
}
free(del);
return;
}
void insertNode(Node** head_ref, int new_data) {
Node* new_node = new Node();
new_node->data = new_data;
new_node->prev = NULL;
new_node->next = (*head_ref);
if ((*head_ref) != NULL) {
(*head_ref)->prev = new_node;
}
(*head_ref) = new_node;
}
void printLinkedList(Node* node) {
while (node != NULL) {
cout << node->data << " -> ";
node = node->next;
}
}
int main() {
Node* head = NULL;
insertNode(&head, 1);
insertNode(&head, 2);
insertNode(&head, 3);
insertNode(&head, 4);
insertNode(&head, 5);
cout << "Linked List before deletion:" << endl;
printLinkedList(head);
deleteNode(&head, head->next);
cout << "\nLinked List after deletion:" << endl;
printLinkedList(head);
return 0;
}
Output:
If you execute the above program, then you will get the following result.
Linked List before deletion:
5 -> 4 -> 3 -> 2 -> 1 ->
Linked List after deletion:
5 -> 3 -> 2 -> 1 ->