You are on page 1of 45

Program 1: Sorting and Searching an element in an array

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

//bubble sorting
void sort(int a[],int n)
{
int i,j,t;
for(i=0;i<n-1;i++)
{
for(j=0;j<n-i-1;j++)
{
if(a[j]>a[j+1])
{
t=a[j];
a[j]=a[j+1];
a[j+1]=t;
}
}
}
}
//binary searching
int search(int a[],int n,int ele)
{
int l,u,mid;
l=0;
u=n-1;
while(l<=u)
{
mid=(l+u)/2;
if(a[mid]==ele)
{
return 0;
}
if(a[mid]>ele)
{
u=mid-1;
}
else
{
l=mid+1;
}
}
return 1;
}
void display(int a[],int n)
{
int i;
cout<<"ELEMENTS : ";
for(i=0;i<n;i++)
{
cout<<a[i]<<" ";
}
cout<<endl;
}
int main()
{
clrscr();
int a[10];
int n,i,ele,check=0;
cout<<"Enter size of array"<<endl;
cin>>n;
for(i=0;i<n;i++)
{
cout<<"Enter element"<<endl;
cin>>a[i];
}
display(a,n);
cout<<"Enter element you want to search"<<endl;
cin>>ele;
sort(a,n);
check=search(a,n,ele);
if(check==0)
cout<<"ELEMENT FOUND"<<endl;
else
cout<<"ELEMENT NOT FOUND"<<endl;
getch();
return 0;
}//end of main
Program 2: Multiplying two 2 Dimension Arrays
#include<iostream.h>
#include<conio.h>
int main()
{
clrscr();
int n,i,j,a[50][50],b[50][50],c[50][50],x=0,y=0;
cout<<"ENTER SIZE OF 2 ARRAYS (SAME)"<<endl;
cin>>n;
cout<<"ENTER ELEMNTS OF 1st ARRAY"<<endl;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
cin>>a[i][j];
}
cout<<"ENTER ELEMNTS OF 2nd ARRAY"<<endl;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
cin>>b[i][j];
}
for(i=0;i<n;i++)
{
y=0;
for(j=0;j<n;j++)
{
for(int k=0;k<n;k++)
c[x][y]=c[x][y]+(a[i][k]*b[k][y]);
y++;
}
x++;
}
cout<<"Multiplied Arrays : "<<endl;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
cout<<c[i][j]<<" ";
cout<<endl;
}
getch();
return 0;
}//end of main
Program 3: Find a triplet in array which sums up to the entered number.
#include<iostream.h>
#include<conio.h>

