Auto AdSense

Showing posts with label Data Structures. Show all posts
Showing posts with label Data Structures. Show all posts

Tuesday, 18 November 2014

Data Structures Program on Tree Sort

      #include< iostream >
  using namespace std;
  struct tree
  {
     int info;
     tree *Left, *Right;
  };
  tree *root;
  class TreeSort
  {
     public:
       int no_of_elements;
       int elements[10];
     public:
       void getarray();
       void sortit();
       void insert1(int);
       tree *insert2(tree *, tree *);
       void display(tree *);
  };
  void TreeSort::getarray()
  {
     cout<<"How many elements? ";
     cin>>no_of_elements;
     cout<<"Insert array of element to sort: ";
     for(int i=0;i< no_of_elements;i++)
     {
       cin>>elements[i];
     }
  }
  void TreeSort::sortit()
  {
     for(int i = 0; i < no_of_elements; i++)
     {
       insert1(elements[i]);
     }
  }
  tree* TreeSort::insert2(tree *temp,tree *newnode)
  {
     if(temp==NULL)
     {
       temp=newnode;
     }
     else if(temp->info < newnode->info)
     {
       insert2(temp->Right,newnode);
       if(temp->Right==NULL)
         temp->Right=newnode;
     }
     else
     {
       insert2(temp->Left,newnode);
       if(temp->Left==NULL)
         temp->Left=newnode;
     }
     return temp;
  }
  void TreeSort::insert1(int n)
  {
     tree *temp=root,*newnode;
     newnode=new tree;
     newnode->Left=NULL;
     newnode->Right=NULL;
     newnode->info=n;
     root=insert2(temp,newnode);
  }
  /* Inorder traversal */
  void TreeSort::display(tree *t = root)
  {
     if(root==NULL)
     {
       cout<<"Nothing to display";
     }
     else
     if(t!=NULL)
     {
       display(t->Left);
       cout<< t->info<<" ";
       display(t->Right);
     }
  }
  int main()
  {
     TreeSort TS;
     TS.getarray();
     TS.sortit();
     TS.display();
     return 0;
  }   

Data Structures Program on Tree Search

      #include< iostream >
  #include< cstdlib >
  #include< string >
  using namespace std;
  struct tree
  {
     int info;
     tree *Left, *Right;
  };
  tree *root;
  class Binary_tree
  {
     string path;
     public:
       Binary_tree();
       void insert1(int);
       tree *insert2(tree *, tree *);
       void search(int);
       void pretrav(tree *);
       void intrav(tree *);
       void posttrav(tree *);
  };
  Binary_tree::Binary_tree()
  {
     root = NULL;
  }
  tree* Binary_tree::insert2(tree *temp,tree *newnode)
  {
     if(temp==NULL)
     {
       temp=newnode;
     }
     else if(temp->info < newnode->info)
     {
       insert2(temp->Right,newnode);
       if(temp->Right==NULL)
         temp->Right=newnode;
     }
     else
     {
       insert2(temp->Left,newnode);
       if(temp->Left==NULL) temp->Left=newnode;
     }
     return temp;
  }
  void Binary_tree::insert1(int n)
  {
     tree *temp=root,*newnode;
     newnode=new tree;
     newnode->Left=NULL;
     newnode->Right=NULL;
     newnode->info=n;
     root=insert2(temp,newnode);
  }
  void Binary_tree::pretrav(tree *t = root)
  {
     if(root == NULL)
     {
       cout<<"Nothing to display";
     }
     else if(t != NULL)
     {
       cout<< t->info<<" ";
       pretrav(t->Left);
       pretrav(t->Right);
     }
  }
  void Binary_tree::intrav(tree *t = root)
  {
     if(root==NULL)
     {
       cout<<"Nothing to display";
     }
     else if(t!=NULL)
     {
       intrav(t->Left);
       cout<< t->info<<" ";
       intrav(t->Right);
     }
  }
  void Binary_tree::posttrav(tree *t = root)
  {
     if(root==NULL)
     {
       cout<<"Nothing to display";
     }
     else if(t!=NULL)
     {
       posttrav(t->Left);
       posttrav(t->Right);
       cout<< t->info<<" ";
     }
  }
  void Binary_tree::search(int key)
  {
     tree *temp=root,*parent=root;
     path = "";
     if(temp==NULL)
     {
       cout<<"The tree is empty"<< endl;
     }
     else
     {
       path = "root";
       while(temp!=NULL && temp->info!=key)
       {
         parent=temp;
         if(temp->info< key)
         {
           temp=temp->Right;
           path += "->Right";
         }
         else
         {
           temp=temp->Left;
           path += "->Left";
         }
       }
     }
     if(temp==NULL)
     {
       cout<<"Key not found!";
     }
     else
     {
       cout<<"Key is found\n";
       cout<<"Paht is: ";
       cout<< path;
     }
  }
  int main()
  {
     Binary_tree bt;
     int choice, n, key;
     while(1)
     {
       cout<<"\n\t1. Insert\n\t2. Search\n\t3. Preorder Traversal\n\t4. Inorder Treversal\n\t5. Postorder Traversal\n\t6. Exit"<< endl;
       cout<<"Enter your choice: ";
       cin>>choice;
       switch(choice)
       {
         case 1:
           cout<<"Enter item: ";
           cin>>n;
           bt.insert1(n);
           break;
         case 2:
           cout<<"Enter element to search: ";
           cin>>key;
           bt.search(key);
           break;
         case 3:
           cout<< endl;
           bt.pretrav();
           break;
         case 4:
           cout<< endl;
           bt.intrav();
           break;
         case 5:
           cout<< endl;
           bt.posttrav();
           break;
         case 6:
           exit(0);
       }
     }
     return 0;
  }   

