#include <bits/stdc++.h>
#include <string>
using namespace std;

// Linked List Node
class Node {

public:
    string name;
    int price;
    Node* next;
    
	// Constructor
    Node(string info1, int info2)
    {
        name = info1;
        price = info2;
        next = NULL;
    }
};

// Inserts at the head of the list
void insertAtFront(Node*& head, string info1, int info2)
{
    Node* newnode = new Node(info1, info2);

    if (head == NULL) {          
	//Insertion in an empty list
       	newnode->next = newnode;
       	head = newnode;
       	
        return;
    }
     
	//Insertion at the front in non-empty list
	Node *temp = head;
	
	while (temp->next != head)
	{
		temp = temp->next;
	}
	temp->next = newnode;
	newnode->next = head;
	head = newnode;
}


// Inserts at the tail of the list
void insertAtEnd(Node*& head, string info1, int info2)
{

    if (head == NULL) {       
	//insertAtEnd - but list is empty , call insertAtFront function
   		insertAtFront(head, info1, info2);
   		
       return;
    }

    //Insertion at the end in non-empty list
    Node* newnode = new Node(info1, info2);     
   
    Node* temp = head;
    while (temp->next != head)
    {
    	temp = temp->next;
	}
    temp->next = newnode;
    newnode->next = head;
}

void deleteNode(Node*& head, string info1, int info2)
{
	// If linked list is empty
    if (head == NULL)
        return;
        
    // If the list contains only a single node
    if(head->name==info1 && head->price==info2 && head->next==head)
    {
        head=NULL;
        return;
    }
    
    Node *last=head,*d;
    
    // If head is to be deleted
    if(head->name==info1 && head->price==info2)  
    {
        // Find the last node of the list
        while(last->next!=head)
            last=last->next;
            
        // Point last node to the next of head i.e. 
        // the second node of the list
        last->next=head->next;
        head=last->next;
     	return;
    }
    
    // Either the node to be deleted is not found 
    // or the end of list is not reached
    while(last->next!=head && last->next->name!=info1 && last->next->price!=info2) 
    {
        last=last->next;
    }
    
    // If node to be deleted was found
    if(last->next->name==info1 && last->next->price==info2) 
    {
        d=last->next;
        last->next=d->next;
    }
}

void displaylist(Node* head)
{
	int node;
    Node* temp = head;
    //Traverse from head until end to display the node 
	if (head != NULL)
	{
		do{
			cout << temp->name << "->" << temp->price << endl;
			temp = temp->next;
			node++;
		} while (temp != head);
	}
	cout << "Number of displayed nodes: " << node << endl;
    cout << endl;
}

int main()
{
    Node* head = NULL;
    
    insertAtFront(head, "TW-1",130);
    insertAtEnd(head, "TW-5", 80);
  	insertAtEnd(head,"TW-3", 125);
    displaylist(head);
    
    insertAtFront(head, "TW-4",240);
    insertAtEnd(head, "TW-7", 110);
    deleteNode(head, "TW-5", 80); 
    displaylist(head);
    
    return 0;
}