//bubble sorting
void sort(int a[],int n)
{
int i,j,t;
for(i=0;i<n-1;i++)
{
for(j=0;j<n-i-1;j++)
{
if(a[j]>a[j+1])
{
t=a[j];
a[j]=a[j+1];
a[j+1]=t;
}
}
}

void sum(int a[],int n,int sum)


{
cout<<"The triplets whose sum is "<<sum<<" are :"<<endl;
int l,r,i; //l=left, r=right
for(i=0;i<n-1;i++)
{
l=i+1; //next index
r=n-1; //last index
while(l<r)
{
if((a[i])+a[l]+a[r]==sum) //checking sum
{
cout<<a[i]<<" "<<a[l]<<" "<<a[r]<<endl;
break;
}
else if((a[i]+a[l]+a[r])>sum)
{
r--;
}
else
{
l++;
}
}
}
}

int main()
{
clrscr();
int A[10],i,n,s;
cout<<"Enter size of array"<<endl;
cin>>n;
cout<<"Enter elements"<<endl;
for(i=0;i<n;i++)
{
cin>>A[i];
}
cout<<"Enter any sum"<<endl;
cin>>s;
sort(A,n);
sum(A,n,s);
getch();
return 0;
}//end of main
Program 4: Find Factorial of any number.
#include<iostream.h>
#include<conio.h>

int main()
{
clrscr();
int t,n,i,j,temp,m,x,a[30000];
cout<<"Enter Number"<<endl;
cin>>n;
a[0]=m=1;
temp = 0;
for(i=1;i<=n;i++)
{
for(j=0;j<m;j++)
{
x =(a[j]*i)+temp;
a[j]=x%10;
temp = x/10;
}
while(temp>0)
{
a[m]=temp%10;
temp = temp/10;
m++;
}
}
cout<<"Factorial : ";
for(i=m-1;i>=0;i--)
cout<<a[i];
cout<<endl;
getch();
return 0;
}
Program 5: Print Two Dimension array in spiral form
#include <iostream.h>
#include <conio.h>

void spiral(int m, int n, int a[][10])


{
cout<<"Elements in spiral form : ";
int i, k = 0, l = 0;
/* k - starting row index
m - ending row index
l - starting column index
n - ending column index
i - iterator
*/
while(k<m && l<n)
{
//Print the first row from the remaining rows
for (i=l;i<n;i++)
cout<<a[k][i]<<" ";
k++;
//Print the last column from the remaining columns
for (i=k;i<m;i++)
cout<<a[i][n-1]<<" ";
n--;
//Print the last row from the remaining rows
if (k<m)
{
for(i=n-1;i>=l;i--)
cout<<a[m-1][i]<<" ";
m--;
}
//Print the first column from the remaining columns
if (l<n)
{
for(i=m-1;i>=k;i--)
cout<<a[i][l]<<" ";
l++;
}
}
}

int main()
{
clrscr();
int a[10][10],i,n,j;
cout<<"Enter size of array"<<endl;
cin>>n;
cout<<"Enter elements"<<endl;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
cin>>a[i][j];
}
cout<<"Entered Array "<<endl;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
cout<<a[i][j]<<" ";
cout<<endl;
}
spiral(n,n,a);
getch();
return 0;
}//end of main
Program 6: Reverse any sentence
#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<stdio.h>

int main()
{
clrscr();
int i,l,sp,j;
char sen[50];
cout<<"ENTER ANY SENTENCE"<<endl;
gets(sen);
l=strlen(sen);
sp=l-1;
for(i=l-1;i>=0;i--)
{
if(sen[i]==' ' || i==0)
{
for(j=i;j<=sp;j++)
{
cout<<sen[j];
}
sp=i;
}
}
getch();
return 0;
}
Program 7: Reverse each word
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>