Data Structures Program on Tower Of Hanoi

        #include< iostream > //the c++ standard library for stream input output
  #include< cstdio > //the c standard library for standard input output
  #include< cstdlib > //for the exit function
  using namespace std;
  class arr //arr class that holds each stag
  {
     public:
     int a[100],b[100],c[100];
     int topa,topb,topc;
  }hanoi;
  int move=1; //counts the no. of moves
  int main()
  {
     void tower(int ,int *,int *,int *,int *,int *,int *); //function
    prototype
       void show(int); //function prototype
       int i;
       system("clear");
       cout<<"Enter the no of elements for which u want to solve the problem";
       scanf("%d",&i);
       system("clear");
       for(int j=0;j< i;j++) //feeds the elements in the arrs;
       {
         hanoi.a[j]=j+1;
         hanoi.b[j]=-1;
         hanoi.c[j]=-1;
       }
       hanoi.topa=i-1; //topa,topb,topc,mean the top of each arr
       hanoi.topb=-1;
       hanoi.topc=-1;
       cout<<" Initially "<<"";
       for(int j=i-1;j>-1;j--) //show the statusm of each arr
       {
         show(hanoi.a[j]);
         show(hanoi.b[j]);
         show(hanoi.c[j]);
         cout<<"";
       }
       cout<<"";
       tower(i,hanoi.a,hanoi.c,hanoi.b,&(hanoi.topa),&(hanoi.topc),&(hanoi.topb)); //call to do the job
  }
  void tower(int n,int src[],int dest[],int aux[],int *ts,int *td,int *ta)
  //the tower function passes
  { //the arrs along with the top pointers
     void show (int);
     if(n==1) //if one element is there in source arr ,
     { //then it is moved to the destination arr,
       dest[++(*td)]=src[(*ts)];
       src[*ts]=-1;
       (*ts)--;
       int max;
       max=((*ts)>(*td)?(*ts):(*td));
       max=(max>(*ta)?max:(*ta));
       cout<<" Move "<< move++<<"";
       for(int i=max;i>-1;i--) //status of arrs shown
       {
         show(hanoi.a[i]);
         show(hanoi.b[i]);
         show(hanoi.c[i]);
         cout<<"";
       }
       cout<<"";
       return;
     }
     tower(n-1,src,aux,dest,ts,ta,td); //else the
     tower(1,src,dest,aux,ts,td,ta); //problem is solved by
     tower(n-1,aux,dest,src,ta,td,ts); //recursive calls
  }
  void show(int a) //the show function shows the current status of the arrs
  {
     if(a==-1)
       cout<<"-";
     else
       cout<< a <<"";
  }        

