Social Icons

Showing posts with label DATA. Show all posts
Showing posts with label DATA. Show all posts

Sunday, 14 September 2014

Insertion Deletion In Linked List

#include<iostream>
#include<conio.h>
using namespace std;
struct link
{
int data;
struct link *next;

};

void display();
void initial();
void deletion();

struct link *newn,*loc,*start=NULL,*ptr;


int main()
{
int i,item;
for(i=1;i<5;i++)
    {
    cout<<"Enter element";
cin>>item;
newn =new struct link[1];
newn->data=item;
newn->next=NULL;


if(start==NULL)
    {
    start=newn;
    }
    else
    {
    ptr=start;
    while(ptr->next!=NULL)
    {
    ptr=ptr->next;
   
    }
   
    ptr->next=newn;
    }

 }
   
    display();
    //for(i=1;i<=2;i++)
    //{
   
initial();
//}
}





void display()
{
ptr=start;
while(ptr!=NULL)
{
cout<<ptr->data;
cout<<"-->";
ptr=ptr->next;
}


}



void initial()
{
int item;
    cout<<"Enter the item which u want to insert in begining";
    cin>>item;
   
    struct link *p;
    p=new struct link[1];
    p->data=item;
    p->next=NULL;
    if(start==NULL)
    {
    start=p;
   
    }
else
    {
      p->next=start;
 start=p;
    }

display();
deletion();
}


void deletion()
{
char c;
cout<<"do u want to delete from begining";
cin>>c;
if(c=='y')
{
start=start->next;
display();
}

}

Saturday, 4 May 2013

Reversing a linked list inserted from begining


#include<iostream>
#include<conio.h>
using namespace std;
struct link
{
 int data;
 struct link *next;
 struct link *pre;
};
struct link *newn,*loc, *start,*ptr,*end;
void binsert();
void einsert();
void minsert();
void sinsert();
void reverse();
void display();
int main()
{char ch;
int op;
for(int i=0;i<5;i++)
{

 binsert();
 display();
}
cout<<"\n reverse of the linked list : ";
   reverse();
 
getch();
}


void display()
{
ptr=start;
while(ptr!=NULL)
{
cout<<" "<<ptr->data;
cout<<"-->";
ptr=ptr->next;
}
}



void binsert()
{
int item;
cout<<"\n Enter item to insert in begining :";
cin>>item;
newn = new struct link[1];
newn->data=item;
newn->next=NULL;
newn->pre=NULL;
//tr=start;
loc=end;
if(end==NULL)
{
start=newn;
end=newn;
}
else
{
while(loc->pre!=NULL)
{
loc=loc->pre;
}
 // newn->next=start;
loc->pre=newn;
  newn->next=loc;
  start=newn;




}

}

 void reverse()
 {
  loc=start;
  while(loc!=NULL)
  {
  cout<<" "<<loc->data;
cout<<"-->";
loc=loc->next;
 
  }
 }











Saturday, 27 April 2013

Preorder and Postorder Tree Implementation Using Linked List


#include<iostream>
#include<string.h>
#include<conio.h>
#include<stdio.h>
using namespace std;
int top=0;
struct node
{
int info;
node * right;
node * left;
}*newptr,*root,*par,*ptr,*pos,*save,*loc,*child,*parsuc,*suc,*s,*stack[10];
void ins(int);
void show();
void disp();
void view();
void find(int);
int main()
{
root=NULL;
//clrscr();
int a,item;
char ch;
int i,n;
cout<<"\n enter limit : ";
cin>>n;
cout<<"\n\n\n\tEnter the item to insert: ";
for(i=0;i<n;i++)
{ cin>>a;
ins(a);
}
stack[top]=NULL;
               
cout<<"\n\t Pre-order of tree :\t";
disp();
cout<<"\n\t In-order of tree :\t";
show();

getch();

}
void ins(int a)
{
newptr=new node[1];
newptr->info=a;
newptr->right=newptr->left=NULL;

if(root==NULL)
{
root=newptr;
}
else
{
ptr=root;
while(ptr!=NULL)
{
if(ptr->info>=a)
{
if(ptr->left==NULL)
{
ptr->left=newptr;
break;
}
ptr=ptr->left;
}
else
{
if(ptr->right==NULL)
{
ptr->right=newptr;
break;
}
ptr=ptr->right;
}
}
}
}
void disp()
{

ptr=root;
top=0;
while(ptr!=NULL)
{       cout<<"\t"<<ptr->info;
if(ptr->right!=NULL)
{       top++;
stack[top]=ptr->right;
}
if(ptr->left!=NULL)
{
ptr=ptr->left;
}
else
{    
ptr=stack[top--];
}

}

}

  void show()
  {
  ptr=root;
  x: while(ptr!=NULL)
  {
 
  top=top+1;
  stack[top]=ptr;
 
  ptr=ptr->left;
    }
 
    ptr=stack[top];
    top=top-1;
    while(ptr!=NULL)
    {
    cout<<"\t "<<ptr->info;
     if(ptr->right!=NULL)
        {
         ptr=ptr->right;
         goto x;
        }
 
     ptr=stack[top];
    top=top-1;
 
  }
     

    }
 











   
   
   
   
   

 

