Labels

Thursday 25 June 2015

SINGLY LINKLIST ALL OPERATIONS CODE

#include<iostream>
using namespace std;

class linkedlist
{
    public: struct node
    {
    int data;
    node *link;
    }*head;
        
           void append(int);
           void addafter(int,int);
           void search();
           void remove();    
           void display();
           linkedlist();    
};        
linkedlist::linkedlist()
    {
    head=NULL;
    }
void linkedlist::append(int item)
   {
    node *temp,*s;
    if(head==NULL)
    {
      temp=new node;
      temp->data=item;
      temp->link=NULL;
      head=temp;
    }
    else 
     {    
      temp=head;
      while(temp->link!=NULL)
      {
        temp=temp->link;
      }
        s=new node;
        s->data=item;
        s->link=NULL;
        temp->link=s;
      }
        
   }
void linkedlist::addafter(int loc,int item)
    {
    int i;
    node *temp,*s;

    temp=head;
    for(i=1;i<loc;i++)
    {
    temp=temp->link;
    }
        {
        if(temp==NULL)
        {
        cout<<"location is greater than linkedlist";
        }
        else
            {
            s=new node;
            s->data=item;
            s->link=temp->link;
            temp->link=s;
            }
        }
    }


void linkedlist::display()
    {
         node *temp;
     temp=head;
     while(temp!=NULL)
       {
         cout<<"["<<temp->data<<"]"<<"\n";
         temp=temp->link;  
           }
    }

void linkedlist::search()
{
    int i=0,key;
    node *temp;
    temp=new node;
    cout<<"Enter the element you wish to search";
    cin>>key;
    temp=head;
    while(temp->data != key)
        {
            if(temp==NULL)
            {
            cout<<"Element not found";
            }
            else
            {
            temp= temp->link;
            i++;
            }
        }
    cout<<"Your element is stored at"<<i<<"location";
}

void linkedlist::remove()
    {
        int key;
        node *temp,*p,*q,*r;
        temp=new node;
        cout<<"\n"<<"Enter the element you want to delete";
        cin>>key;
        while(temp->data !=key)
        {
            if(temp==NULL)
             {
             cout<<"Element not found";
             }
            else
                {
                temp= temp->link;
                }
        }
        p=temp;
        q=temp->link;
        r=q->link;
        p=r;
    }
    
int main()
{
linkedlist l;
l.append(15);
l.append(30);
l.append(44);
l.append(12);
l.addafter(2,41);
l.addafter(4,35);
l.search();
l.remove();
l.display();
return 0;
}          

QUICK SORT IN PYTHON LANGUAGE CODE

def quickSort(alist): 
   quickSortHelper(alist,0,len(alist)-1) 

def quickSortHelper(alist,first,last): 
   if first<last: 

       splitpoint = partition(alist,first,last) 

       quickSortHelper(alist,first,splitpoint-1) 
       quickSortHelper(alist,splitpoint+1,last) 


def partition(alist,first,last): 
   pivotvalue = alist[first] 

   leftmark = first+1 
   rightmark = last 

   done = False 
   while not done: 

       while leftmark <= rightmark and \ 
               alist[leftmark] <= pivotvalue: 
           leftmark = leftmark + 1 

       while alist[rightmark] >= pivotvalue and \ 
               rightmark >= leftmark: 
           rightmark = rightmark -1 

       if rightmark < leftmark: 
           done = True 
       else: 
           temp = alist[leftmark] 
           alist[leftmark] = alist[rightmark] 
           alist[rightmark] = temp 

   temp = alist[first] 
   alist[first] = alist[rightmark] 
   alist[rightmark] = temp 


   return rightmark 

alist = [54,26,93,17,77,31,44,55,20] 
quickSort(alist) 
print(alist)

INORDER, PREORDER, POSTORDER TRAVERSING TREE CODE

#include<iostream.h> 
#include<conio.h> 

typedef struct tree 

int key; 
struct tree * left; 
struct tree * right; 
} Tree; 
int search(int arr[],int left, int right,int key) 

int i; 
for(i=left; i <= right; i++) 

if( key == arr[i]) 

return i; 


return -1; 


Tree * createTree(int postorder[],int inorder[],int left,int right, int pos) 

if(left > right) 
return NULL; 

Tree *t = new tree; 
t->key = postorder[pos]; 
int index = search(inorder,left, right, t->key); 
cout<<"\n"<<t->key; 
t->left = createTree(postorder,inorder ,left,index-1, pos - 1- (right - index)); 
t->right = createTree(postorder,inorder,index +1, right, pos -1); 


return t; 



void main(){ 

int postorder[] = {4,1,3,10,11,8,2,7}; 
int inorder[] = {4,10,3,1,7,11,8,2}; 
 clrscr(); 
Tree *newTree = createTree(postorder,inorder,0,7,7); 
getch(); 
}

NEWSPAPER BOY FINDING SHORTEST PATH USING PRIMS ALGORITHM CODE Program

#include<iostream.h> 
#include<conio.h> 
class newspaper{ 
public: 
    int houses; 
    int dist[10][10],visited[10]; 
    void setpath(); 
    void dfs(int); 
    void prims(int[10][10],int); 

}; 

void newspaper::setpath(){ 
int i,j; 
cout<<"Enter no of houses"; 
cin>>houses; 
cout<<"Now enter the distance between them..if no route place 999"; 
for(i=1;i<=houses;i++){ 
    for(j=1;j<=houses;j++){ 
        cout<<"Enter distance between house"<<i<<"to house"<<j; 
        cin>>dist[i][j]; 
        if(dist[i][j]==0)dist[i][j]=999; 
        } 
    } 
for(i=1;i<=houses;i++){ 
        visited[i]=0; 
        } 

void newspaper::dfs(int k){ 
int i; 
cout<<k; 
visited[k]=1; 
for(i=1;i<=houses;i++){ 
    if(dist[i][k]!=0||dist[i][k]!=-99){ 
    if(!visited[i]) 
    dfs(i); 



void newspaper::prims(int dist[10][10],int n){ 
 int visited[10]={0}; 
 int ne,i,j,a,b,u,v=1; 
 int minwt,min=0; 
 visited[1]=1; 
  while(ne<n) 
 { 
  for(i=1,min=999;i<=n;i++) 
   for(j=1;j<=n;j++) 
    if(dist[i][j]<min) 
     if(visited[i]!=0) 
     { 
      min=dist[i][j]; 
      a=u=i; 
      b=v=j; 
     } 
  if(visited[u]==0 || visited[v]==0) 
  { 
   cout<<"\n Edge "<<ne++<<"\t"<<a<<"-"<<b<<"\t"<<min; 
   minwt+=min; 
   visited[b]=1; 
  } 
  dist[a][b]=dist[b][a]=999; 
 } 
 cout<<"\n Minimun cost"<<minwt; 

void main(){ 
newspaper np;int k; 
clrscr(); 
np.setpath(); 
cout<<"Enter the name of house from where to traverse the newspaper"; 
cin>>k; 
cout<<"\n"; 
cout<<"*********Path as per DFS*********"<<"\n"; 
np.dfs(k); 
cout<<"\n"; 
cout<<"******************Shortest Path********"<<"\n"; 
np.prims(np.dist,np.houses); 
getch(); 
}