Data Structures - A Templated Stack Data Structure Example

       #include < dos.h > // For sleep()
  #include < iostream.h > // For I/0
  #include < windows.h > // FOR MessageBox() API
  #include < conio.h >
  #define MAX 10 // MAXIMUM STACK CONTENT
  template < class T > // Using Templates so that any type of data can be // stored in Stack without multiple defination of class
  class stack
  {
   protected:
     T arr[MAX]; // Contains all the Data
   public:
     T item,r;
     int top; //Contains location of Topmost Data pushed onto Stack
     stack() //Constructor
     {
       for(int i=0;i< MAX;i++)
       {
         arr[i]=NULL; //Initialises all Stack Contents to NULL
       }
       top=-1; //Sets the Top Location to -1 indicating an empty stack
     }
     void push(T a) // Push ie. Add Value Function
     {
       top++; // increment to by 1
       if(top< MAX)
       {
         arr[top]=a; //If Stack is Vacant store Value in Array
       }
       else // Bug the User
       {
         MessageBox(0,"STACK IS FULL","STACK WARNING!",MB_ICONSTOP);
         top--;
       }
     }
     T pop() // Delete Item. Returns the deleted item
     {
       if(top==-1)
       {
         MessageBox(0,"STACK IS EMPTY","WARNING",MB_ICONSTOP);
         return NULL;
       }
       else
       {
         T data=arr[top]; //Set Topmost Value in data
         arr[top]=NULL; //Set Original Location to NULL
         top--; // Decrement top by 1
         return data; // Return deleted item
       }
     }
  };
  void main()
  {
     stack < int >a; // Create object of class a with int Template
     int opt=1;
     while (opt!=3)
     {
       clrscr();
       cout<<" MAX STACK CAPACITY="<<((MAX-a.top)-1)<<"";
       cout<<"1) Push Item";
       cout<<"2) Pop Item";
       cout<<"3) Exit";
       cout<<"Option?";
       cin>>opt;
       switch(opt)
       {
         case 1:
           cout<<"Which Number should be pushed?";
           cin>>a.item;
           a.push(a.item);
           break;
         case 2:
           a.r=a.pop();
           cout<<"Item popped from Stack is:"<< a.r << endl;
           sleep(2);
           break;
       }
     }
   }   

Data Structures - Take two doubly-linked lists of nodes and merges them into another doubly-linked list of nodes

       #include< iostream >
  using namespace std;
  class Node
  {
     public:
       int data;
       Node *next;
       Node *prev;
       Node()
       {
         data=0;
         next=prev=NULL;
       }
  };
  class Node1
  {
     public:
       int data;
       Node1 *next;
       Node1 *prev;
       Node1()
       {
         data=0;
         next=prev=NULL;
       }
  };
  class Node2
  {
     public:
       int data;
       Node2 *next;
       Node2 *prev;
       Node2()
       {
         data=0;
         next=prev=NULL;
       }
  };
  class DoublyLinkedList
  {
     private:
       Node *headNode, *tailnode;
       Node1 *headNode1, *tailNode1;
       Node2 *headNode2, *tailNode2;
     public:
       DoublyLinkedList()
       {
         headNode=tailNode=NULL;
         headNode1=tailNode1=NULL;
         headNode2=tailNode2=NULL;
       }
       void Insert();//Insert data into the two lists
       void Merge() ;//Merging two lists into one
       void Display();//Display only the merged list
  };
  void DoublyLinkedList::Insert()
  {
     char option;
     //This section inserts elements into the nodes of the first list
     do
     {
       Node *newnode = new Node();
       cin>>newnode->data;
       newnode->next=NULL;
       newnode->prev=NULL;
       if(headNode==NULL)
       {
         headNode= tailNode=newnode;
       }
       else
       {
         Node *curr = headNode;
         while(curr->next!=NULL)
         {
           curr=curr->next;
         }
         curr->next=tailNode=newnode;
         newnode->prev=curr;
       }
       cout<<"Enter y to continue adding data into the first list :";
       cin>>option;
     }
     while(option=='y'||option=='Y');
     //The section inserts the elements into the nodes of the second list
     do
     {
       Node1 *newnode = new Node1();
       cin>>newnode->data;
       newnode->next=NULL;
       newnode->prev=NULL;
       if(headNode1==NULL)
       {
         headNode1=tailNode1=newnode;
       }
       else
       {
         Node1 *curr = headNode1;
         while(curr->next!=NULL)
         {
           curr=curr->next;
         }
         curr->next= tailNode1=newnode;
         newnode->prev=curr;
       }
       cout<<"Enter y to continue adding data into the second list :";
       cin>>option;
     }
     while(option=='y'||option=='Y');
  }
  void DoublyLinkedList::Merge()
  {
     Node *currentNode=headNode;
     Node1 *currentNode1=headNode1;
     //This section of code copies all the data from list 1 into the new list
     while(currentNode!=NULL)
     {
       Node2 *newnode = new Node2();
       newnode->data = currentNode->data;
       newnode->next = NULL;
       newnode->prev = NULL;
       if(headNode2==NULL)
       {
         headNode2=tailNode2=newnode;
       }
       else
       {
         Node2 *temp = headNode2;
         while(temp->next!=NULL)
         {
           temp=temp->next;
         }
         temp->next=tailNode2=newnode;
         newnode->prev=temp;
       }
       currentNode=currentNode->next;
     }
     //This section of code appends the new list with data from the second list
     while(currentNode1!=NULL)
     {
       Node2 *newnode = new Node2();
       newnode->data = currentNode1->data;
       newnode->next = newnode->prev=NULL;
       if(tailNode2->next==NULL)
       {
         tailNode2->next=newnode;
         newnode->prev = tailNode2;
         tailNode2=newnode;
       }
       currentNode1= currentNode1->next;
     }
  }
  void DoublyLinkedList::Display()
  {
     Node2 *currentNode2 = headNode2;
     while(currentNode2!=NULL)
     {
       cout << currentNode2->data <<" ";
       currentNode2=currentNode2->next;
     }
     cout<< endl;
  }
  void main()
  {
     DoublyLinkedList DLL;
     DLL.Insert();
     DLL.Merge();
     DLL.Display();
  }       

