Tuesday, 23 October 2012

CLL


/*  CLL as a class */
#include <iostream.h>
#include <conio.h>

class node
 {
public:
int  data;
node *next;
node()   //default constructor
 {
next=NULL;
 }
node(int x)   //parameterised constructor
 {
next=NULL;
data=x;
 }
  /* parameterised constructor will initialize the data field with x */
  };


class linkedlist
 {
node *head; //to store the address of the starting node
public:
linkedlist() //Default constructor
  {
head=NULL;
  }

~linkedlist(); // Destructor to delete a linked list.
void create();
void print();
int count();
int search(int x);
void insertf(int x);//insert at the front
void insertr(int x);//insert at the rear
void Deletef( );//Delete from the front
void Deleter();//Delete from the Rear
 };

 linkedlist:: ~linkedlist()
     {
node *p;
if(head!=NULL)
  {
while(head->next!=head)
  {
p=head->next;
head->next=p->next;
delete p;
  }
delete head;
head=NULL;
   }
    }

void linkedlist:: create()
  {
int x,n,i;
node *p;
cout << "\n Enter no of nodes : ";
cin>>n;
for(i=0;i<n;i++)
  {
cout<<"\n Next Data : ";
cin >> x;
if(head==NULL) // inserting the first node
   {
head=new node(x);
head->next=head;
   }
else
   {
p=new node(x);
p->next=head->next;
head->next=p;
head=p;
   }
  }
  }

 void linkedlist::print()
  {
cout<<"\n Data stored in the linked list : ";
node *p;
if(head==NULL)
return;
p=head->next;
do
  {
cout<<"  "<<p->data;
p=p->next;
  }while(head->next != p);
 }

int linkedlist::count()
 {
int i=0;
node *p;
if(head==NULL)
return 0;
p=head->next;
   do
     {
i++;
p=p->next;
     }while(head->next != p);
   return i;
 }

int linkedlist::search(int x)
 {
int i=0;
node *p;
if(head==NULL)
return -1;
p=head->next;
do
  {
i++;
if(p->data==x)
return i;
p=p->next;
  }while(head->next != p);
return -1;
 }

void linkedlist::insertf(int x)
 {
node *p;
if(head==NULL) // inserting the first node
   {
head=new node(x);
head->next=head;
   }
else
   {
p=new node(x);
p->next=head->next;
head->next=p;
   }
 }

void linkedlist::insertr(int x)
 {
node *p;
if(head==NULL) // inserting the first node
 {
head=new node(x);
head->next=head;
 }
else
 {
p=new node(x);
p->next=head->next;
head->next=p;
head=p;
 }

 }

 void linkedlist::Deletef()
   {
node *p;
if(head->next==head)
 {
delete head;
head=NULL;
 }
else
 {
p=head->next;
head->next=p->next;
delete p;
 }
  }

 void linkedlist::Deleter()
   {
node *p;
if(head->next==head)
 {
delete head;
head=NULL;
 }
else
 {
p=head->next;
while(p->next != head) // locate the predecessor of head node
p=p->next;
p->next=head->next;
delete head;
head=p;
 }
  }



void main()
 {
int op,x,loc;
linkedlist l,l1,l2;
clrscr();
do
{
cout<<"\n\n1)Create\n2)Print\n3)Insert(front)\n4)insert(rear)";
cout<<"\n5)Delete(front)\n6)Delete(rear)\n7)length";
cout<<"\n8)Search\n9)Quit";
cout<<"\nEnter your Choice : ";
cin >> op;
switch(op)
  {
case 1: l.create();break;
case 2: l.print();break;
case 3: cout<<"Enter the data  :"; cin>>x;
l.insertf(x);break;
case 4: cout<<"Enter the data  :"; cin>>x;
l.insertr(x);break;
case 5: l.Deletef();break;
case 6: l.Deleter();break;
case 7: cout << "Length of the linked list = "<<l.count();
break;
case 8: cout << "Enter element to be searched : ";
cin >> x;
loc=l.search(x);
if(loc==-1)
cout << "\n Element not found ";
else
cout << "\n Found at location : "<<loc;
break;
}
      }while(op!=9);
 }