int main()
{
clrscr();
int l,i,a=0,x=0,y,strl;
char str[50],str1[50],str2[50];
cout<<"ENTER STRING"<<endl;
gets(str);
strl=strlen(str);
for(i=0;i<=strl;i++)
{
if(str[i]==' ' || str[i]=='\0')
{
str1[a]='\0';
strrev(str1);
l=strlen(str1);
for(y=0;y<l;y++)
{
str2[x]=str1[y];
x++;
}
str2[x]=' ';
x++;
a=0;
}
else
{
str1[a]=str[i];
a++;
}
}
str2[x]='\0';
cout<<str2;
getch();
return 0;
}
Program 8: Longest Palindrome word in a sentence
#include <iostream.h>
#include <stdio.h>
#include <string.h>
#include <conio.h>
int palin(char str[],int l){
char str1[100];
strcpy(str1,str);
strrev(str1);
if(strcmpi(str1,str)==0)
return 1;
else
return 0;
}
void main(){
clrscr();
int l,len,i,k,lar=0,p1,p2=-1,sp,lp;
char str[100],word[50],larpalin[50];
cout<<"ENTER STRING"<<endl;
gets(str);
l=strlen(str);
for(i=0;i<=l;i++){
if(str[i]==' ' || str[i]=='\0'){
p1=i;
len=p1-p2-1;
if(len>lar){
int a=0;
for(k=p2+1;k<p1;k++){
word[a]=str[k];
a++;
}
word[a]='\0';
if(palin(word,len)==1){
strcpy(larpalin,word);
lar=len;
sp=p2;
lp=p1;
}
}
p2=i;
}
}
cout<<"LARGEST PALINDROME WORD = "<<larpalin<<endl;
getch();
}
Program 9: Age calculator
#include<iostream.h>
#include<conio.h>
struct date
{
int d;
int m;
int y;
};
void calc(date d1,date d2)
{
int days,months,year;
days=d2.d-d1.d;
months=d2.m-d1.m;
year=d2.y-d1.y;
cout<<year<<" years "<<months<<" months "<<days<<" days "<<endl;
}
int main()
{
clrscr();
int days,months,year;
date d1,d2;
cout<<"ENTER DATE OF BIRTH (dd mm yy)"<<endl;
cin>>d1.d>>d1.m>>d1.y;
cout<<"ENTER ANY DATE (dd mm yy)"<<endl;
cin>>d2.d>>d2.m>>d2.y;
if(d2.d<=d1.d && d2.m>d1.m)
{
if(d2.m==1 ||d2.m==5 ||d2.m==7 ||d2.m==8 ||d2.m==10 ||d2.m==12)
d2.d+=30;
if(d2.m==4 ||d2.m==6 ||d2.m==9 ||d2.m==11 ||d2.m==2)
d2.d+=31;
if(d2.m==3)
{
if(d2.y%4==0)
d2.d+=29;
else
d2.d+=28;
}
d2.m-=1;
calc(d1,d2);
}
else if(d2.m<=d1.m && d2.d>d1.d)
{
d2.m+=12;
d2.y-=1;
calc(d1,d2);
}
else if(d2.d<=d1.d && d2.m<=d1.m)
{
if(d2.m==1 ||d2.m==5 ||d2.m==7 ||d2.m==8 ||d2.m==10 ||d2.m==12)
d2.d+=30;
if(d2.m==4 ||d2.m==6 ||d2.m==9 ||d2.m==11 ||d2.m==2)
d2.d+=31;
if(d2.m==3)
{
if(d2.y%4==0)
d2.d+=29;
else
d2.d+=28;
}
d2.m+=11;
d2.y-=1;
calc(d1,d2);
}
else
{
calc(d1,d2);
}
getch();
return 0;
}
Program 10: Different types of numbers
#include<iostream.h>
#include<conio.h>
#include<math.h>

void palin(int n)
{
int m,x,rev=0;
m=n;
while(n>0)
{
x=n%10;
n=n/10;
rev=(rev*10)+x;
}
if(rev==m)
{
cout<<"PALINDROME"<<endl;
}
else
{
cout<<"NOT PALINDROME"<<endl;
}
}

void neon(int n)
{
long sq,x,s=0;
sq=n*n;
while(sq>0)
{
x=sq%10;
sq=sq/10;
s=s+x;
}
if(s==n)
{
cout<<"NEON NUMBER"<<endl;
}
else
{
cout<<"NOT NEON NUMBER"<<endl;
}
}

void special(int n)
{
int s=0,f=1,m,x,j;
m=n;
while(n>0)
{
f=1;
x=n%10;
n=n/10;
for(j=1;j<=x;j++)
f=f*j;
s=s+f;
}
if(s==m)
cout<<"SPECIAL NUMBER"<<endl;
else
cout<<"NOT SPECIAL NUMBER"<<endl;
}

void arms(int n)
{
long m,x=0,s=0,sum=0;
m=n;
while(n>0)
{
n=n/10;
s++;
}
n=m;
while(n>0)
{
x=n%10;
n=n/10;
sum=sum+((int)(pow(x,s)));
}
if(sum==m)
{
cout<<"ARMSTRONG NUMBER"<<endl;
}
else
{
cout<<"NOT ARMSTRONG NUMBER"<<endl;
}
}

void Tprime(int n)
{
int m,check=0,check1=0,rev=0,i,x;
for(i=1;i<=n;i++)
{
if(n%i==0)
check++;
}
if(check==2)
{
m=n;
while(m>0)
{
x=m%10;
m=m/10;
rev=(rev*10)+x;
}
for(i=1;i<=rev;i++)
{
if(rev%i==0)
check1++;
}
if(check1==2)
cout<<"TWISTED PRIME"<<endl;
else
cout<<"NOT TWISTED PRIME"<<endl;
}
else
cout<<"NOT TWISTED PRIME"<<endl;
}