Data Structures Program on Stack With OOP and Exception Handling Concept

       #ifndef STACK_H
  #define STACK_H
  template < class A >
  class Stack
  {
    public:
       virtual void makeNewStack(int) = 0 ;
       virtual void push(A) = 0 ;
       virtual A pop() = 0 ;
       virtual bool isEmpty() = 0 ;
       virtual bool isFull() = 0 ;
       virtual A topOfStack() = 0 ;
       virtual void display() = 0 ;
       virtual ~Stack(){}
       virtual A traverse() = 0 ;
       virtual bool endTrav() = 0 ;
       virtual void resetTrav() = 0 ;
  } ;
  #endif
  #ifndef STATICSTACK_H
  #define STATICSTACK_H
  #include < stdlib.h >
  #include "Stack.h"
  #include "StackExceptions.h"
  #include < iostream >
  #include< string >
  using namespace std ;
  template < class T >
  class StaticStack : public Stack< T >
  {
    private:
       int top ;
       int size ;
       T* array ;
       int trav;
    public:
       StaticStack()
       {
         top = -1 ;
         size = 0 ;
         array = NULL ;
         trav = 0 ;
       }
       StaticStack(int s)
       {
         top = -1 ;
         size = s ;
         array = new T[size] ;
         trav = 0 ;
       }
       virtual void makeNewStack(int s)
       {
         delete[] array ;
         top = -1 ;
         trav = 0 ;
         size = s ;
         array = new T[size] ;
       }
       virtual void push(T arg)
       {
         if(isFull())
         {
           throw StackFull("The Stack is Full") ;
         }
         else
         {
           if(top == -1)
           {
             top = top + 1 ;
             array[top] = arg;
             top++;
           }
           else
           {
             array[top] = arg ;
             top++ ;
           }
         }
       }
   virtual T pop()
   {
     if(isEmpty())
     {
       throw StackEmpty("The Stack is Empty") ;
     }
     else
     {
       top-- ;
       if(top == -1)
       {
         throw StackEmpty("The Stack is Empty") ;
       }
       else
       {
       return array[top] ;
       }
     }
   }
   virtual T traverse()
   {
     /* if( trav == top )
     {
       char ch;
       cout<<"The value of Traverse reach at it`s Peak";
       cout<<"May you Want to Reset The Traverse (y/n) : ";
       cin>>ch;
       if(ch=='y')
       resetTrav();*/
     }
     if(endTrav())
     {
       throw CantTrav("Transverse Cant Proceed");
     }
     else
     {
       if( trav > top )
       {
         trav = trav - 2 ;
         if(trav == -1)
           throw CantTrav("Transverse Cant Proceed Because The Stack Is Empty");
         else
         {
           return array[trav++];
         }
       }
       else if(trav == top)
       {
         trav = trav - 2 ;
         if(trav == -1)
           throw CantTrav("Transverse Cant Proceed Because The Stack Is Empty");
         else
         {
           return array[trav];
         }
       }
       else
       {
         if(trav == -1)
           throw CantTrav("Transverse Cant Proceed Because The Stack Is Empty");
         else
         {
           return array[trav++];
         }
       }
     }
   }
   virtual bool endTrav()
   {
     if(top == -1)
     {
       return (trav == top + 1) ;
     }
     else
       return (trav == top) ;
   }
   virtual void resetTrav()
   {
     trav = 0;
   }
   virtual bool isEmpty()
   {
     return (top == -1) ;
   }
   virtual bool isFull()
   {
     return (top == size) ;
   }
   virtual T topOfStack()
   {
     if(isEmpty())
     {
       throw StackEmpty("The Stack is Empty") ;
     }
     else
     {
       return (array[top - 1]) ;
     }
   }
   virtual void display()
   {
     int i = -1;
     cout << endl ;
     for(i = i+1 ; i < top ; i++)
     {
       cout << array[i] << ' ' ;
     }
     cout << endl ;
   }
   virtual ~StaticStack()
   {
     delete[] array ;
     top = 0 ;
     size = 0 ;
     array = NULL ;
   } } ;
  #endif
  #ifndef STACKEXCEPTIONS_H
  #define STACKEXCEPTIONS_H
  #include < exception >
  using namespace std ;
  class StackEmpty : public exception
  {
  public:
     StackEmpty(char* c) : exception(c)
     {}
  } ;
  class StackFull : public exception
  {
    public:
       StackFull(char* c) : exception(c)
       {}
  } ;
  class CantTrav : public exception
  {
    public:
       CantTrav(char* c) : exception(c)
       {}
  };
  #endif
  #include "StaticStack.h"
  //#include "StackExceptions.h"
  #include < iostream >
  using namespace std ;
  void main()
  {
     int size;
     cout<<"Enter the Maximum Size of Stack : ";
     cin>>size;
     Stack < int >* S = new StaticStack< int >(size) ;
     int choice ;
     int data ;
     bool cont = true ;
     while(cont)
     {
       try
       {
         cout<< endl << "Enter 1 push()"
         << endl << "Enter 2 to pop()"
         << endl << "Enter 3 to display()"
         << endl << "Enter 4 to exit"
         << endl << "Enter 5 to transverse"
         << endl << "Enter 6 to reset the transverse" << endl;
         cin >> choice ;
         switch(choice)
         {
           case 1:
             cout << endl << "Enter value to push in stack " ;
             cin >> data ;
             S->push(data) ;
             break ;
           case 2:
             data = S->pop() ;
             cout << "The value returned is : " << data ;
             break ;
           case 3:
             S->display() ;
             break ;
           case 4:
             cont = false ;
             break ;
           case 5:
             data = S->traverse();
             cout<< "The value returned is : "<< data << endl << endl;
             break;
           case 6:
             S->resetTrav();
             break;
             default:
             cout << endl << "wrong value entered, try again" << endl ;
         }// switch ends
       }
       catch(exception e)
       {
         cout << endl << e.what() << endl ;
       }
     } // while loop ends
     delete S ;
  }   