/*

OUTPUT:

1)Create
2)Print
3)Insert(front)
4)insert(rear)
5)Delete(front)
6)Delete(rear)
7)length
8)Search
9)Quit
Enter your Choice : 1

 Enter no of nodes : 4

 Next Data : 1

 Next Data : 2

 Next Data : 3

 Next Data : 4



Enter your Choice : 2

 Data stored in the linked list :   1  2  3  4


Enter your Choice : 3

Enter the data  :55

Enter your Choice : 4

Enter the data  :45

Enter your Choice : 5

Enter your Choice : 6

Enter your Choice : 2

 Data stored in the linked list :   1  2  3  4


Enter your Choice : 7
 Length of the linked list = 4


Enter your Choice : 8
 Enter element to be searched : 4

 Found at location : 4


Enter your Choice : 9

*/

//SLL


//Singly linked list(SLL)....................
#include<iostream.h>
#include<conio.h>
class node
{
 public:
 int data;
 node *next;
 node()
 {
  next=NULL;
 }
 node(int x)
 {
  next=NULL;
  data=x;
 }
};
class linkedlist
{
 node *head;
 public:
       linkedlist()
{
 head=NULL;
}
 
void create();
void print();
void insert(int x,int loc);
void Delete(int loc);
int search(int x);

};

void linkedlist::create()
{
 int x,n,i;
 node *p;
 cout<<"\nEnter no of nodes:";
 cin>>n;
 for(i=0;i<n;i++)
 {
  cout<<"\nNext Data:";
  cin>>x;
  if(head==NULL)
  head=p=new node(x);
  else
   {
    p->next=new node(x);
    p=p->next;
   }
  }
}
void linkedlist::print()
{
 cout<<"\nData stored in a linked list:";
 for(node *p=head;p!=NULL;p=p->next)
 cout<<p->data<<" ";
}
void linkedlist::insert(int x,int loc)
{
node *p=new node(x);
if(loc==1)
{
p->next=head;
head=p;
}
else
{
node *q=head;
for(int i=1;i<loc-1;i++)
if(q!=NULL)
 q=q->next;
 else
   {
      cout<<"OVERFLOW";
      return;
   }
 p->next=q->next;
 q->next=p;
}
}
void linkedlist::Delete(int loc)
{
 node *p,*q;
 int i;
 if(loc==1)
 {
  p=head;
  head=head->next;
  delete p;
 }
 else
 {
 q=head;
 for(i=1;i<loc-1;i++)
 if(q->next==NULL)
 {
  cout<<"\nUnderflow........";
  return;
 }
 else
    q=q->next;
    p=q->next;
    q->next=p->next;
    delete p;
    }
    }
int linkedlist::search(int x)
{
 int i=0;
 for(node *p=head;p!=NULL;p=p->next)
 {
  if(p->data==x)
  return(i);
  i++;
 }
  return -1;
}
void main()
{
 int op,x,loc;
 clrscr();
 linkedlist l;
 do
 {
  cout<<"\n1)Create\n2)print\n3)Insert\n4)Delete\n5)search\n6)quit";
  cout<<"\nEnter your choice:";
  cin>>op;
  switch(op)
  {
   case 1:l.create();
   break;
   case 2:l.print();
   break;
   case 3:cout<<"\nEnter the location:";
 cin>>loc;
 cout<<"\nEnter the data:";
 cin>>x;
 l.insert(x,loc);
 break;
   case 4:cout<<"\nEnter the location:";
 cin>>loc;
 l.Delete(loc);
 break;
   case 5:cout<<"\nEnter the element to be searched:";
 cin>>x;
 loc=l.search(x);
 if(loc==-1)
 cout<<"\nElement not found..";
 else
 cout<<"\nfound at location:"<<loc+1;
 break;

  }
 }while(op!=6);getch();
 }

Few Linux App & commands which is going to used to Build AOSP(Android Open Source Project) Source Code

1. sudo apt-get install git-core gnupg flex bison gperf build-essential zip curl zlib1g-dev gcc-multilib g++-multilib libc6-dev-i386 lib32n...