Docstoc

C plus plus Data Structure

Document Sample
C plus plus Data Structure Powered By Docstoc
					Master in C plus plus Data Structure (practical Track)
Author : nikin sk

Blog : nikisurf.blogspot.com

Web : nikin.net.tc

Sample Practical -1

#include <iostream>

#include <cstdlib>

#include <ctime>

using namespace std;



int count=0;



void subSum( const int A[], int n )

{

        count += 1;

        for(int i = 0; i < n; i++)

        {

                 count += 2;

                 for(int j = 1, sum = A[0]; j < i; j++)

                 {

                           sum+=A[j];

                           count += 2;

                 }

                 //cout<<"The sum for sub array A[0] -> A["<<i<<"] is "<<sum<<endl;

        }

}
int main()

{



        int size, times;



        srand( time(0) );



        cout << "What is the maximum size of your array? ";

        cin >> size;




        cout << "\nInitializng the array with random numbers....";



        int* array = new int[size];



        for(int i=0; i<size; i++)

                 array[i] = (rand() % 1000) + 1;



        cout << "\n\nHow many times do you want to run the function? ";

        cin >> times;



        cout << "\n\nn\t\t\tCounter\n"

                 << "===\t\t\t=======\n";



        for (int h=1; h<=times; h++)

        {

                 subSum( array , 100 * h );
                     cout << (100 * h) << "\t\t\t" << count << "\n";

                     count = 0;

          }



          delete [] array;



          return 0;

}

````````````````````````````````````````````````````````````````````

Sample Practical -2




Linked List Operation

#include <iostream>

using namespace std;



struct Node

{

          int data;

          Node* link;



          Node( int x )

          {

                     data = x;

                     link = NULL;

          }
           Node( )

           {

                     data = 0;

                     link = NULL;

           }

};



class LinkList

{

private:

           Node *head, *tail, *trav;

           int count;



public:

           LinkList()

           {

                     head = tail = trav = NULL;

                     count = 0;

           }



           void insert (int x)

           {

                     Node *temp = new Node( x );



                     if(!head)

                             head = tail = temp;

                     else
       {

                  tail->link = temp;

                  tail = temp;

       }




       count++;

}



bool remove( int &x )

{

       Node *temp = new Node();



       if(!head)

                  return false;



       x = head->data;

       temp = head;

       head = head->link;



       delete temp;

       if (!head)

                  tail = NULL;



       count--;



       return true;
}



bool findX(int x)

{

        for(trav = head; trav; trav=trav->link)

                   if(trav->data == x)

                           return true;



        return false;

}



// begining // middle // ending

bool delX(int x)

{

        int r=0;

        Node *tmp = new Node();



        if(!head)

                   return false;



        if(head->data == x)

                   return remove(r);



        for(trav=head, tmp=head->link; tmp; trav=tmp, tmp=tmp->link)

                   if(tmp->data == x)

                   {

                           trav->link = tmp-> link;
                         if(tmp == tail)

                                  tail = trav;



                         delete tmp;

                         return true;

                 }



        return false;

}



void printLinkList()

{

        trav = head;



        while(trav != NULL)

        {

                 cout << trav->data << " ";

                 trav = trav->link;

        }



}



int getCount()

{

        return count;

}
        ~LinkList()

        {

                int k=0;

                for(int i=0; i<count; i++)

                           remove(k);

        }




};




int main()