Data Structures Program on Stack using Linked list


  #include< iostream >
  #include< cstdlib >
  #include< malloc.h >
  #include< conio.h >
  using namespace std;
  struct node
  {
     int info;
     struct node *next;
  };
  class stack
  {
     struct node *top;
     public:
       stack();
       void push();
       void pop();
        void display();
  };
  stack::stack()
  {
     top = NULL;
  }
  void stack::push()
  {
     int data;
     struct node *p;
     if((p=(node*)malloc(sizeof(node)))==NULL)
     {
       cout<<"Memory Exhausted";
       exit(0);
     }
     cout<<"Enter a Number to insert:";
     cin>>data;
     p = new node;
     p->info = data;
     p->next = NULL;
     if(top!=NULL)
     {
       p->next = top;
     }
     top = p;
     cout<<"\nNew item inserted"<< endl;
  }
  void stack::pop()
  {
     struct node *temp;
     if(top==NULL)
     {
       cout<<"\nThe stack is Empty"<< endl;
     }
     else
     {
       temp = top;
       top = top->next;
       cout<<"\nThe value popped is "<< temp->info<< endl;
       delete temp;
     }
  }
  void stack::display()
  {
     struct node *p = top;
     if(top==NULL)
     {
       cout<<"\nNothing to Display\n";
     }
     else
     {
       cout<<"\nThe contents of Stack\n";
       while(p!=NULL)
       {
         cout<< p->info<< endl;
         p = p->next;
       }
     }
  }
  int main()
  {
     stack s;
     int choice;
     do
     {
       cout<<"\nEnter your choice:";
       cout<<"\n1. PUSH\n2. POP\n3. DISPLAY\n4. EXIT\n";
       cin>>choice;
       switch(choice)
       {
         case 1:
           s.push();
           break;
         case 2:
           s.pop();
           break;
         case 3:
           s.display();
           break;
         case 4:
           exit(0);
           break;
         default:
           cout<<"Invalid Choice";
           break;
       }
     }while(choice);
     getch();
     return 0;
  }   

