Double Linked List-C++....

#include <iostream>
#include <conio.h>
using namespace std;
struct node
    {
     int data;
     node *prev, *next;
    }*q, *temp, *start=NULL;
int c1, c2 ;
void create();
void display();
void insert();
void del();

int  main()
{
 
    while(1)
    {
 
                cout <<" \t\t ******* MAIN MENU ******\n" ;
                cout <<"Press 1 for adding data \n" ;
                cout <<"Press 2 for displaying data \n" ;
                cout <<"Press 3 for insertion \n" ;
                cout <<"Press 4 for deletion \n" ;
                cout <<"Press 0 for exit\n\nAns. " ;
                char ch;
                cin>>ch;
                switch(ch)
                {
                     case '1':
                      cout<<"At how many nodes you want to enter data?\nAns. ";
                      int n;
                      cin>>n;
                                                for(int i=0;i<n;i++)
                                                create();
                                                cout<<endl<<endl<<endl;
                                                break;
                     case '2':
                                             
                                                display();
                                                getch();
                                                break;
                     case '3':
                                             
                                                insert();
                                                break;
                     case '4':
                                             
                                                del();
                                                break;

                     case '0':
                                                                  return 0;

                }
    }

getch();
return 0;
}
 void create()
 {
    temp = new node;
    temp -> next = NULL;
    cout << "Enter data :  " ;
    cin >> temp -> data ;
    if(start == NULL)
    {
                start = temp;
                temp -> prev = NULL;
    }
    else
    {
                q= start;
                while(q->next != NULL)
                {
                    q = q->next;
                }
                q->next = temp;
                temp->prev = q;
    }

 }
 void display()
 { cout<<"\n\nData you enterd : ";
       q=start;
       while(q!=NULL)
       {
                   cout<<q->data<<"\t";
                   q = q->next;
       }
 cout<<endl<<endl<<endl;
 }
 void insert()
 {
      cout << "\n\nPress 1 for insertion at start\n" ;
      cout << "Press 2 for insertion at middle\n" ;
      cout << "Press 3 for insertion at end\nAns. " ;
      int choice;
      cin>>choice;
      switch(choice)
      {
                   case 1:
                             
                                  temp = new node;
                                  cout<<"Enter data : ";
                                  cin>>temp->data;
                                  start->prev =temp;
                                  temp->next = start;
                                  start =  temp;
                                  temp -> prev = NULL;
                                  break;
                   case 2:
                             
                                  cout<<"Enter the data aftre which u want to add this\n";
                                  int ch;
                                  cin>>ch;
                                  q= start;
                                  while(q->next!=NULL)
                                  {
                                       if(q->data == ch)
                                       {
                                                   temp = new node;
                                                   cout<<"Enter data \n";
                                                   cin>>temp->data;
                                                   q->next->prev = temp;
                                                   temp->next = q->next;
                                                   temp->prev = q;
                                                   q->next = temp;

                                       }
                                       q = q->next;
                                  }
                                  break;
                   case 3:
                               
                                  temp = new node;
                                  cout<<"Enter data\n";
                                  cin>> temp->data;
                                  temp->next = NULL;
                                  q =  start;
                                  while(q->next != NULL)
                                  {
                                      q= q->next;
                                  }
                                  q->next =  temp;
                                  temp->prev = NULL;
      }
 cout<<endl<<endl<<endl;
 }
 void del()
 {
 
    cout<<"Enter the data you want to delete \n";
    int num;
    cin>>num;
    q = start;
    if (start->data == num)
    start = start -> next;
    else
    {
    while(q != NULL)
    {
       if(q->next->data == num)
       {
                   temp = q->next;
                   q->next = temp->next;
                   temp->next->prev = q;
                   delete temp;
       }
       q = q->next;
    }
    }
    cout<<num<<" is deleted from list";
 cout<<endl<<endl<<endl;
 }


Popular posts from this blog

Shutdown Pc

Ellipse using OpenGl

String Comparisons