{

        int option1, option2, temp=0;

        LinkList k;



        do

        {

                cout << "What do you want to do? \n"

                           << "\t1. Insert a value \n"

                           << "\t2. Remove the first values \n"

                           << "\t3. Search \n"

                           << "\t4. Delete a value \n"

                           << "\t5. Print the linked list \n"

                           << "To Exit enter 6 \n"

                           << "Your choice: ";
        cin >> option1;



        cout << "\n";



        switch(option1)

        {

        case 1:

                  cout << "Enter a list of integers (-1 to stop): ";

                  cin >> option2;



                  while(option2 != -1)

                  {

                           k.insert(option2);

                           cin >> option2;

                  }

                  break;



        case 2:

                  if(k.remove(temp))

                           cout << "The result of removing the first element you get " <<
temp;

                  else

                           cout << "The List is already Empty";



                  break;



        case 3:
          cout << "Enter an integer to search the list: ";

          cin >> option2;



          if(k.findX(option2))

                   cout << "Element " << option2 << " was found";

          else

                   cout << "Element " << option2 << " was NOT found";

          break;



case 4:

          cout << "Enter an integer to delete from the list: ";

          cin >> option2;



          if(k.delX(option2))

                   cout << "Element " << option2 << " was deleted";

          else

                   cout << "Element " << option2 << " was NOT found";

          break;



case 5:

          cout << "Printing... \n\n"

                   << "The List is: ";



                   k.printLinkList();



          cout << "\nThe number of nodes is: " << k.getCount();

          break;
                     }



                     cout << "\n\n";



          } while (option1 != 6);



          cout << "Exiting ...\n\n";



          return 0;

}

`````````````````````````````````````````````````````````````````````



Part -2

#include <iostream.h>



struct Node

{

          int data;

          Node * link;



          Node (int x)

          {

                     data = x;

                     link = NULL;

          }



};
class Stack

{

private:

           Node *top, *tmp;



public:

           Stack()

           {

                     top = tmp = NULL;

           }



           bool isEmpty()

           {

                     if(!top)

                                return true;



                     return false;

           }



           void push(int x)

           {

                     tmp = new Node(x);



                     if(isEmpty())

                                top = tmp;

                     else
        {

                   tmp->link = top;

                   top = tmp;

        }

}



bool pop(int &x)

{

        if(isEmpty())

                   return false;

        else

        {

                   x = top->data;

                   tmp = top;

                   top = top->link;

                   delete tmp;

                   return true;

        }

}



void printStack()

{

        Stack t;

        int k;



        while(pop(k))

        {
                   t.push(k);

                   cout << k << " ";

        }



        while(t.pop(k))

                   push(k);

}



bool findX(int x)

{

        Stack t;

        int k;

        bool found = false;



        while(pop(k))

        {

                   t.push(k);

                   if(x == k)

                            found = true;

        }



        while(t.pop(k))

                   push(k);



        return found;

}
bool delX(int x)

{

        Stack t;

        int k;

        bool del = false;



        while(pop(k))

        {

                   if(x != k)

                            t.push(k);

                   else

                            del = true;

        }



        while(t.pop(k))

                   push(k);



        return del;

}



~Stack()

{

        int k;

        while(pop(k)) { }

}
};




int main()

{



        Stack s;

        int op, num, k;



        cout << "What do you want to do? \n"

                   << "\t1. Push values \n"

                   << "\t2. Pop \n"

                   << "\t3. Search \n"

                   << "\t4. Delete \n"

                   << "\t5. Print the stack \n"

                   << "To Exit enter 6 \n"

                   << "Your choice: ";



        cin >> op;



        while (op != 6)

        {



                   cout << endl;



                   switch(op)

                   {
case 1:

          cout << "Enter a list of integers (-1 to stop): ";

          cin >> num;

          while (num != -1)

          {

                   s.push(num);

                   cin >> num;

          }

          break;



case 2:

          if(s.pop(k))

                   cout << "The pop returned: " << k << endl;

          else

                   cout << "The stack is empty \n";

          break;



case 3:

          cout << "Enter an integer to search the stack: ";

          cin >> num;



          if(s.findX(num))

                   cout << "Element " << num << " was found...\n";

          else

                   cout << "Element " << num << " was NOT found...\n";

          break;
case 4:

          cout << "Enter an integer to delete from the stack: ";

          cin >> num;



          if(s.delX(num))

                   cout << "Element " << num << " was deleted...\n";

          else

                   cout << "Element " << num << " was NOT found...\n";

          break;



case 5:

          cout << "Printing...\n"

                   << "The stack contains: \n";

          s.printStack();

          cout << endl;

          break;



}




cout << "\nWhat do you want to do? \n"

          << "\t1. Push values \n"

          << "\t2. Pop \n"

          << "\t3. Search \n"

          << "\t4. Delete \n"
                                 << "\t5. Print the stack \n"

                                 << "To Exit enter 6 \n"

                                 << "Your choice: ";



                     cin >> op;

          }



          cout << "\nExiting... \n\n";



          return 0;

}

```````````````````````````````````````````````````````````````````````