Data Structures - Stack Implementation as a Class

        # include< iostream.h>
  # include< process.h>
  # include< conio.h>
  # define SIZE 20
  class stack
  {
    int a[SIZE];
    int tos; // Top of Stack
    public:
       stack();
       void push(int);
       int pop();
       int isempty();
       int isfull();
  };
  stack::stack()
  {
    tos=0; //Initialize Top of Stack
  }
  int stack::isempty()
  {
    return (tos==0?1:0);
  }
  int stack::isfull()
  {
    return (tos==SIZE?1:0);
  }
  void stack::push(int i)
  {
    if(!isfull())
    {
      a[tos]=i;
      tos++;
    }
    else
    {
       cerr<<"Stack overflow error !
      Possible Data Loss !";
    }
  }
  int stack::pop()
  {
    if(!isempty())
    {
      return(a[--tos]);
    }
    else
    {
      cerr<<"Stack is empty! What to pop...!";
    }
    return 0;
  }
  void main()
  {
    stack s;
    int ch=1,num;
    while(ch!=0)
    {
       cout<<"Stack Operations Mani Menu
      1.Push
      2.Pop
      3.IsEmpty
      4.IsFull
      0.Exit
      ";
       cin>>ch;
       switch(ch)
       {
         case 0:
           exit(1); //Normal Termination of Program
         case 1:
           cout<<"Enter the number to push";
           cin>>num;
           s.push(num);
           break;
         case 2:
           cout<<"Number popped from the stack is: "<< s.pop()<< endl;
           break;
         case 3:
           (s.isempty())?(cout<<"Stack is empty."):(cout<<"Stack is not empty.");
           break;
         case 4:
           (s.isfull())?(cout<<"Stack is full."):(cout<<"Stack is not full.");
           break;
         default:
           cout<<"Illegal Option.Please try again";
       }
    }//end of while
    getch();
  }   

