Tuesday, June 22, 2010

C++ Program list (link list)

1. Create SimPle link list with some operation
#include
using namespace std;


class node
{
public:
T data;
node *next;
};
template <class T>
class link
{

public:
node *first;
link();
void add_node(T data);
void create_link_list();
void insert_data_link_list(T data);
void add_node_link_list(T data);
void delete_node_after_data_node(T data);
void delete_node_before_data_node(T data);
friend ostream& operator<< (ostream &o ,link &link_lst);

};
template<class T>
link::link()
{
first =NULL;
}

//Create node in link list
template
void link::add_node(T data)
{

node *last;
node *p =new node;
//filling data
p->data =data;
p->next =NULL;

if(first==NULL)
first=p;

else
{
   last=first;
   while(last->next!=NULL)
   last =last->next;
   last->next =p;
}

}

// Provide the input and create the link list.
template<class T>
void link::create_link_list()
{

node *p =new node;
T data;
node *last;
if(first == NULL)
{
cout<<"Enter the data"<<data<<endl;

p->data =data;
p->next =NULL;

first =p;
}

int continue_list =1;
while(continue_list)
{
    cout<<"Enter data "<<data;
    add_node(data);
      cout<<" Do you want to continue Please enter 1 or 0"<<endl;
 }

}

//Insert data into Link list
//Ex  1->2->3
//output : 1->11->2->11->3
template <class T>
void link::insert_data_link_list(T data)
{

node *last =first;

while(last->next!=NULL)
{
   node *p =new node;
   p->data =data;
   p->next =last->next;
  last->next =p;
  last = p->next;
 }
}

// Add Node before Node into Link list
template<class T>
void link::add_node_link_list(T data)
{

    node *last;
   //if the list is empty
   if(first->next == NULL)
   {
        node *p =new node;
        p->data =data;
        p->next =first;
        first =p;
   }
while(last->next!=NULL)
{
    node *p =new node;
    p->data =data;
    p->next =last->next;
    last->next =p;
    last=p->next;
}
}

//Delete the Node After Data found
template<class T>
void link::delete_node_after_data_node(T data)
{
node *last;
if(first->data =data &&  first->next ==NULL)
{
cerr<<"you have only one node"<<endl;
}
else
{
      last=first;
      while(last->next!=NULL)
      {
                 if(last->data ==data)
                    {
                         p = last->next;
                        last->next = p->next;
                        delete p;
                    }
               last =last->next;
          }
 }

}
//Delete the Node Before Data found
template
void link::delete_node_before_data_node(T data)
{

      node *last;

      if(first->data =data && first->next == NULL)
     {
         cerr<<"you have only one node"<<endl;
     }
    else if (first->next->data == data)
    {
              p=first->next;
              first=p;
             delete first;
     }
else
{
 last =first;
while(last->next !=NULL)
{
if(last->next->next->data =data)
{
       p=last->next;
      last->next = p->next;
      delete p;
 }
last= last->next;
}
}
}

// Print the link list
template<class T>
ostream& operator<< <T>(ostream &o, link &link_list)
{
       node *last =link_list.first;
       while(last !=NULL)
      {
             o<<last->data<<endl;
             last=last->next;
      }
 last =last->next;
}
return o;
}

2. Add two singly link list


#include <iostream.h>

template<class T> 
class node
{
   public:
  T data;
   node *next;
};


template<class T>
class link
{
   public:
   node *first;
   link();
   void operator+(link& b);
};

template <class T>
link::link()
{
first =NULL;
}

//Add two link list
template <class T>
void link::operator + (link &b)
{

node *last =first;
while(last-&gt;next!=NULL)
last =last-&gt;next;

node *second_last = b.first;
last->next =second_last;

while(second_last->next!=NULL)
second_last =second_last->next;

}

int main()
{
   a.operator+(b);
}

#Swap the data of Node

#include<iostream>
using namespace std;

template<class T>
class node
{
 public:
 T data;
  node *next;
};

template<class T>
class link_list
{

 public:
 node<T> *first;
 link_list();
 void create_link_list(T data);
 void swap_node_data();
 friend ostream& operator<< <T>(ostream &o,link_list<T>& link);
};

