Posts

Showing posts from February, 2014

Quick Sort

Image
#include<iostream> #include<conio.h> using namespace std; void quickSort(int arr[], int left, int right) {   int l = left, r = right;   int a;   int pivot = arr[((left + right) / 2)];   while (l <= r) {         while (arr[l] < pivot)               l++;         while (arr[r] > pivot)               r--;         if (l<=r) {               a = arr[l];               arr[l] = arr[r];               arr[r] = a;               l++;               r--;     } } if (left<r)     quickSort(arr, left, r); if (l< right)         quickSort(arr, l, right); } int main() {     int arr[10];     cout<<"Enter oderable data: ";     for(int i=0;i<10;i++)     cin>>arr[i];     for(int i=0;i<10;i++)     quickSort(arr,0,9);     cout<<"Data after Quick Sorting: \n\t| "; for(int i=0;i<10;i++)     cout<<arr[i]<<" | " ; getch(); return 0; }

Binary Search

Image
#include<iostream> #include<conio.h> using namespace std; int main() { int search[50],num,left,right,middle,item; cout<<"Enter number of elements : "; cin>>num; cout<<"\nEnter Elements in SORTED ORDER: "; for(int i=1;i<=num;i++) { cin>>search[i]; cout<<"\t\t\t\t"; } cout<<"\nEnter Item you want to search : "; cin>>item; left=1; right=num; if(item<search[left] || item>search[right]) { cout<<"Item not found."; return 0; } middle=(left+right)/2;                     while(left<=right && search[middle]!=item)   { if(search[middle]<item) left=middle+1; else right=middle-1; middle=(left+right)/2; } if(search[middle]==item) { cout<<"\n"<<item<<" is Found at location : "<<middle; } else { cout<<"Item is not found"

Queue Using Linked List

Image
Queue :-  In  programming , a queue is a  data structure  in which elements are removed in the same order they were entered. This is often referred to as FIFO (first in, first out). Program #include <iostream> #include <conio.h> #define MAX 10 using namespace std; struct queue     {      int data;      queue *prev, *next;     }*ctr, *temp, *start=NULL; void push(); void show(); void pop(); int  main() { int count=0;       while(1)     {                   cout <<"MAIN MENU\n" ;                 cout <<"\tPress 1 for PUSH \n" ;                 cout <<"\tPress 2 for SHOW \n" ;                 cout <<"\tPress 3 for POP \n" ;                 cout <<"\tPress 0 for exit\n\nAns. " ;                 char ch;                 cin>>ch;                 switch(ch)                 {                      case '1':                       count++;                  

Stack Using Linked List

Image
#include <iostream> #include <conio.h> #define MAX 10 using namespace std; struct stack     {      int data;      stack *prev, *next;     }*ctr, *temp, *start=NULL; void push(); void show(); void pop(); int  main() { int count=0;         while(1)     {                     cout <<"MAIN MENU\n" ;                 cout <<"\tPress 1 for PUSH \n" ;                 cout <<"\tPress 2 for SHOW \n" ;                 cout <<"\tPress 3 for POP \n" ;                 cout <<"\tPress 0 for exit\n\nAns. " ;                 char ch;                 cin>>ch;                 switch(ch)                 {                      case '1':                       count++;                               if(count<=MAX)                                                 push();                                            else                                            cout<&

Static Routing

Image
Step1. Assign IP's to all PC's and Routers.  Step2. Click on Router0 and insert these values. Step3. Click on Router1 and insert these values. Step4. Click serially.                                                                                                                             SmartPorgramOn

QUEUE USING ARRAY ....

Image
#include<iostream> #include<conio.h> #define MAX 10 using namespace std; void insert(); void delet(); void display(); int queue[MAX], rear=-1, front=-1, item; int main() { int ch; do  {   cout<<"Press\n1. Insert\n2. Delete\n3. Display\n4. Exit\nAns.\t";   cin>>ch;     switch(ch)   {   case 1:     insert();     break;     case 2:       delet();     break;     case 3:       display();     break;     case 4:               return 0;     default:       cout<<"\n\nInvalid entry. Please try again...\n";   } } while(1); getch(); } void insert() { if(rear == MAX-1)   cout<<"\n\nQueue is full.\n\n"; else {   cout<<"\nEnter ITEM: ";   cin>>item;             if(rear == -1 && front == -1)   {               rear = 0;               front = 0;      

Implement Stack using Array...

Image
#include<iostream> #include <conio.h> #define MAX 10 using namespace std; void push(); void pop(); void display(); int stack[MAX], top=-1, item; int main() {  int ch;  do  {   cout<<"Press\n1.\tPush\n2.\tPop\n3.\tDisplay\n4.\tExit\n";     cin>>ch;    switch(ch)    {     case 1:     push();     break;     case 2:         pop();         break;     case 3:         display();         break;     case 4:         return 0;     default:       cout<<"\n\nInvalid entry. Please try again...\n";       }     }   while(ch!=4);     getch();     return 0; } void push() { if(top == MAX-1)   cout<<"\n\n\t\t\tStack is full."; else {   cout<<"\nEnter ITEM: ";   cin>>item;   top++;   stack[top] = item;   cout<<item; } } void pop() { if(top == -1)