Data Structures Program on Sorted Doubly Linked List with Insertion and Deletion

     #include < iostream >
  #include < cstdlib >
  #include < string >
  using namespace std;
  class Dllist
  {
     private:
       typedef struct Node
       {
         string name;
         Node* next;
         Node* prev;
       };
       Node* head;
       Node* last;
     public:
       Dllist()
       {
         head = NULL;
         last = NULL;
       }
       bool empty() const { return head==NULL; }
       friend ostream& operator <<(ostream& ,const Dllist& );
       void Insert(const string& );
       void Remove(const string& );
  };
  void Dllist::Insert(const string& s)
  {
     // Insertion into an Empty List.
     if(empty())
     {
       Node* temp = new Node;
       head = temp;
       last = temp;
       temp->prev = NULL;
       temp->next = NULL;
       temp->name = s;
     }
     else
     {
       Node* curr;
       curr = head;
       while( s>curr->name && curr->next != last->next) curr = curr->next;
    
       if(curr == head)
       {
         Node* temp = new Node;
         temp->name = s;
         temp->prev = curr;
         temp->next = NULL;
         head->next = temp;
         last = temp;
         // cout<<" Inserted "<< s <<" After "<< curr->name << endl;
       }
       else
       {
         if(curr == last && s>last->name)
         {
           last->next = new Node;
           (last->next)->prev = last;
           last = last->next;
           last->next = NULL;
           last->name = s;
           // cout<<" Added "<< s <<" at the end "<< endl;
         }
         else
         {
           Node* temp = new Node;
           temp->name = s;
           temp->next = curr;
           (curr->prev)->next = temp;
           temp->prev = curr->prev;
           curr->prev = temp;
           // cout<<" Inserted "<< s <<" Before "<< curr->name<< endl;
         }
       }
     }
  }
  ostream& operator<<(ostream& ostr, const Dllist& dl )
  {
     if(dl.empty()) ostr << " The list is empty. "<< endl;
     else
     {
       Dllist::Node* curr;
       for(curr = dl.head; curr != dl.last->next; curr=curr->next)
         ostr << curr->name <<" ";
       ostr << endl;
       ostr << endl;
       return ostr;
     }
  }
  void Dllist::Remove(const string& s)
  {
     bool found = false;
     if(empty())
     {
       cout<<" This is an empty list! "<< endl;
       return;
     }
     else
     {
       Node* curr;
       for(curr = head; curr != last->next; curr = curr->next)
       {
         if(curr->name == s)
         {
           found = true;
           break;
         }
       }
       if(found == false)
       {
         cout<<" The list does not contain specified Node"<         return;
       }
       else
       {
         // Curr points to the node to be removed.
         if (curr == head && found)
         {
           if(curr->next != NULL)
           {
             head = curr->next;
             delete curr;
             return;
           }
           else
           {
             delete curr;
             head = NULL;
             last = NULL;
             return;
           }
         }
         if (curr == last && found)
         {
           last = curr->prev;
           delete curr;
           return;
         }
         (curr->prev)->next = curr->next;
         (curr->next)->prev = curr->prev;
         delete curr;
       }
     }
  }
  int main()
  {
     Dllist d1;
     int ch;
     string temp;
     while(1)
     {
       cout<< endl;
       cout<<" Doubly Linked List Operations "<< endl;
       cout<<" ------------------------------"<< endl;
       cout<<" 1. Insertion "<< endl;
       cout<<" 2. Deletion "<< endl;
       cout<<" 3. Display "<< endl;
       cout<<" 4. Exit "<< endl;
       cout<<" Enter your choice : ";
       cin>>ch;
       switch(ch)
       {
         case 1: cout<<" Enter Name to be inserted : ";
           cin>>temp;
           d1.Insert(temp);
           break;
         case 2: cout<<" Enter Name to be deleted : ";
           cin>>temp;
           d1.Remove(temp);
           break;
         case 3: cout<<" The List contains : ";
           cout<< d1;
           break;
         case 4: system("pause");
           return 0;
           break;
       }
     }
  }    