Friday, 26 April 2013

Merge Sort Using arrays in Decreasing Order

#include<iostream>
#include<conio.h>
using namespace std;
int  merge(int a[],int m,int b[],int n,int c[],int p);
 int main()
{
    int a[10],b[10],c[10];
    int m,n,p;
   
   
        cout<<"\n Enter size of First array : ";
        cin>>m;
    cout<<"\n\t Enter element of First array : \n";
    for(int i=0;i<m;i++)
    {
        cin>>a[i];
               
    }
   
      cout<<"\n Enter size of Second array  : ";
        cin>>n;
    cout<<"\n\t Enter element of Second array  : \n";
    for(int i=0;i<n;i++)
    {
        cin>>b[i];
               
    }
   
     
   

   merge(a,m,b,n,c,p);
   cout<<"\n after merging : ";
  
   for(int k=0;k<m+n;k++)
   {
      
       cout<<" "<<c[k];
   }
  
  

getch();

}


int  merge(int a[10],int m,int b[10],int n,int c[10],int p)
{
     int i=0;
     int j=0;
     int k=0;
  while(i<m && j<n)
  {
      if(a[i]>=b[j])
      {
          c[k]=a[i];
          i++;
      }
      else if(a[i]<=b[j])
      {
          c[k]=b[j];
          j++;
      }
     
   k++;
  }
 
  if(i==m && j<n)
  {
      c[k]=b[j];
      j++;
      k++;
     
  }
    if(j==n && i<m)
  {
      c[k]=a[i];
      i++;
     k++;
     
  }
   


}


Output :





Selection Sort Using arrays


#include<iostream>
#include<conio.h>
using namespace std;
int main()
{

int a[100],i,n,p,k,min,loc,temp;

cout<<"\n------------ SELECTION SORT ------------ \n\n";
cout<<"Enter No. of Elements=";
cin>>n;

cout<<"\nEnter Elements=\n";
for(i=1;i<=n;i++)
{
cin>>a[i];
}

for(p=1;p<=n-1;p++)              // Loop for Pass
{
min=a[p];                        // Element Selection
loc=p;

for(k=p+1;k<=n;k++)              // Finding Min Value
{
if(min>a[k])
{
min=a[k];
loc=k;
}
}

temp=a[p];                        // Swap Selected Element and Min Value
a[p]=a[loc];
a[loc]=temp;

}

cout<<"\nAfter Sorting : \n";

for(i=1;i<=n;i++)
{
cout<<a[i]<<endl;
}

getch();
}

Merge sort using arrays


#include<iostream>
#include<conio.h>
using namespace std;
int  merge(int a[],int m,int b[],int n,int c[],int p);
 int main()
{
int a[10],b[10],c[10];
int m,n,p;


cout<<"\n Enter size of First array";
   cin>>m;
cout<<"\n Enter element of First array";
for(int i=0;i<m;i++)
{
cin>>a[i];

}

  cout<<"\n Enter size of Second array";
   cin>>n;
cout<<"\n Enter element of Second array";
for(int i=0;i<n;i++)
{
cin>>b[i];

}

 


   merge(a,m,b,n,c,p);
   cout<<"\n after merging : ";
 
   for(int k=0;k<m+n;k++)
   {
   
    cout<<" "<<c[k];
   }
 
 

getch();

}


int  merge(int a[10],int m,int b[10],int n,int c[10],int p)
{
  int i=0;
  int j=0;
  int k=0;
  while(i<m && j<n)
  {
  if(a[i]<=b[j])
  {
  c[k]=a[i];
  i++;
  }
  else if(a[i]>=b[j])
  {
  c[k]=b[j];
  j++;
  }
 
   k++;
  }

  if(i==m && j<n)
  {
  c[k]=b[j];
  j++;
  k++;
 
  }
if(j==n && i<m)
  {
  c[k]=a[i];
  i++;
     k++;
 
  }



}



output:






 

Sample Text

Sample text

 
Just Programming Cse DriveReputation Management