Another Example



#include <iostream>

using namespace std;



struct Node

{

          int data;

          Node *link;



          Node( int x )

          {

                     data = x;

                     link = NULL;

          }
};



class Queue

{

       private:

                  Node *head , *tail;

                  int count;



       public:

                  Queue()

                  {

                          head = tail = NULL;

                          count = 0;

                  }



                  bool isEmpty()

                  {

                          if(!head)

                                   return true;



                          return false;

                  }



                  void enque( int x )

                  {

                          Node *temp = new Node( x );
        if(isEmpty())

                   head = tail = temp;

        else

        {

                   tail -> link = temp;

                   tail = temp;

        }



        count++;

}



bool deque( int &x)

{

        if(isEmpty())

                   return false;



        x = head -> data;

        Node *tmp = head;



        head = head -> link;

        delete tmp;



        count--;

        return true;

}



bool findX(int y)
{

        Queue q;

        int x;

        bool found = false;



        while(deque(x))

        {

                   if(x == y)

                            found = true;



                   q.enque( x );

        }



        while(q.deque(x))

                   enque( x );



        return found;

}



bool delX(int y)

{

        Queue q;

        int x;

        bool del = false;



        while(deque(x))

        {
                 if(x == y)

                          del = true;

                 else

                          q.enque( x );

        }



        while(q.deque(x))

                 enque( x );



        return del;

}



void print()

{



        Queue q;

        int x;



        while(deque(x))

        {

                 cout << x << " ";

                 q.enque( x );

        }



        while(q.deque(x))

                 enque( x );
               }



               int getCount()

               {

                       return count;

               }



               ~Queue()

               {

                       int x;



                       while(deque(x))

                       {



                       }



                       tail = NULL;

               }




};




int main()

{

        int option1, option2, temp=0;
Queue k;



do

{

      cout << "What do you want to do? \n"

                << "\t1. Insert a value \n"

                << "\t2. Remove the first values \n"

                << "\t3. Search \n"

                << "\t4. Delete a value \n"

                << "\t5. Print the Queue \n"

                << "To Exit enter 6 \n"

                << "Your choice: ";



      cin >> option1;



      cout << "\n";



      switch(option1)

      {

      case 1:

                cout << "Enter a list of integers (-1 to stop): ";

                cin >> option2;



                while(option2 != -1)

                {

                        k.enque(option2);

                        cin >> option2;
                  }

                  break;



        case 2:

                  if(k.deque(temp))

                           cout << "The result of removing the first element you get " <<
temp;

                  else

                           cout << "The List is already Empty";



                  break;



        case 3:

                  cout << "Enter an integer to search the list: ";

                  cin >> option2;



                  if(k.findX(option2))

                           cout << "Element " << option2 << " was found";

                  else

                           cout << "Element " << option2 << " was NOT found";

                  break;



        case 4:

                  cout << "Enter an integer to delete from the list: ";

                  cin >> option2;



                  if(k.delX(option2))

                           cout << "Element " << option2 << " was deleted";
                                else

                                           cout << "Element " << option2 << " was NOT found";

                                break;



                     case 5:

                                cout << "Printing... \n\n"

                                            << "The List is: ";



                                            k.print();



                                cout << "\nThe number of nodes is: " << k.getCount();

                                break;

                     }



                     cout << "\n\n";



          } while (option1 != 6);



          cout << "Exiting ...\n\n";



          return 0;

}

``````````````````````````````````````````````````````````````````````

The heap



#include <iostream>

#include <string>
#include <cmath>

using namespace std;



class Heap

{

       private:



                  struct Node

                  {

                         int pri;

                         string task;



                         Node()

                         {



                         }



                         Node(int mypri, string mytask)

                         {

                                    pri = mypri;

                                    task = mytask;

                         }



                         void operator = (Node& RHS)

                         {

                                    pri = RHS.pri;

                                    task = RHS.task;
        }

};



Node * heap;



int count;

int HEAP_SIZE;



void reheapUp (int indexNew)

{



        if(indexNew < 0)

                 return;



        int parent = (indexNew - 1) / 2;




        if(heap[indexNew].pri < heap[parent].pri)

        {

                 Node temp = heap[parent];

                 heap[parent] = heap[indexNew];

                 heap[indexNew] = temp;

                 reheapUp( parent );

        }

}



void reheapDown (int root)
          {



              int LC = 2 * root + 1,

                         RC = 2 * root + 2;



              int min;



              if(LC < count)

              {

                         min = LC;



                         if(RC < count)

                                 if(heap[RC].pri < heap[LC].pri)

                                          min = RC;



                         if(heap[root].pri > heap[min].pri)

                         {

                                 Node temp = heap[min];

                                 heap[min] = heap[root];

                                 heap[root] = temp;

                                 reheapDown( min );

                         }

              }

          }



public:
Heap( int numNodes)

{

        HEAP_SIZE = numNodes;

        heap = new Node[HEAP_SIZE];

        count = 0;

}



bool insertHeap( int data, string t)

{

        if(count == HEAP_SIZE)

                   return false;



        heap[count].pri = data;

        heap[count].task = t;

        reheapUp( count );

        count++;

        return true;

}



bool deleteHeap( int &dataOut, string &tOut)

{

        if(count == 0)

                   return false;



        dataOut = heap[0].pri;

        tOut = heap[0].task;

        count--;
                          heap[0] = heap[count];

                          reheapDown( 0 );

                          return true;

                   }



                   void printHeap()

                   {

                          for(int i=0; i<count; i++)

                                  cout << heap[i].pri << " \t " << heap[i].task << endl;



                          cout << endl;

                   }



                   ~Heap()

                   {

                          delete [] heap;

                   }



};




int main()

{

        int pri;

        string task;

        Heap h(15);
          cout << "Insert task priority and task name into the Heap (-1 to stop): \n";

          cin >> pri;



          while(pri != -1)

          {

                     cin >> task;

                     h.insertHeap( pri , task );

                     cin >> pri;

          }



          cout << "\n\nThe default size of the heap is 15. \n"

                      << "Printing the contents of the heap: \n";



          h.printHeap();



          cout << "\n\nThe order of the taske when deleted from the heap: \n";



          while( h.deleteHeap( pri , task ))

                     cout << pri << " \t " << task << "\n";



          return 0;

}