Data Structures Program on Single Linked List

       #include< iostream.h >
  #include< conio.h >
  #include< stdlib.h >
  class list
  {
     struct node
     {
       int data;
       node *link;
     }*p;
    public:
       void inslast(int);
       void insbeg(int);
       void insnext(int,int);
       void delelement(int);
       void delbeg();
       void dellast();
       void disp();
       int seek(int);
       list(){p=NULL;}
       ~list();
  };
  void list::inslast(int x)
  {
     node *q,*t;
     if(p==NULL)
     {
       p=new node;
       p->data=x;
       p->link=NULL;
     }
     else
     {
       q=p;
       while(q->link!=NULL)
       q=q->link;
       t=new node;
       t->data=x;
       t->link=NULL;
       q->link=t;
     }
     cout<<"Inserted successfully at the end..";
     disp();
  }
  void list:: insbeg(int x)
  {
     node *q;
     q=p;
     p=new node;
     p->data=x;
     p->link=q;
     cout<<"Inserted successfully at the begining..";
     disp();
  }
  void list::delelement(int x)
  {
     node *q,*r;
     q=p;
     if(q->data==x)
     {
       p=q->link;
       delete q;
       return;
     }
     r=q;
     while(q!=NULL)
     {
       if(q->data==x)
       {
         r->link=q->link;
         delete q;
         return;
       }
       r=q;
       q=q->link;
     }
     cout<<"Element u entered "<< x <<" is not found..";
  }
  void list:: delbeg()
  {
     cout<<"The list before deletion:";
     disp();
     node *q;
     q=p;
     if(q==NULL)
     {
       cout<<"No data is present..";
       return;
     }
     p=q->link;
     delete q;
     return;
  }
  void list:: dellast()
  {
     cout<<"The list before deletion:";
     disp();
     node *q,*t;
     q=p;
     if(q==NULL)
     {
       cout<<"There is no data in the list..";
       return;
     }
     if(q->link==NULL)
     {
       p=q->link;
       delete q;
       return;
     }
     while(q->link->link!=NULL)
       q=q->link;
     q->link=NULL;
     return;
  }
  list::~list()
  {
     node *q;
     if(p==NULL) return;
     while(p!=NULL)
     {
       q=p->link;
       delete p;
       p=q;
     }
  }
  void list::disp()
  {
     node *q;
     q=p;
     if(q==NULL)
     {
       cout<<" No data is in the list..";
       return;
     }
     cout<<"The items present in the list are :";
     while(q!=NULL)
     {
       cout<<" "<< q->data;
       q=q->link;
     }
  }
  void list :: insnext(int value,int position)
  {
     node *temp,*temp1;
     temp=p;
     if(temp1==NULL)
     {
       temp1= new node;
       temp1->data=value;
       temp1->link=NULL;
       p=temp1;
       return;
     }
     for(int i=0;((i< position) && (temp->link!=NULL)) ;i++)
     {
       if(i==(position-1))
       {
         temp1= new node;
         temp1->data= value;
         temp1->link=temp->link;
         temp->link=temp1;
       }
       temp=temp->link;
     }
     //cout<<"Inserted successfully at the position.."<< position;
     disp();
  }
  int list::seek(int value)
  {
     node *temp;
     temp=p;
     int position=0;
     while(temp!=NULL)
     {
       if(temp->data==value)
         return position+1;
       else
       {
         temp=temp->link;
         position=position+1;
       }
     }
     cout<<"Element "<< value <<" not found";
     return 0;
  }
  void main()
  {
    list l;
    int ch,v,p,ps;
    do
    {
       clrscr();
       cout<<"Operations on List..";
       cout<<"
      1.Insertion
      2.Deletion
      3.Display
      4.Seek
      5.Exit";
       cout<<" Enter ur choice:";
       cin>>ch;
       switch(ch)
       {
         case 1:
           cout<<"1.Insertion at begining
           2.Insertion at the end";
           cout<<"3.Insertion after the mentioned position";
           cout<<"Enter ur choice:";
           cin>>ps;
           cout<<" Enter the value to insert:";
           cin>>v;
           switch(ps)
           {
             case 1:
               l.insbeg(v);
               break;
             case 2:
               l.inslast(v);
               break;
             case 3:
               cout << "Enter the position to insert the value:";
               cin>>p;
               l.insnext(v,p);
               break;
  
             default:
               cout<<"The choice is invalid";
               return;
           }
           break;
           case 2:
             cout<<"1.Delete the first element
             2.Delete the last element";
             cout<<"3.Enter the element to delete from the list";
             cout<<"Enter ur choice:";
             cin>>ps;
             switch(ps)
             {
               case 1:
                 l.delbeg();
                 cout<<"The list after deletion:";
                 l.disp();
                 break;
               case 2:
                 l.dellast();
                 cout<<"The list after deletion:";
                 l.disp();
                 break;
               case 3:
                 l.disp();
                 cout<<"Enter the element to delete : ";
                 cin>>v;
                 l.delelement(v);
                 cout<<"The list after deletion:";
                 l.disp();
                 break;
               default:
                 cout<<"The option is invalid...";
                 break;
             }
             break;
             case 3:
               l.disp();
               break;
             case 4:
               l.disp();
               cout<<"Enter the element to search:";
               cin>>v;
               cout<<" The position of the element "<< v <<" is "<< l.seek(v);
               getch();
               break;
             case 5:
               exit(1);
             default:
               cout<<"The option is invalid...";
               return;
       }
       getch();
    }while(ch!=5);
    getch();
    return;
  }   

Data Structures Program on Shell Sort

       #include< iostream.h >
   #include< constream.h >
   void read(int a[10],int n)
   {
     cout << "reading\n";
     for(int i=0;i < n;i++)
       cin >> a[i];
   }
   void display(int a[10],int n)
   {
     for(int i=0;i < n;i++)
       cout << a[i] <<"\t";
   }
   void shellsort(int a[10],int n)
   {
     int gap=n/2;
     do
     {
       int swap;
       do
       {
         swap=0;
         for(int i=0;i < n-gap;i++)
         if(a[i] > a[i+gap])
         {
           int t=a[i];
           a[i]=a[i+gap];
           a[i+gap]=t;
           swap=1;
         }
       }
       while(swap);
     }
     while(gap=gap/2);
   }
   void main()
   {
     int a[10];
     int n;
     clrscr();
     cout<<"enter n\n";
     cin>>n;
     read(a,n);
     cout<<"before sorting\n";
     display(a,n);
     shellsort(a,n);
     cout<<"\nafter sorting\n";
     display(a,n);
     getch();
   }