template<class T>
link_list<T>::link_list()
{
 first =NULL;
}

//Create Node in the link list
template<class T>
void link_list<T>::create_link_list(T data)
{
 node<T> *last;
 node<T> *p =new node<T>;
 p->data =data;
 p->next =NULL;

 if(first ==NULL)
 {
    first =p;
  }
 else
{
  last =first;
  while(last->next!=NULL)
   last =last->next;
  last->next =p;
 }
}

//swap the data
template<class T>
void link_list<T>::swap_node_data()
{
 node<T> *last =first;
 node<T> *p;

 while(last ->next->next !=NULL)
 {
   int no =last->data;
   last->data =last->next->data;
  last->next->data =no;
  last =last->next->next;
}
}

template<class T>
ostream& operator<<(ostream& o,link_list<T>& link)
{
 node<T> *last = link.first;
 while(last!=NULL)
 {
  o<<last->data<<" ------>" ;
  last =last->next;
 }
 return o;
}

int main()
{
 link_list<int>a;

 for(int i=0;i<6;i++)
 {
  a.create_link_list(i);
 }
 cout<<a;
 cout<<endl<<endl;
 a.swap_node_data();

 cout<<a;
 return 0;
}


Overload New Operator Program
#include<iostream>
#include<malloc.h>

using namespace std;
class sample
{
     int i;
     char c;
    public:
    void* operator new(size_t s,int ii,char cc)
   {
        sample *q =(sample*) malloc(s);
        q->i =ii;
        q->c =cc;
       cout<<"Memory allocation"<<endl;
       return q;
    }
void operator delete(void *q)
 {
  free(q);
 }
 void display()
 {
  cout<<i<<"   "<<c;
 }
};

int main()
{
 sample *s =new (7,'a')sample;
 return 0;
}


6.   1-------->2------->3------->4-------->5



         \    /     \  /        \  /       \  /


           3         -1         7          -1




#include<iostream>
using namespace std;

template<class T>
class node
{
 public:
 T data;
  node<T> *next;
};

template<class T>
class link_list
{
 public:
 node<T> *first;
 link_list();
 void create_link_list(T data);
 void new_link_list();
 friend ostream& operator<< <T>(ostream &o,link_list<T> &link);
};

template<class T>
link_list<T>::link_list()
{
 first =NULL;
}
template<class T>
void link_list<T>::create_link_list( T data)
{
 node<T> *last;
 node<T> *p =new node<T>;
 p->data =data;
 p->next =NULL;

 if(first ==NULL)
 {
  first =p;
 }
 else
 {
  last =first;
  while(last->next!=NULL)
   last =last->next;
  last->next =p;
 }
}

template<class T>
void link_list<T>::new_link_list()
{
 node<T> *last =first;
 node<T> *t =first;
 while(last->next!=NULL)
 {
  node<T> *p =new node<T>;
  p->data =last->data +last->next->data;
  last =last->next;
  p->next =t->next;
  t->next =p;
  t=last;
 // cerr<<"last"<<last->data<<endl;
  node<T> *s =new node<T>;

  s->data = last->data - last->next->data;
  last =last->next;
  s->next =t->next;
  t->next =s;
  t =last;
 }
}
template<class T>
ostream& operator<< ( ostream& o,link_list<T>&list)
{
 node<T> *last =list.first;
 while(last!=NULL)
 {
  o<< last->data <<"   ----->";
  last =last->next;
 }
}

int main()
{
 link_list<int> a;
 for(int i=0;i<5;i++)
  a.create_link_list(i);
 cout<<a;

 a.new_link_list();
 cout<<a;
 return 0;
}


1------->2------>3-------->4--------->5


                   \/ \/                 \/  \/


                   3  -1                7  -1






#include<iostream>
using namespace std;

template<class T>
class node
{
  public:
 T data;
 node<T> *next;
};

template<class T>
class link_list
{
 public:
 link_list();
 void create_link_list(T data);
 void new_link_list();
 friend ostream& operator<< <T>(ostream &o,link_list<T>&link);
 node<T> *first;
};

template<class T>
link_list<T>::link_list()
{
 first =NULL;
}