int main()
{
clrscr();
int ch,no;
cout<<"1 - Palindrome Number"<<endl;
cout<<"2 - Neon Number"<<endl;
cout<<"3 - Special Number"<<endl;
cout<<"4 - Armstrong Number"<<endl;
cout<<"5 - Twisted Prime Number"<<endl;
cout<<"Enter choice : "<<endl;
cin>>ch;
cout<<"Enter number"<<endl;
cin>>no;
switch(ch)
{
case 1:
palin(no);
break;
case 2:
neon(no);
break;
case 3:
special(no);
break;
case 4:
arms(no);
break;
case 5:
Tprime(no);
break;
default:
cout<<"Enter correct choice"<<endl;
break;
}
getch();
return 0;
}
Program 11: To find the frequency of any word in string
#include<iostream.h>
#include<stdio.h>
#include<string.h>
#include<conio.h>
int check(char str1[],char str2[],int len)
{
int c=0,t;
for(int i=0;i<len;i++)
{
if(str1[i]!=str2[i])
{
c=1;
break;
}
}
if(c==1)
return 0;
else
return 1;
}
void main()
{
clrscr();
int l,len,len1,i,k,freq=0,p1,p2=-1;
char str[100],word[50],word1[50];
cout<<"ENTER STRING"<<endl;
gets(str);
cout<<"ENTER WORD FOR FINDING FREQUENCY"<<endl;
gets(word);
l=strlen(str);
len=strlen(word);
for(i=0;i<=l;i++)
{
if(str[i]==' ' || str[i]=='\0')
{
p1=i;
len1=p1-p2-1;
if(len1==len)
{
int a=0;
for(k=p2+1;k<p1;k++)
{
word1[a]=str[k];
a++;
}
word1[a]='\0';
if(check(word1,word,len)==1)
{
freq++;
}
}
p2=i;
}
}
cout<<"FREQUENCY OF "<<word<<" = "<<freq<<endl;
getch();
}
Program 12: Convert Binary to Decimal
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
long m,n,x,s=0,p=1;
cout<<"ENTER ANY NUMBER"<<endl;
cin>>n;
m=n;
while(n>0)
{
x=n%10;
n=n/10;
s=s+(p*x);
p=p*2;
}
cout<<m<<" in binary is "<<s<<endl;
getch();
}

Program 13: Convert Decimal to Binary


#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
long m,n,x,s=0,p=1;
cout<<"ENTER ANY NUMBER"<<endl;
cin>>n;
m=n;
while(n>0)
{
x=n%2;
n=n/2;
s=s+(p*x);
p=p*10;
}
cout<<m<<" in binary is "<<s<<endl;
getch();
}
Program 14: Reading and writing Class objects in File Handling
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
#include<fstream.h>

