Untitled
unknown
c_cpp
a year ago
5.1 kB
12
Indexable
#include<iostream>
class Node {
int data;
Node* next;
public:
Node* last;
Node(){last = NULL;}
void addBeg(int);
void addEnd(int);
void deleteBeg();
void deleteEnd();
void deleteAny(int);
void display();
Node* createNode(int);
};
Node* Node::createNode(int item){
Node* ptr = new Node();
ptr -> data = item;
return ptr;
}
void Node::addBeg(int item){
Node* curr = createNode(item);
if(last == NULL){
last = curr;
curr -> next = curr;
}
curr -> next = last -> next;
last -> next = curr;
std::cout << item << " is inserted at the beginning of the list!" << std::endl;
}
void Node::addEnd(int item){
Node* curr = createNode(item);
if(last == NULL){
last = curr;
curr -> next = curr;
}
curr -> next = last -> next;
last -> next = curr;
last = curr;
std::cout << item << " is inserted at the end of the list!" << std::endl;
}
void Node::display(){
if(last == NULL){
std::cout << "Empty List!" << std::endl;
return;
}
Node* temp = last -> next;
do{
std::cout << temp -> data << " ";
temp = temp -> next;
}while(temp != last -> next);
}
void Node::deleteBeg(){
if(last == NULL){
std::cout << "Empty List!" << std::endl;
return;
}
if(last == last -> next){
delete last;
last = NULL;
std::cout << "The Beginning Node is deleted!" << std::endl;
return;
}
Node* temp = last -> next;
last -> next = last -> next -> next;
delete temp;
std::cout << "The Beginning Node is deleted!" << std::endl;
}
void Node::deleteEnd(){
if(last == NULL){
std::cout << "Empty List!" << std::endl;
return;
}
if(last == last -> next){
delete last;
last = NULL;
std::cout << "The Ending Node is deleted!" << std::endl;
return;
}
Node* temp = last -> next;
while(temp -> next != last)
temp = temp -> next;
temp -> next = last -> next;
delete last;
last = temp;
std::cout << "The Ending Node is deleted!" << std::endl;
}
void Node::deleteAny(int item){
if(last == NULL){
std::cout << "Empty List!" << std::endl;
return;
}
Node* temp = last -> next;
Node* temp2 = last -> next;
bool found = false;
do {
if (temp->data == item) {
found = true;
break;
}
temp = temp->next;
} while (temp != last->next);
if (!found) {
std::cout << item << " is not found!!" << std::endl;
return;
}
while(temp -> data != item)
temp = temp -> next;
while(temp2 -> next -> data != item)
temp2 = temp2 -> next;
if(temp == last)
last = temp2;
if(temp == temp2){
delete last;
last = NULL;
std::cout << item << " is deleted successfully!" << std::endl;
return;
}
temp2 -> next = temp -> next;
delete temp;
std::cout << item << " is deleted successfully!" << std::endl;
}
int main() {
Node LinkedList;
int choice;
int node;
while(true){
std::cout << "\nMenu:\n";
std::cout << "1. Check if the Linked List is Empty\n";
std::cout << "2. Add a node from the Begining\n";
std::cout << "3. Add a node at the End\n";
std::cout << "4. Display the Linked List\n";
std::cout << "5. Delete a node from the Begining\n";
std::cout << "6. Delete a node from the End\n";
std::cout << "7. Delete any node\n";
// std::cout << "8. Display the Linked List in reversed order\n";
std::cout << "0. Exit\n";
std::cout << "Enter your choice: ";
std::cin >> choice;
switch(choice) {
case 2:
std::cout << "Enter the node:";
std::cin >> node;
LinkedList.addBeg(node);
break;
case 3:
std::cout << "Enter the node:";
std::cin >> node;
LinkedList.addEnd(node);
break;
case 4:
LinkedList.display();
break;
case 5:
LinkedList.deleteBeg();
break;
case 6:
LinkedList.deleteEnd();
break;
case 7:
std::cout << "Enter the node:";
std::cin >> node;
LinkedList.deleteAny(node);
break;
// case 8:
// std::cout << "Enter the node:";
// std::cin >> node;
// LinkedList.search(node);
// break;
case 0:
std::cout << "Exiting...\n";
return 0;
default:
std::cout << "Invalid choice! Please try again.\n";
}
}
}Editor is loading...
Leave a Comment