Sunday, August 15, 2010

c++ Program list3

Program list
1. Program to convert octal number
2. Convert decimal to binary
3. Program for reverse number
4. Create link list
5. Stack with link list 

/*

* Program to convert octal number
* */

#include<iostream>

using namespace std;

int main()
{
cout<<"Enter the decimal number"<<endl;
int decimal;
cin>>decimal;
int octal =0;
int a=1;

while(dedcimal>0)
{
int i =decimal%8; //i=4
i =i*a; //i=4
a= a*10; // a=10
octal =octal +i; // i=4
decimal =decimal/8;
}
cout<<"octal --->"<<octal<<endl;
return 0;
}

/*

* Convert decimal to binary
* */

#include<iostream>

using namespace std;

int main()
{
long i;
cout<<"enter the decimal number"<<endl;
int decimal;
cin>>decimal;

int bina=0;
int a=1;
while(decimal>0)
{
i= decimal%2;
i=i*a;
a=a*10;
bina =bina+i;
decimal =decimal/2;
}

cout<<"binary number "<<bina<<endl;
return 0;
}

/*

* Program for reverse number
* */

#include<iostream>

using namespace std;
int main()
{
cout<<"Enter the number"<<endl;
int decimal;
cin>>decimal;
int rev=0;
while(decimal>0)
{
int a=decimal%10;
rev =rev*10+a;
decimal =decimal/10;
}
cout<<"reverse number "<<rev<<endl;
}

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:
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;
}

Stack with 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;
}

No comments:

Post a Comment