class student
{
int roll;
char name[15],f_name[20];
public:
void put();
void get();
int rollret();
};
student s;
int student::rollret()
{
return roll;
}
void student::put()
{
fstream file;
cout<<"Enter Details "<<endl;
cout<<"Enter roll no: ";
cin>>roll;
cout<<"Enter name: ";
gets(name);
cout<<"Enter father name: ";
gets(f_name);
file.open("stu.dat",ios::out|ios::app);
file.write((char *)this,sizeof(student));
file.close();
}
void student::get()
{
int temp;
fstream file;
student s;
cout<<"Enter roll no: ";
cin>>temp;
file.open("stu.dat",ios::in);
file.seekg(0,ios::beg);
while(file.read((char *)&s,sizeof(s)));
{
if(s.rollret()==temp)
{
cout<<"roll no. "<<roll<<endl;
cout<<"stu name: "<<name<<endl;
cout<<"father name: "<<f_name<<endl;
}
else
{
cout<<"Not Found"<<endl;
}
}
file.close();
}
void main()
{
clrscr();
int i;
do
{
cout<<"1 - Read \t 2 - Write \t 3 - Exit ";
cin>>i;
switch(i)
{
case 1:
s.get();
break;
case 2:
s.put();
break;
case 3:
exit(0);
default:
cout<<"wrong choice ";
}
}while(i!=3);
}
Program 15: Convert string to corresponding Binary code
#include<iostream.h>
#include<string.h>
#include<stdio.h>
#include<conio.h>
void convert(int n)
{
long x,p=1,m=n;
long s=0;
while(n>0)
{
x=n%2;
n=n/2;
s=s+(p*x);
p=p*10;
}
cout<<s<<" ";
}
int main()
{
long l,chnum,i; char ch,word[50];
cout<<"ENTER ANY WORD"<<endl;
gets(word);
l=strlen(word);
cout<<"STRING IN BINARY FORM"<<endl;
for(i=0;i<l;i++)
{
ch=word[i];
chnum=(int)(ch);
convert(chnum);
}
getch();
}
Program 16: Insertion and Deletion in Linked Lists
#include<iostream.h>
#include<process.h>
#include<conio.h>