Double Linked List-C++....

Image
#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

To delete last from Linked List.

Image
#include<iostream> #include<conio.h> using namespace std; struct node {        int info;        node *next; }; class list {       public:       int count=0;       node *head;       list()       {head=NULL;}                void add_end(int x)           {   if(head==NULL)             { head=new node;             head->info=x;             head->next=NULL;             count++;   }       else       {           node *ptr;           ptr=head;           while(ptr->next!=NULL)           ptr=ptr->next;           node *ctr;           ctr=new node;           ctr->info=x;           ctr->next=NULL;           ptr->next=ctr;           count++;   }      }           void delete_from_end()           {           node *ptr, *ctr;           ptr=head;           if(ptr==NULL)           cout<<"There is no node present in list. List is empty.";  

To delete a node from beginning.

Image
#include<iostream> #include<conio.h> using namespace std; struct node {        int info;        node *next; }; class list {       public:       int count=0;       node *head;       list()       {head=NULL;}                         void add_end(int x)           {   if(head==NULL)             { head=new node;             head->info=x;             head->next=NULL;             count++;   }       else       {           node *ptr;           ptr=head;           while(ptr->next!=NULL)           ptr=ptr->next;           node *ctr;           ctr=new node;           ctr->info=x;           ctr->next=NULL;           ptr->next=ctr;           count++;   }      }           void delete_from_beginning()           {           node *ptr;           ptr=head;           if(ptr==NULL)           cout<<"There is no node present in list. List is empty.

Add a node after a given location.

Image
#include<iostream> #include<conio.h> using namespace std; struct node {        int info;        node *next; }; class list {       public:       int count=0;       node *head;       list()       {head=NULL;}                       void add_end(int x)           {   if(head==NULL)             { head=new node;             head->info=x;             head->next=NULL;             count++;   }       else       {           node *ptr;           ptr=head;           while(ptr->next!=NULL)           ptr=ptr->next;           node *ctr;           ctr=new node;           ctr->info=x;           ctr->next=NULL;           ptr->next=ctr;           count++;   }    }           void add_at_loction(int y)           {           if(count<y)           {           cout<<"Not valid location because total number of of nodes you enterd is "<<

Add a node before given location of node

Image
#include<iostream> #include<conio.h> using namespace std; struct node {        int info;        node *next; }; class list {       public:       int count=0;       node *head;       list()       {head=NULL;}                       void add_end(int x)           {   if(head==NULL)             { head=new node;             head->info=x;             head->next=NULL;             count++;   }       else       {           node *ptr;           ptr=head;           while(ptr->next!=NULL)           ptr=ptr->next;           node *ctr;           ctr=new node;           ctr->info=x;           ctr->next=NULL;           ptr->next=ctr;           count++;   }    }           void add_at_loction(int y)           {           if(count<y)           {           cout<<"Not valid location because total number of of nodes you enterd is "<<

Inserting node with choice (Beginning/End)

Image
#include<iostream> #include<conio.h> using namespace std; struct node {        int info;        node *next; }; class list {       public:       node *head;       list()       {head=NULL;}       void add_begining(int x)       {    if(head==NULL)            {head=new node;             head->info=x;             head->next=NULL;            }       else       {           node *ctr;           ctr=new node;           ctr->info=x;           ctr->next=head;           head=ctr;           }                 }           void disp()           {  node *ptr;                  ptr=head;                while(ptr!=NULL)                {cout<<endl<<ptr->info;                ptr=ptr->next;}           }           void add_end(int x)           {           node *ptr;           ptr=head;           while(ptr->next!=NULL)           ptr=ptr->next;           node *ctr;           ctr=new node;           ctr->info=x;  

Insert node at end of linked list

Image
#include<iostream> #include<conio.h> using namespace std; struct node {        int info;        node *next; }; class list {       public:       node *head;       list()       {head=NULL;}                         void add_end(int x)           {  if(head==NULL)            {head=new node;             head->info=x;             head->next=NULL;            }       else       {           node *ptr;           ptr=head;           while(ptr->next!=NULL)           ptr=ptr->next;           node *ctr;           ctr=new node;           ctr->info=x;           ctr->next=NULL;           ptr->next=ctr;        }     }            void disp()           {  node *ptr;             ptr=head;                cout<<"\n\nElements you entered\n";   while(ptr!=NULL)                { cout<<endl<<"\t\t"<<ptr->info;                ptr=ptr->next;}           }                 }

Adding in the beginning

Image
#include<iostream> #include<conio.h> using namespace std; struct node {        int info;        node *next; }; class list {       public:       node *head;       list()       {head=NULL;}       void add_beginning(int x)       {    if(head==NULL)            {head=new node;             head->info=x;             head->next=NULL;            }       else       {           node *ctr;           ctr=new node;           ctr->info=x;           ctr->next=head;           head=ctr;           }                 }           void disp()           {  node *ptr;                  ptr=head;                while(ptr!=NULL)                {cout<<endl<<"\t"<<ptr->info;                ptr=ptr->next;}           }                         }; int main() {     list obj;     int n,a;     cout<<"How many nodes you want to use ";     cin>>n;     for(int i=1;i<=n;i++)     {cout<<&qu