template<class T>
void link_list<T>::create_link_list(T data)
{
node<T> *last;
node<T> *p =new node<T>;

 p->data =data;
 p->next =NULL;
 if(first ==NULL)
 {
  first =p;
 }
 else
 {
  last =first;
  while(last->next!=NULL)
   last=last->next;

  last->next=p;
 }
}

template<class T>
ostream& operator<< (ostream& o,link_list<T>& link)
{
 node<T> *last =link.first;
 while(last!=NULL)
 {
  o<<last->data<<" --->";
  last =last->next;
 }

}

template<class T>
void link_list<T>::new_link_list()
{
 node<T> *last =first;
 node<T> *a  =first;

// node<T> *b =first;

 while(last->next!=NULL)
 {
  node<T> *p =new node<T>;
  node<T> *s =new node<T>;


  p->data =last->data +last->next->data;
  s->data =last->data -last->next->data;

  last =last->next;
  a= last;

 last =last->next;
 p->next =a->next;
 a->next =p;

 // b=a->next;
  s->next =p->next;
  p->next =s;
 }
}

int main()
{
 link_list<int> a;
 for(int i=1;i<=5;i++)
 a.create_link_list(i);
 cout<<a;
 cerr<<endl<<endl;
 a.new_link_list();
 cout<<endl;
 cout<<a;
}




1---->2---->3----->-1------->3----->4----->7---->-1----->5


To remove the same node in the singly link list

#include<iostream>
using namespace std;

template<class T>
class node
{
 public:
 T data;
 node<T> *next;

};

template<class T>
class link_list
{
 public:
 link_list();
 void create_link_list(T data);
 void new_link_list();
 friend ostream& operator<< <T>(ostream &o,link_list<T> &link);
 node<T> *first;
};

template<class T>
link_list<T>::link_list()
{
 first =NULL;
}
template<class T>
void link_list<T>::create_link_list(T data)
{
 node<T> *last;
 node<T> *p =new node<T>;
 p->data =data;
 p->next =NULL;

 if(first ==NULL)
    first =p;
 else
 {
  last =first;
  while(last->next!=NULL)
   last=last->next;
  last->next =p;
 }
}

template<class T>
ostream& operator << ( ostream &o,link_list<T> &link)
{
 node<T> *last =link.first;
 while(last!=NULL)
 {
  o<<last->data<<"  ----->";
  last =last->next;
 }
}

template<class T>
void link_list<T>::new_link_list()
{
 node<T> *last=first;
 node<T> *q;
 while(last->next!=NULL)
 {
  node<T> *second =last->next;
 node<T> *p =last;

  while(second->next!=NULL)
 {
   cerr<<" second->data "<<second->data<<endl;
   if(last->data != second->data)
  {
    second =second->next;
    p =p->next;
   }

   else
   {
          q=p->next;
          p->next =q->next;
          delete q;
          second = p->next;
   }
}
      last =last->next;
 }

}

int main()
{
 link_list<int>a;
 for(int i=0;i<5;i++)
 {
  int data;
 cout<<" enter data"<<endl;
  cin>>data;
  a.create_link_list(data);
 }

 cout<<a;
 a.new_link_list();
 cout<<a;
}


can you make stack with singly link list
#include<iostream>
using namespace std;

template<class T>
class node
{

 public:
 T data;
 node<T> *next;
};
template<class T>
class stack
{

 public:
 node<T> *top;
 stack();

 void push(T data);
 int pop();
 friend ostream& operator<< <T>(ostream& o,stack<T>& stack);

};

template<class T>
stack<T>::stack()
{
 top =NULL;
}

template<class T>
void stack<T>::push(T data)
{

 node<T> *p =new node<T>;
 p->data =data;
 p->next =NULL;

 if(top ==NULL)
 {
      top =p;
 }
 else
 {
  p->next =top;
  top =p;
 }
}

template<class T>
int stack<T>::pop()
{
 node<T> *p;
 if(top ==NULL)
 cout<<"stack is empty"<<endl;
else
 {
  p=top;
  top =top->next;
  return p->no;
 }

}