struct node
{
int data;
node *next;
};
node *save,*ptr;
class LL
{
private:
node *head,*tail;
public:
LL()
{
head=NULL;
tail=NULL;
}
void add_node_beg(int i)
{
node *tmp=new node;
tmp->data=i;
tmp->next=NULL;
if(head==NULL)
{
head=tmp;
tail=tmp;
}
else
{
save=head;
head=tmp;
tmp->next=save;
}
}
add_node_end(int i)
{
node *tmp=new node;
tmp->data=i;
tmp->next=NULL;
if(head==NULL)
{
head=tmp;
tail=tmp;
}
else
{
tail->next=tmp;
tail=tmp;
}
}
void del_node()
{
if(head==NULL)
{
cout<<"NO NODE TO DELETE"<<endl;
}
else
{
ptr=head;
head=head->next;
delete ptr;
}
}
void display()
{
node *tmp;
tmp=head;
cout<<"Elements : ";
while(tmp!=NULL)
{
cout<<tmp->data<<" ";
tmp=tmp->next;
}
cout<<endl;
}
};
int main()
{
clrscr();
int i,ch;
LL a;
begin:
cout<<"1 - Insert at the start "<<endl;
cout<<"2 - Insert at the end "<<endl;
cout<<"3 - Delete from the beginning "<<endl;
cout<<"4 - Display "<<endl;
cout<<"5 - Exit"<<endl;
cout<<"Enter choice : ";
cin>>ch;
while(ch!=5)
{
switch(ch)
{
case 1:
cout<<"Enter Number"<<endl;
cin>>i;
a.add_node_beg(i);
goto begin;
case 2:
cout<<"Enter Number"<<endl;
cin>>i;
a.add_node_end(i);
goto begin;
case 3:
a.del_node();
goto begin;
case 4:
a.display();
goto begin;
case 5:
exit(0);
default:
goto begin;
}
}
getch();
return 0;
}
Program 17: Finding nth element in a Linked List from end
#include<iostream.h>
#include<conio.h>
#include<process.h>
struct node
{
int data;
node *next;
};
class LL
{
node *head,*tail;
public:
LL()
{
head=NULL;
tail=NULL;
}
void add(int x)
{
node *temp=new node;
temp->data=x;
temp->next=NULL;
if(head==NULL)
{
head=temp;
tail=temp;
}
else
{
tail->next=temp;
tail=temp;
}
}
void display()
{
node *temp;
temp=head;
while(temp!=NULL)
{
cout<<temp->data<<" ";
temp=temp->next;
}
cout<<endl;
}
void n_node(int k)
{
int count=0,pos=1;
node *temp;
temp=head;
while(temp!=NULL)
{
count++;
temp=temp->next;
}
temp=head;
while(pos!=(count-k+1))
{
temp=temp->next;
pos++;
}
cout<<temp->data<<endl;
}
};
int main()
{
clrscr();
LL a;
int i,n;
for(int j=0;j<10;j++)
{
cout<<"Enter number"<<endl;
cin>>i;
a.add(i);
}
cout<<"Enter position from back "<<endl;
cin>>n;
cout<<"Elements : "<<endl;
a.display();
a.n_node(n);
getch();
return 0;
}
Program 18: Reversing the Linked List
#include<iostream.h>
#include<conio.h>
struct node
{
int data;
node *next;
};
class LL
{
private:
node *head,*tail;
public:
LL()
{
head=NULL;
tail=NULL;
}
void add(int i)
{
node *temp=new node;
temp->data=i;
temp->next=NULL;
if(head==NULL)
{
head=temp;
tail=temp;
}
else
{
tail->next=temp;
tail=temp;
}
}
void display()
{
node *temp;
temp=head;
while(temp!=NULL)
{
cout<<temp->data<<"->";
temp=temp->next;
}
cout<<"NULL"<<endl;
}
void reverse()
{
node *currnode=head,*prevnode=NULL,*nextnode=NULL;
while(currnode!=NULL)
{
nextnode=currnode->next;
currnode->next=prevnode;
prevnode=currnode;
currnode=nextnode;
}
head=prevnode;
}
};
int main()
{
clrscr();
LL a;
int n;
for(int j=0;j<10;j++)
{
cout<<"Enter number"<<endl;
cin>>n;
a.add(n);
}
a.display();
a.reverse();
cout<<"LL after reversing"<<endl;
a.display();
getch();
return 0;
}
Program 19: Implementation of Stacks using Array
#include<iostream.h>
#include<conio.h>
#define SIZE 10
int STACK[SIZE],TOP;
//stack initialization
void initStack()
{
TOP=-1;
}
//check it is empty or not
int isEmpty(){
if(TOP==-1)
return 1;
else
return 0;
}
//check stack is full or not
int isFull()
{
if(TOP==SIZE-1)
return 1;
else
return 0;
}
void push(int num)
{
if(isFull())
{
cout<<"STACK is FULL."<<endl;
return;
}
++TOP;
STACK[TOP]=num;
cout<<num<<" has been inserted."<<endl;
}
void display()
{
int i;
if(isEmpty())
{
cout<<"STACK is EMPTY."<<endl;
return;
}
for(i=TOP;i>=0;i--)
{
cout<<STACK[i]<<" ";
}
cout<<endl;
}
//pop - to remove item
void pop()
{
int temp;
if(isEmpty())
{
cout<<"STACK is EMPTY."<<endl;
return;
}
temp=STACK[TOP];
TOP--;
cout<<temp<<" has been deleted."<<endl;
}
int main()
{
clrscr();
int num;
initStack();
char ch;
do
{
int a;
cout<<"1 - push \t 2 - pop \t 3 - display\n";
cout<<"Please enter your choice: ";
cin>>a;
switch(a)
{
case 1:
cout<<"Enter an Integer Number: ";
cin>>num;
push(num);
break;
case 2:
pop();
break;
case 3:
display();
break;
default :
cout<<"An Invalid Choice!!!"<<endl;
}
cout<<"Do you want to continue ? ";
cin>>ch;
}while(ch=='Y'||ch=='y');
getch();
return 0;
}
Program 20: Implementation of Stacks using Linked Lists
#include <iostream.h>
#include <conio.h>
#include <string.h>

struct Node
{
int stu_no;
char stu_name[50];
int p;
Node *next;
};

Node *top;

class stack
{
public:
void push(int n,char name[],int perc);
void pop();
void display();
};

void stack :: push(int n,char name[],int perc)


