Files
seer/tests/hellocirlelist/hellocirclelist.cpp
T

198 lines
4.5 KiB
C++
Raw Normal View History

2022-09-01 19:17:11 -05:00
#include<iostream>
// https://www.softwaretestinghelp.com/circular-linked-list/
using namespace std;
2022-09-04 12:01:51 -05:00
struct Node {
2022-09-01 19:17:11 -05:00
int data;
2022-09-04 12:01:51 -05:00
struct Node* next;
2022-09-01 19:17:11 -05:00
};
//insert a new node in an empty list
2022-09-04 12:01:51 -05:00
struct Node* insertInEmpty(struct Node* last, int new_data) {
2022-09-01 19:17:11 -05:00
// if last is not null then list is not empty, so return
2022-09-04 12:01:51 -05:00
if (last != NULL) {
2022-09-01 19:17:11 -05:00
return last;
2022-09-04 12:01:51 -05:00
}
2022-09-01 19:17:11 -05:00
// allocate memory for node
2022-09-04 12:01:51 -05:00
struct Node* temp = new Node;
2022-09-01 19:17:11 -05:00
// Assign the data.
temp -> data = new_data;
last = temp;
// Create the link.
last->next = last;
return last;
}
//insert new node at the beginning of the list
2022-09-04 12:01:51 -05:00
struct Node* insertAtBegin(struct Node* last, int new_data) {
2022-09-01 19:17:11 -05:00
//if list is empty then add the node by calling insertInEmpty
2022-09-04 12:01:51 -05:00
if (last == NULL) {
2022-09-01 19:17:11 -05:00
return insertInEmpty(last, new_data);
2022-09-04 12:01:51 -05:00
}
2022-09-01 19:17:11 -05:00
//else create a new node
2022-09-04 12:01:51 -05:00
struct Node* temp = new Node;
2022-09-01 19:17:11 -05:00
//set new data to node
temp -> data = new_data;
temp -> next = last -> next;
last -> next = temp;
return last;
}
//insert new node at the end of the list
2022-09-04 12:01:51 -05:00
struct Node* insertAtEnd(struct Node* last, int new_data) {
2022-09-01 19:17:11 -05:00
//if list is empty then add the node by calling insertInEmpty
2022-09-04 12:01:51 -05:00
if (last == NULL) {
2022-09-01 19:17:11 -05:00
return insertInEmpty(last, new_data);
2022-09-04 12:01:51 -05:00
}
2022-09-01 19:17:11 -05:00
//else create a new node
2022-09-04 12:01:51 -05:00
struct Node* temp = new Node;
2022-09-01 19:17:11 -05:00
//assign data to new node
temp -> data = new_data;
temp -> next = last -> next;
last -> next = temp;
last = temp;
return last;
}
//insert a new node in between the nodes
2022-09-04 12:01:51 -05:00
struct Node* insertAfter(struct Node* last, int new_data, int after_item) {
2022-09-01 19:17:11 -05:00
//return null if list is empty
2022-09-04 12:01:51 -05:00
if (last == NULL) {
2022-09-01 19:17:11 -05:00
return NULL;
2022-09-04 12:01:51 -05:00
}
2022-09-01 19:17:11 -05:00
2022-09-04 12:01:51 -05:00
struct Node* temp, *p;
2022-09-01 19:17:11 -05:00
p = last -> next;
2022-09-04 12:01:51 -05:00
do {
if (p ->data == after_item) {
2022-09-01 19:17:11 -05:00
temp = new Node;
temp -> data = new_data;
temp -> next = p -> next;
p -> next = temp;
2022-09-04 12:01:51 -05:00
if (p == last) {
2022-09-01 19:17:11 -05:00
last = temp;
2022-09-04 12:01:51 -05:00
}
2022-09-01 19:17:11 -05:00
return last;
}
2022-09-04 12:01:51 -05:00
2022-09-01 19:17:11 -05:00
p = p -> next;
2022-09-04 12:01:51 -05:00
} while (p != last -> next);
2022-09-01 19:17:11 -05:00
2022-09-04 12:01:51 -05:00
cout << "The node with data " << after_item << " is not present in the list." << endl;
return last;
2022-09-01 19:17:11 -05:00
}
//traverse the circular linked list
2022-09-04 12:01:51 -05:00
void traverseList(struct Node* last) {
struct Node* p;
2022-09-01 19:17:11 -05:00
// If list is empty, return.
if (last == NULL) {
cout << "Circular linked List is empty." << endl;
return;
}
p = last -> next; // Point to the first Node in the list.
// Traverse the list starting from first node until first node is visited again
do {
cout << p -> data << "==>";
p = p -> next;
2022-09-04 12:01:51 -05:00
} while (p != last->next);
if (p == last->next) {
cout << p->data;
}
cout << "\n\n";
2022-09-01 19:17:11 -05:00
}
//delete the node from the list
2022-09-04 12:01:51 -05:00
void deleteNode(Node** head, int key) {
2022-09-01 19:17:11 -05:00
// If linked list is empty retun
if (*head == NULL)
return;
// If the list contains only a single node,delete that node; list is empty
2022-09-04 12:01:51 -05:00
if ((*head)->data == key && (*head)->next == *head) {
2022-09-01 19:17:11 -05:00
free(*head);
2022-09-04 12:01:51 -05:00
*head = NULL;
2022-09-01 19:17:11 -05:00
}
2022-09-04 12:01:51 -05:00
Node* last = *head, *d;
2022-09-01 19:17:11 -05:00
// If key is the head
2022-09-04 12:01:51 -05:00
if ((*head)->data == key) {
while (last->next != *head) { // Find the last node of the list
last = last->next;
}
2022-09-01 19:17:11 -05:00
// point last node to next of head or second node of the list
2022-09-04 12:01:51 -05:00
last->next = (*head)->next;
2022-09-01 19:17:11 -05:00
free(*head);
2022-09-04 12:01:51 -05:00
*head = last->next;
2022-09-01 19:17:11 -05:00
}
// end of list is reached or node to be deleted not there in the list
2022-09-04 12:01:51 -05:00
while (last->next != *head&&last->next->data != key) {
last = last->next;
2022-09-01 19:17:11 -05:00
}
2022-09-04 12:01:51 -05:00
2022-09-01 19:17:11 -05:00
// node to be deleted is found, so free the memory and display the list
2022-09-04 12:01:51 -05:00
if (last->next->data == key) {
d = last->next;
last->next = d->next;
cout << "The node with data " << key << " deleted from the list" << endl;
2022-09-01 19:17:11 -05:00
free(d);
2022-09-04 12:01:51 -05:00
cout << endl;
cout << "Circular linked list after deleting " << key << " is as follows:" << endl;
2022-09-01 19:17:11 -05:00
traverseList(last);
2022-09-04 12:01:51 -05:00
} else {
cout << "The node with data " << key << " not found in the list" << endl;
2022-09-01 19:17:11 -05:00
}
}
// main Program
2022-09-04 12:01:51 -05:00
int main() {
struct Node* last = NULL;
2022-09-01 19:17:11 -05:00
last = insertInEmpty(last, 30);
last = insertAtBegin(last, 20);
last = insertAtBegin(last, 10);
last = insertAtEnd(last, 40);
last = insertAtEnd(last, 60);
last = insertAfter(last, 50,40 );
2022-09-04 12:01:51 -05:00
cout << "The circular linked list created is as follows:" << endl;
2022-09-01 19:17:11 -05:00
traverseList(last);
deleteNode(&last,10);
return 0;
}