template<class T>
ostream& operator<< (ostream&o, stack<T>&a)
{
 node<T> *last =a.top;
 cout<<last->data<<"   ---->";
}

int main()
{
   stack<int> a;
   for(int i=0;i<5;i++)
   a.push(i);
   cout<<a;
}
can you make queue with single link list

#include<iostream>
using namespace std;
template<class T>
class node
{
 public:
 T data;
 node<T> *next;
};

template<class T>
class queue
{
 public:
 node<T> *front;
 node<T> *rear;
 queue();

 void add(T data);
 int remove();
 friend ostream& operator<< <T>(ostream&a,queue<T>& qu);
};

template<class T>
queue<T>::queue()
{
 front =NULL;
 rear =NULL;
}

template<class T>
void queue<T>::add(T data)
{
 node<T> *p =new node<T>;
 p->data =data;
 p->next =NULL;

 if(rear==NULL)
    front=rear=p;

 else
 {
  rear->next=p;
  rear=rear->next;
 }
}

template<class T>
int queue<T>::remove()
{

 if(front==NULL)
   cout<<"queue is empty"<<endl;
 else
 {
     p=front;
     if(front->next!=NULL)
  front=front->next;
  return p->no;

 }
}
template<class T>
ostream& operator<< (ostream& o,queue<T>&q)
{
     node<T>* last =q.rear;
     while(last!=q.front)
    {
      o<<q.front->data;
      q.front =q.front->next;
   }
}

int main()
{
 queue<int>a;
  for(int i=0;i<5;i++)
 a.add(i);
 cout<<a;
}

Encrypt & decrypt Program
#include<iostream>
#include<string>

using namespace std;
class enc
{

public:
void getstring();
void encrypt();
void decrypt();
void show();

string s;
};


void enc::getstring()
{
cout<<"enter the string "<<endl;
cin>>s;
}

void enc::encrypt()
{
int i=0;
while(s[i]!='\0')
{
s[i] =s[i]+1;
i++;
}
}

void enc::decrypt()
{
int i=0;
while(s[i]!='\0')
{
s[i]=s[i]-1;
i++;
}
}

void enc::show()
{
cout<<s;
}

int main()
{
enc c;
c.getstring();
c.encrypt();
c.show();
c.decrypt();
c.show();
return 0;
}

The Program for 2^n

/*


* The Program for 2^n with the help

* of shift operator */

#include<iostream>
using namespace std;

int main()
{
int base;
int power;

cout<<" enter the base number"<<endl;
cin>>base;

cout<<" enter the power of number"<<endl;
cin>>power;

for(int i=0;i<power-1;i++)
base =base<<1;

cout<<base;
return 0;
}

To count the number of character & tabs blanks

/*



* To count the number of character & tabs blanks


* */


#include<iostream>
#include<fstream>
using namespace std;

int main()
{
FILE *fp;
char ch;
int nob=0;
int noc=0;
int nol=0;

fp=fopen("abhi.txt","r");
while(1)
{
ch=fgetc(fp); //to get character
if(ch ==EOF)
noc++;
if(ch==' ')
nob++;
if(ch=='\n')
nol++;
}
fclose(fp);

cout<<"number of character "<<noc<<endl;
cout<<"number of blank space "<<nob<<endl;
cout<<"number of line"<<nol<<endl;
return 0;
}

Print the top two element of stack

/*


* Print the top two element of stack

* */

#include<iostream>
#define size 5
using namespace std;

template<class T>
class stack
{
int pos;
public:
void push(T data);
T pop();
stack();
void show();
T stack_size[size];
};

template<class T>
stack<T>::stack()
{
pos=-1;
for(int i=0;i<5;i++)
stack_size[i]=0;
};
template<class T>
void stack<T>::push(T data)
{
stack_size[++pos]= data;
}
template<class T>
T stack<T>::pop()
{
return stack_size[pos--];
}

template<class T>
void stack<T>::show()
{
cout<<"showing top two element of stack"<<endl;
cout<<stack_size[pos]<<" " <<stack_size[pos-1]<<endl;
cout<<endl<<endl;
}

int main()
{
stack<int> s;
for(int j=0;j<5;j++)
s.push(j);
s.show();
return 0;
}