#include<stdio.h>
#include<conio.h>
int main()
{
float a,b;
printf("enter the values to swap \n");
scanf("%f%f",&a,&b);
printf("before swapping a:%f b:%f\n",a,b);
/*a=a+b;
b=a-b;
a=a-b;*/
a=a*b;
b=a/b;
a=a/b;
printf("After swapping a:%f b:%f\n",a,b);
_getch();
return 0;
}
#include<iostream>
using namespace std;
#include<conio.h>
#include<process.h>
template<class T>
class Stack
{
private:
int top,maxtop;
T *stack;
public:
Stack(int maxstacksize)
{
top=-1;
maxtop=maxstacksize;
stack=new T[maxtop];
}
~Stack()
{
delete[] stack;
}
int isfull();
int isempty();
void insertion();
void deletion();
void display();
void topmost();
};
template<class T>
int Stack<T>::isempty()
{
if(top==-1)
return 1;
else
return 0;
}
template<class T>
int Stack<T>::isfull()
{
if(top==maxtop-1)
return 1;
else
return 0;
}
template<class T>
void Stack<T>::topmost()
{
if(isempty())
cout<<"\nstack is underflow.......";
else
cout<<"\n topmost element of the stack is:"<<stack[top];
}
template<class T>
void Stack<T>::display()
{
if(isempty())
cout<<"\n stack is underflow..........";
else
cout<<"\nstack elements are :";
for(int i=0;i<=top;i++)
{
cout<<"\t";
cout<<stack[i];
}
}
template<class T>
void Stack<T>::insertion()
{
T ele;
if(isfull())
cout<<"\n stack is overflow.........";
else
{
cout<<"\n enter the element u want to insert :";
cin>>ele;
stack[++top]=ele;
}
}
template<class T>
void Stack<T>::deletion()
{
if(isempty())
cout<<"\n stack is underflow..........";
else
cout<<"\n deleted element is:"<<stack[top--];
}
int main()
{
int choice;
Stack<int> s(10);
do
{
cout<<"\n1.insertion\n2.deletion\n3.topmost\n4.display\n5.exit\n";
cout<<"\n enter ur choice :";
cin>>choice;
switch(choice)
{
case 1:
s.insertion();
break;
case 2:
s.deletion();
break;
case 3:
s.topmost();
break;
case 4:
s.display();
break;
case 5:
_exit(0);
}
}while(choice<=5);
_getch();
return 0;
}
/*top was compared to null instead of initializing to null*/
#include<iostream>
using namespace std;
template<class T>
class node
{
public:
T data;
node *link;
};
template<class T>
class stack
{
node<T> *top;
public:
stack();
~stack();
int isempty();
void push(T x);
T pop();
T topmost();
void display();
};
template<class T>
stack<T>::stack()
{
top=NULL;/*initially was top==null*/
}
template<class T>
int stack<T>::isempty()
{
if(top==NULL)
return 1;
else
return 0;
}
template<class T>
void stack<T>::push(T x)
{
node<T> *temp;
temp=new node<T>;
temp->data=x;
temp->link=top;
top=temp;
}
template<class T>
T stack<T>::pop()
{
node<T> *temp;
T x;
if(isempty())
return -1;
else
{
temp=top;
top=top->link;
x=temp->data;
delete temp;
return x;
}
}
template<class T>
void stack<T>::display()
{
node<T> *temp;
if(isempty())
{
cout<<"\n stack is empty ";
}
else
{
cout<<"\nthe contents are :";
temp=top;
while(temp!=NULL)
{
cout<<temp->data;
temp=temp->link;
}
}
}
template<class T>
T stack<T>::topmost()
{
if(isempty())
return -1;
else
return(top->data);
}
template<class T>
stack<T>::~stack()
{
node<T> *temp=NULL;
while(top!=NULL)
{
temp=top;
top=top->link;
delete temp;
}
}
int main()
{
int ch,x;
stack<int> a;
do
{
cout<<"\n 1.insertion \n 2.deletion \n 3.display \n 4.topmost element \n 5.exit \n";
cout<<"\n enter ur choice :";
cin>>ch;
switch(ch)
{
case 1:
cout<<"enter data element :\n";
cin>>x;
a.push(x);
break;
case 2:
x=a.pop();
if(x==-1)
cout<<"\n stack is empty....";
else
cout<<"\n the deleted value is :"<<x<<"\n";
break;
case 3:
a.display();
break;
case 4:
x=a.topmost();
if(x==-1)
cout<<"\n stack is empty..";
else
cout<<"\n the topmost value is :"<<x<<"\n";
break;
case 5:
break;
}
}while(ch<5);
return 0;
}
#include<iostream>
using namespace std;
#include<conio.h>
template<class T>
class node
{
public:
T data;
node<T> *link;
};
template<class T>
class list
{
private:
node<T> *first;
public:
list();
~list();
void createlist();
void deletion();
void insertion();
void display();
};
template<class T>
list<T>::list()
{
first=NULL;
}
template<class T>
list<T>::~list()
{
node<T> *next;
while(first)
{
next=first->link;
delete first;
first=next;
}
}
template<class T>
void list<T>::createlist()
{
T a;
node<T> *cur,*ptr;
cout<<"\nenter data for new node:";
cin>>a;
while(a)
{
cur=new node<T>;
cur->data=a;
cur->link=NULL;
if(first==NULL)
first=cur;
else
ptr->link=cur;
ptr=cur;
cout<<"\nenter data for new node :";
cin>>a;
}
}
template<class T>
void list<T>::insertion()
{
node<T> *cur,*ptr;
T ele;
char ch;
ptr=first;
cur=new node<T>;
cout<<"\n enter data for new node :";
cin>>cur->data;
cur->link=NULL;
cout<<"\n do u want to insert first [y/n]:";
cin>>ch;
if(ch=='Y'||ch=='y')
{
cur->link=first;
first=cur;
}
else
{
cout<<"\n after which element do u want to insert :";
cin>>ele;
while(ptr!=NULL)
{
if(ptr->data==ele)
{
cur->link=ptr->link;
ptr->link=cur;
break;
}
else
{
ptr=ptr->link;
}
}
}
}
template<class T>
void list<T>::deletion()
{
T ele;
char ch;
node<T> *ptr,*ptr1;
if(first==NULL)
{
cout<<"\nlist is empty .....";
}
else
{
ptr=first;
cout<<"\ndo u want to delete first element [y/n]:";
cin>>ch;
if(ch=='y'||ch=='Y')
{
first=first->link;
delete ptr;
}
else
{
cout<<"\nwhich element do u want to delete :";
cin>>ele;
while(ptr!=NULL)
{
if(ptr->link->data==ele)
{
ptr1=ptr->link;
ptr->link=ptr1->link;
delete ptr1;
return;
}
else
{
ptr=ptr->link;
}
}
}
}
}
template<class T>
void list<T>::display()
{
node<T> *ptr;
if(first==NULL)
{
cout<<"\n list is empty..";
}
else
{
ptr=first;
while(ptr!=NULL)
{
cout<<ptr->data<<" ";
ptr=ptr->link;
}
}
}
int main()
{
int n;
list <int> l;
l.createlist();
do
{
cout<<"\n 1.insertion \n2.deletion \n3.display \n4.exit \n";
cout<<"\n enter ur option : ";
cin>>n;
switch(n)
{
case 1: l.insertion();
break;
case 2: l.deletion();
break;
case 3: l.display();
break;
case 4:
exit(0);
break;
}
}while(n<=4);
_getch();
return 0;
}
//selection sorting
#include<iostream.h>
using namespace std;
#include<conio.h>
template <class T>
void selectionsort(T a[],int n)
{
int i,j,temp;
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(a[j]<a[i])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
}
int main()
{
int a[50],i,n;
cout<<"enter number of elements do u want to sort :";
cin>>n;
cout<<"enter elements to sort:";
for(i=0;i<n;i++)
{
cin>>a[i];
}
selectionsort(a,n);
cout<<"after sorting the elements are....\n";
for(i=0;i<n;i++)
{
cout<<a[i]<<"\n";
}
_getch();
return 0;
}