```````````````````````````````````````````````````````````````

Effective code




#include <iostream>
using namespace std;



class BST

{

       struct Node

       {

                  int data;

                  Node *left , *right;



                  Node(int x=0)

                  {

                          data = x;

                          left = right = NULL;

                  }

       };



       private:

                  Node *root;

                  int count;



                  void insert( Node *&p , int x )

                  {

                          if(!p)

                                   p = new Node( x );

                          else

                          {

                                   if(x < p->data)
                         insert( p->left , x );



                 else if(x > p->data)

                         insert( p->right , x);

        }

}



bool search(Node *p , int key)

{

        if(!p)

                 return false;



        if(key == p->data)

                 return true;

        else if(key < p->data)

                 return search( p->left , key);

        else

                 return search( p->right , key);

}



bool Delete( Node *&p , int x )

{



        Node *hold;



        if(!p)

                 return false;
else if (x < p->data)

         return Delete (p->left , x );

else if (x > p->data)

         return Delete (p->right , x );



if(!p->left)

{

         hold = p;

         p = p->right;

         delete hold;

         return true;

}

else if(!p->right)

{

         hold = p;

         p = p->left;

         delete hold;

         return true;

}

else

{

         Node *w = p->left;



         while( w->right )

                 w = w->right;



         p->data = w->data;
               return Delete( p->left , w->data );

       }

}




void preOrder( Node *p )

{

       if(p)

       {

               cout << p->data << " ";

               preOrder( p->left);

               preOrder( p->right);

       }

}



void inOrder( Node *p )

{

       if(p)

       {

               inOrder( p->left);

               cout << p->data << " ";

               inOrder( p->right);

       }

}



void postOrder( Node *p )
          {

                  if(p)

                  {

                           postOrder( p->left);

                           postOrder( p->right);

                           cout << p->data << " ";

                  }

          }




public:

          BST()

          {

                  root = NULL;

                  count = 0;

          }



          void insert(int x)

          {

                  insert( root , x );

          }



          bool Delete(int key)

          {

                  return Delete( root , key );

          }
     bool search(int key)

     {

              return search( root , key );

     }



     void preOrder( )

     {

              preOrder( root );

     }



     void inOrder( )

     {

              inOrder( root );

     }



     void postOrder( )

     {

              postOrder( root );

     }



     ~BST()

     {

              while(root)

                       Delete( root-> data);

     }



};
int main()

{

        BST t;

        int num;



        cout << "Enter a sequence of integers until 0: \n";

        cin >> num;



        while( num != 0)

        {

                 t.insert(num);

                 cin >> num;

        }



        cout << "\nThe preorder traversal: \n";

        t.preOrder();



        cout << "\n\nThe inorder traversal: \n";

        t.inOrder();



        cout << "\n\nThe postorder traversal: \n";

        t.postOrder();




        cout << "\n\nEnter a Number to search for (0 to stop): ";

        cin >> num;
while(num != 0)

{

       if(t.search(num))

                  cout << "Number " << num << " was found";

       else

                  cout << "Number " << num << " was NOT found";



       cout << "\n\nEnter a Number to search for (0 to stop): ";

       cin >> num;

}



cout << "\n\nEnter a Number to delete (0 to stop): ";

cin >> num;



while(num != 0)

{

       if(t.Delete(num))

                  cout << "Number " << num << " was deleted";

       else

                  cout << "Number " << num << " was NOT found";



       cout << "\n\nEnter a Number to delete (0 to stop): ";

       cin >> num;

}



cout << "\n\n";
          return 0;

}



`````````````````````````````````````````````````````````

#include <iostream>

using namespace std;



class BST

{

          struct Node

          {

                     int data;

                     Node *left , *right;



                     Node(int x=0)

                     {

                                data = x;

                                left = right = NULL;

                     }

          };



          private:

                     Node *root;

                     int count;
void insert( Node *&p , int x )

{

        if(!p)

                 p = new Node( x );

        else

        {

                 if(x < p->data)

                         insert( p->left , x );



                 else if(x > p->data)

                         insert( p->right , x);

        }

}



bool search(Node *p , int key)

{

        if(!p)

                 return false;



        if(key == p->data)

                 return true;

        else if(key < p->data)

                 return search( p->left , key);

        else

                 return search( p->right , key);

}
bool Delete( Node *&p , int x )

{



        Node *hold;



        if(!p)

                 return false;

        else if (x < p->data)

                 return Delete (p->left , x );

        else if (x > p->data)

                 return Delete (p->right , x );



        if(!p->left)

        {

                 hold = p;

                 p = p->right;

                 delete hold;

                 return true;

        }

        else if(!p->right)

        {

                 hold = p;

                 p = p->left;

                 delete hold;

                 return true;

        }

        else
       {

                Node *w = p->left;



                while( w->right )

                          w = w->right;



                p->data = w->data;



                return Delete( p->left , w->data );

       }

}



void rotateRight( Node *&p, int k )

{

       if(!p)

       {

                cout << "Error: cannot perform a right rotation!\n\n";

                return;

       }



       if(k < p->data)

       {

                rotateRight( p->left, k );

                return;

       }

       else if(k > p->data)

       {
                 rotateRight( p->right, k );

                 return;

        }



        if(p->left)

        {

                 Node * temp = p->left;



                 p->left = temp->right;

                 temp->right = p;

                 p = temp;

        }

        else

                 cout << "Error: cannot perform a right rotation!\n\n";




}



void rotateLeft( Node *&p , int k )

{

        if(!p)

        {

                 cout << "Error (1): cannot perform a left rotation!\n\n";

                 return;

        }
       if(k < p->data)

       {

               rotateLeft( p->left, k );

               return;

       }

       else if(k > p->data)

       {

               rotateLeft( p->right, k );

               return;

       }



       if(p->right)

       {

               Node * temp = p->right;



               p->right = temp->left;

               temp->left = p;

               p = temp;

       }

       else

               cout << "Error (2): cannot perform a left rotation!\n\n";



}



void preOrder( Node *p )

{

       if(p)
       {

               cout << p->data << " ";

               preOrder( p->left);

               preOrder( p->right);

       }

}



void inOrder( Node *p )

{

       if(p)

       {

               inOrder( p->left);

               cout << p->data << " ";

               inOrder( p->right);

       }

}



void postOrder( Node *p )

{

       if(p)

       {

               postOrder( p->left);

               postOrder( p->right);

               cout << p->data << " ";

       }

}
public:

