C++ Saved Program: placid-rain

facebook share

Here's the online compiler accompanied with IDE at your service. This is a saved code by one of our users. Try it out or save your own from the IDE page and don't forget to leave a feedback. You can find that at the bottom of the page.


#include <iostream.h>
#include <stdlib.h>
Class node()
{
   
   Public:
	struct node
	{
    	struct node *left;
   	 int n;
  	  struct node *right;
	}*h,*temp,*temp1,*first,*temp4;
}
Class chain()
{
    Friend class node();
    Public:
	void insert_begin();
	void insert2();
	void insert_position();
	void display();
	void traverseend(int);
	void sort();
	void search();
	void update();
	void delete();
    private:
	int count = 0;
 }
int main()
{
    chain<int s>
    int ch;
 
    h = NULL;
    #include <iostream.h>
#include <stdlib.h>
Class node()
{
   
   Public:
	struct node
	{
    	struct node *left;
   	 int n;
  	  struct node *right;
	}*h,*temp,*temp1,*first,*temp4;
}
Class chain()
{
    Friend class node();
    Public:
	void insert_begin();
	void insert2();
	void insert_position();
	void display();
	void traverseend(int);
	void sort();
	void search();
	void update();
	void delete();
    private:
	int count = 0;
 }
int main()
{
    chain<int s>
    int ch;
 
    h = NULL;
    temp = temp1 = NULL;
     while (1)
    {
   	    cout<<"\n 1 - Insert at beginning ";
      	cout<<"\n 2 - Insert at position ";
        cout<<"\n 3 - Delete at position ";
        cout<<"\n 4 - Display";
  	    cout<<"\n 5 - Search for element";
        cout<<"\n 6 - create a node";
   	    cout<<"\n 7 - Exit";
        cout<<"\n Enter choice : ";
        cin>>ch;
        switch (ch)
        {
        case 1:
            s.insert_begin();
            break;
        case 2:
            s.insert_position();
            break;
        case 3:
            s.delete();
            break;
        case 4:
            s.display();
            break;
        case 5:
            s.search();
            break;
	case 6:
	     s.create();
	     break;
        case 7:
            exit(0);
        default:
            cout<<f("\nEntered Wrong choice");
        }
    }
}
void chain<int>::create()
{
    int data;
  
    node *temp =new node();
    temp->left = NULL;
    temp->right = NULL;
    cout<<"\n Enter value to node : ";
    cin>>data;
    temp->n = data;
    count++;
}
 
void insert_begin()
{
    if (h == NULL)
    {
        create();
        h = temp;
        temp1 = h;
    }
    else
    {
        create();
        temp->right = h;
        h->left = temp;
        h = temp;
    }
}

void insert_position()
{
    int pos, i = 2;
 
    cout<<"\n Enter position to be inserted : ";
    cin>>pos;
    first = h;
 
    if ((pos < 1) || (pos >= count + 1))
    {
        cout<<"\n Position out of range to insert";
        return;
    }
    if ((h == NULL) && (pos != 1))
    {
        cout<<"\n Empty list cannot insert other than 1st position";
        return;
    }
    if ((h == NULL) && (pos == 1))
    {
        create();
        h = temp;
        temp1 = h;
        return;
    }
    else
    {
        while (i < pos)
        {
            first = first->right;
            i++;
        }
        create();
        temp->left = first;
        temp->right = first->right;
        first->right->left = temp;
        first->right = temp;
    }
}
void delete()
{
    int i = 1, pos;
 
    cout<<"\n Enter position to be deleted : ";
    cin>>pos;
    first = h;
 
    if ((pos < 1) || (pos >= count + 1))
    {
        cout<<"\n Error : Position out of range to delete";
        return;
    }
    if (h == NULL)
    {
        cout<<"\n Error : Empty list no elements to delete";
        return;
    }
    else
    {
        while (i < pos)
        {
            first = first->right;
            i++;
        }
        if (i == 1)
        {
            if (first->right == NULL)
            {
                cout<<"Node deleted from list";
                delete first;
                first = h = NULL;
                return;
            }
        }
        if (first->right == NULL)
        {
            first->left->right = NULL;
            delete first;
            cout<<"Node deleted from list";
            return;
        }
        first->right->left = first->left;
        if (i != 1)
            first->left->right = first->right;    
        if (i == 1)
            h = first->right;
        cout<<"\n Node deleted";
        delete first;
    }
    count--;
}
void display()
{
    first = h;
 
    if (first == NULL)
    {
        cout<<"\n List empty to display \n";
        return;
    }
    cout<<"\n Linked list elements from begining : ";
 
    while (first->right != NULL)
    {
        cout<<first->n;
        first = first->right;
    }
    cout<<first->n;
}
 
void search()
{
    int data, count = 0;
    first = h;
 
    if (first == NULL)
    {
        cout<<"\n Error : List empty to search for data";
        return;
    }
    cout<<"\n Enter value to search : ";
    cin>>data;
    while (first != NULL)
    {
        if (first->n == data)
        {
            cout<<"\n Data found in %d position"<<count + 1;
            return;
        }
        else
             first = first->right;
            count++;
    }
    cout<<"\n Error :,"<<data<<"not found in list";
}
Execute

Output

Press the execute button or CTRL + enter to run code.

Inputs