{
struct Node *newNode=new Node;
//fill data part
newNode->stu_no=n;
newNode->p=perc;
strcpy(newNode->stu_name,name);
//link part
newNode->next=top;
//make newnode as top/head
top=newNode;
}
void stack ::pop()
{
if(top==NULL)
{
cout<<"List is empty!"<<endl;
return;
}
cout<<top->stu_name<<" is removed."<<endl;
top=top->next;
}
void stack:: display()
{
if(top==NULL)
{
cout<<"List is empty!"<<endl;
return;
}
struct Node *temp=top;
while(temp!=NULL)
{
cout<<temp->stu_no<<" ";
cout<<temp->stu_name<<" ";
cout<<temp->p<<" ";
cout<<endl;
temp=temp->next;
}
cout<<endl;
}
int main()
{
clrscr();
stack s;
char ch;
do
{
int n;
cout<<"ENTER CHOICE\n"<<"1.Push\n"<<"2.Pop\n"<<"3.Display\n";
cout<<"Make a choice: ";
cin>>n;
switch(n)
{
case 1:
Node n;
cout<<"Enter details of the element to be pushed : \n";
cout<<"Roll Number : ";
cin>>n.stu_no;
cout<<"Enter Name: ";
cin.ignore(1);
cin.getline(n.stu_name,50);
cout<<"Enter Percentage: ";
cin>>n.p;
//push data into the stack
s.push(n.stu_no,n.stu_name,n.p);
break;
case 2 :
//pop data from stack
s.pop();
break;
case 3 :
//display data
s.display();
break;
default :
cout<<"Invalid Choice\n";
}
cout<<"Do you want to continue ? : ";
cin>>ch;
}while(ch=='Y'||ch=='y');
getch();
return 0;
}
Program 21: Checking Palindrome word using Stacks
#include<iostream.h>
#include<string.h>
#include<ctype.h>
#include<conio.h>
#define MAX 50
class Stack
{
private:
char data[MAX],str[MAX];
int top,length,count;
void pushData(char);
char popData();
public:
Stack()
{
top=-1;
length=0;
count=0;
}
void getString();
void checkPalindrome();
void extractString();
};
int main()
{
clrscr();
Stack obj;
obj.getString();
obj.extractString();
obj.checkPalindrome();
getch();
return 0;
}
void Stack::getString() {
cout<<"Enter a String : ";
cin.getline(str,MAX);

length=strlen(str);
}
void Stack::extractString() {
char temp[MAX];
int i,j;
for(i=0; i<length; i++) {
temp[i]=str[i];
}
j=0;
for(i=0; i<length; i++ ) {
if(isalpha(temp[i])) {
str[j]=tolower(temp[i]);
j++;
}
}
length=j; //update length with new str length
}
void Stack::checkPalindrome()
{
int i;
for(i=0; i<length; i++)
pushData(str[i]);

for(i=0; i<length; i++)


{
if(str[i]==popData())
count++;
}
if(count==length)
{
cout<<"Entered string is a Palindrome. \n";
}
else
cout<<"Entered string is not a Palindrome. \n";
}
void Stack::pushData(char temp)
{
if(top==MAX-1)
{
cout<<"\n Stack Overflow!!!";
return;
}
top++;
data[top]=temp;
}
char Stack::popData()
{
if(top==-1)
{
cout<<"\n Stack Underflow!!!";
char ch='\n';
return ch;
}
char temp=data[top];
top--;
return temp;
}
Program 22: Implementation of Queues using Array
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>

class queue
{
int queue1[5];
int rear,front;
public:
queue()
{
rear=-1;
front=-1;
}
void insert(int x)
{
if(rear > 4)
{
cout <<"Queue over flow "<<endl;
front=rear=-1;
return;
}
queue1[++rear]=x;
cout <<"Inserted "<<x<<endl;
}
void delet()
{
if(front==rear)
{
cout <<"Queue under flow "<<endl;
return;
}
cout <<"Deleted "<<queue1[++front]<<endl;
}
void display()
{
int i;
if(rear==front)
{
cout <<"Queue empty"<<endl;
return;
}
for(i=front+1;i<=rear;i++)
cout <<queue1[i]<<" ";
cout<<endl;
}
};