          BST()

          {

                  root = NULL;

                  count = 0;

          }



          void insert(int x)

          {

                  insert( root , x );

          }



          bool Delete(int key)

          {

                  return Delete( root , key );

          }



          bool search(int key)

          {

                  return search( root , key );

          }



          void rotateRight( int k )

          {

                  rotateRight( root , k );

          }
void rotateLeft( int k )

{

         rotateLeft( root , k );

}



void preOrder( )

{

         preOrder( root );

}



void inOrder( )

{

         inOrder( root );

}



void postOrder( )

{

         postOrder( root );

}



~BST()

{

         while(root)

                  Delete( root-> data);

}
};



int main()

{

        BST t;

        int num;



        cout << "Enter a sequence of integers until 0: \n";

        cin >> num;



        while( num != 0)

        {

                 t.insert(num);

                 cin >> num;

        }



        cout << "\n\nThe preorder traversal: \n";

        t.preOrder();



        cout << "\n\nThe inorder traversal: \n";

        t.inOrder();



        cout << "\n\nThe postorder traversal: \n";

        t.postOrder();




        while (true)
{

    cout << "\n\nEnter the number to rotate right: ";

    cin >> num;

    t.rotateRight( num );



    cout << "\n\nThe preorder traversal: \n";

    t.preOrder();



    cout << "\n\nThe inorder traversal: \n";

    t.inOrder();



    cout << "\n\nThe postorder traversal: \n";

    t.postOrder();




    cout << "\n\nEnter the number to rotate left: ";

    cin >> num;

    t.rotateLeft( num );



    cout << "\n\nThe preorder traversal: \n";

    t.preOrder();



    cout << "\n\nThe inorder traversal: \n";

    t.inOrder();



    cout << "\n\nThe postorder traversal: \n";
       t.postOrder();



       cout << "\n\n";

}




/*

cout << "\n\nEnter a Number to search for (0 to stop): ";

cin >> num;



while(num != 0)

{

       if(t.search(num))

               cout << "Number " << num << " was found";

       else

               cout << "Number " << num << " was NOT found";



       cout << "\n\nEnter a Number to search for (0 to stop): ";

       cin >> num;

}



cout << "\n\nEnter a Number to delete (0 to stop): ";

cin >> num;



while(num != 0)

{
                if(t.Delete(num))

                        cout << "Number " << num << " was deleted";

                else

                        cout << "Number " << num << " was NOT found";



                cout << "\n\nEnter a Number to delete (0 to stop): ";

                cin >> num;

      }



      cout << "\n\n";

      */



      return 0;

}




Happy coding…

				
DOCUMENT INFO
Shared By:
Stats:
views:19
posted:5/25/2012
language:English
pages:54
Description: C plus plus complete Data structure Practicals