void main()
{
clrscr();
int ch;
queue qu;
while(1)
{
cout<<"1 - insert \t 2 - delete \t 3 - display \t 4 - exit"<<endl;
cout<<"Enter choice : ";
cin >> ch;
switch(ch)
{
case 1:
cout <<"Enter the element : ";
cin >> ch;
qu.insert(ch);
break;
case 2:
qu.delet();
break;
case 3:
qu.display();
break;
case 4:
exit(0);
}
}
}
Program 23: Implementation of Queues using Linked Lists
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>

struct node
{
int data;
struct node *next;
}*front=NULL,*rear,*temp;

void ins()
{
temp=new node;
cout<<"Enter data : ";
cin>>temp->data;
temp->next=NULL;

if(front==NULL)
front=rear=temp;
else
{
rear->next=temp;
rear=temp;
}
}

void del()
{
if(front==NULL)
cout<<"Queue is empty"<<endl;
else
{
temp=front;
front=front->next;
cout<<"Deleted node is "<<temp->data<<endl;
delete(temp);
}
}

void dis()
{
if(front==NULL)
cout<<"Queue is empty"<<endl;
else
{
temp=front;
while(temp!=NULL)
{
cout<<temp->data<<"->";
temp=temp->next;
}
cout<<"NULL"<<endl;
}
}

int main()
{
clrscr();
int ch;
while(1)
{
cout<<"1 - Insert \t 2 - Delete \t 3 - Display \t 4 - Exit"<<endl;
cout<<"Enter choice : ";
cin>>ch;
switch(ch)
{
case 1: ins();
break;
case 2: del();
break;
case 3: dis();
break;
case 4: exit(0);
break;
default: cout<<"Wrong Choice!!!";
}
}
getch();
return 0;
}
Program 24: Implementation of Circular Queues using Array
#include<iostream.h>
#include<conio.h>
#include<process.h>
#define MAX 5
class Circular_Queue
{
private:
int *cqueue_arr;
int front, rear;
public:
Circular_Queue()
{
cqueue_arr = new int [MAX];
rear = front = -1;
}
void insert(int item)
{
if ((front == 0 && rear == MAX-1) || (front == rear+1))
{
cout<<"Queue Overflow \n";
return;
}
if (front == -1)
{
front = 0;
rear = 0;
}
else
{
if (rear == MAX - 1)
rear = 0;
else
rear = rear + 1;
}
cqueue_arr[rear] = item ;
}
void del()
{
if (front == -1)
{
cout<<"Queue Underflow\n";
return ;
}
cout<<"Element deleted from queue is : "<<cqueue_arr[front]<<endl;
if (front == rear)
{
front = -1;
rear = -1;
}
else
{
if (front == MAX - 1)
front = 0;
else
front = front + 1;
}
}
void display()
{
int front_pos = front, rear_pos = rear;
if (front == -1)
{
cout<<"Queue is empty\n";
return;
}
cout<<"Queue elements :\n";
if (front_pos <= rear_pos)
{
while (front_pos <= rear_pos)
{
cout<<cqueue_arr[front_pos]<<" ";
front_pos++;
}
}
else
{
while (front_pos <= MAX - 1)
{
cout<<cqueue_arr[front_pos]<<" ";
front_pos++;
}
front_pos = 0;
while (front_pos <= rear_pos)
{
cout<<cqueue_arr[front_pos]<<" ";
front_pos++;
}
}
cout<<endl;
}
};
int main()
{
clrscr();
int choice, item;
Circular_Queue cq;
do
{
cout<<"1 - Insert \t 2 - Delete \t 3 - Display \t 4 - Quit"<<endl;
cout<<"Enter your choice : ";
cin>>choice;
switch(choice)
{
case 1:
cout<<"Input the element for insertion in queue : ";
cin>>item;
cq.insert(item);
break;
case 2:
cq.del();
break;
case 3:
cq.display();
break;
case 4:
exit(0);
break;
default:
cout<<"Wrong choice\n";
}
}while(choice != 4);
return 0;
}

You might also like