Social Icons

banner image

40+ programs in data structure in c

DATA STRUCTURE FILE

                                                      PROGRAM NO.1
WRITE A PROGRAM TO FIND THE FACTORIAL OF THE GIVEN NUMBER.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int n,i,f=1;
cout<<"enter number: ";
cin>>n;
for(i=1;i<=n;i++)
{
f*=i;}
cout<<"factorial is: ";
cout<<f;
getch();
}
:-

PROGRAM NO.2
WRITE A PROGRAM TO FIND THE REVERSE OF A NUMBER.
#include<iostream.h>
#include<conio.h>
void main(){
clrscr();
int n,rev=0,digit;
cout<<"enter number: ";
cin>>n;
while(n!=0)
{
digit=n%10;
rev=rev*10+digit;
n/=10;}
cout<<"reverse is: "<<rev;
getch();}
:-

.
PROGRAM NO.3
WRITE A PROGRAM TO CONVET THE TEMPERATURES FROM CELSIUS TO FARENHIET AND FROM FARENHIET  TO CELSIUS.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
float f, c,temp,a,b;
int ch;
cout<<"ANSHUL"<<endl;
cout<<"enter your choice from:-"<<endl;
cout<<"1.convert celsius to farenhiet"<<endl<<"2.convet farenhiet to celsius"<<endl;
cin>>ch;
switch(ch)
{
case 1:
cout<<"enter temp in celsius=";
cin>>c;
f=(0.56*(c+32));
cout<<"the temp in faren="<<f;
break;
case 2:
cout<<"enter the temp in faren=";
cin>>f;
c=(1.8*(f-32));
cout<<"the temp in celsius="<<c;
break;
default:
cout<<"invalid choice";
}
getch();
}
:-





PROGRAM NO.4
WRITE A PROGRAM TO FIND THAT NUMER IS EVEN OR ODD.
#include<iostream.h>
#include<conio.h>
   int main()
   {      cout<<"WAP to check weather the no. is even or odd"<<endl;
  int a;
 cout<<"enter the value of a";
 cin>>a;
 if(a%2==0)
 cout<<"the no.is even";
 else
 cout<<"the no. is odd";
 return(0);
 }
:-

PROGRAM NO.5
WRITE A PROGRAM TO FIND GREATEST OF 3 NUMBERS.
#include<iostream.h>
#include<conio.h>
void main()
{clrscr();
int a,b,c;
cout<<"enter value of a,b and c: ";
cin>>a>>b>>c;  
if(a>b){
if(a>c){cout<<"a is greater";}
else
cout<<"c is greater";}
else if(b>a)                                              
{if(b>c)
cout<<"b is greater";
else
cout<<"c is greater";}
getch();}
:-
PROGRAM NO.6
WRITE A PROGRAM TO SWAP 2 NUMBERS USING THIRD VARIABLE.
 #include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a,b,c;
cout<<"enter your a and b"<<endl;
cin>>a>>b;
c=a;
a=b;
b=c;
cout<<"the value of a ="<<a<<endl<<"the value of b="<<b;
getch();
}
:-


PROGRAM NO.7
WRITE A PROGRAM TO FIND THAT NUMER IS PRIME OR NOT.
#include<iostream.h>
#include<conio.h>
Void main()
{Clrscr();
Int n,i,prime=1;
Cout<<”enter your no”;
Cin>>n;
For(i=0;i<=4;i++){
If(n%i==0)
Prime=0;
Break;}
Else
If(prime==1)
Cout<<”no is prime”;
Else
Cout<<”no is not prime”;
Getch();}
:-                    
PROGRAM NO.8
WRITE A PROGRAM  TO  ENTER ELEMENTS IN ARRAY.
#include<iostream.h>
#include<conio.h>
void main()
{clrscr();
int n[5],i;
cout<<"enter your elements in array:-"<<endl;
for(i=0;i<=4;i++){
cin>>n[i];}
cout<<"first num="<<n[0]<<endl;
cout<<"sec num="<<n[1]<<endl;
cout<<"third num="<<n[2]<<endl;
cout<<"fourth num="<<n[3]<<endl;
cout<<"fifth num="<<n[4];
getch();}
:-


PROGRAM NO.9
 WRITE A PROGRAM TO  FIND  THE GREATEST  AND  SMALLEST NO AMONG THE ELEMENTS OF THE ARRAY.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
cout<<"ANSHUL SUNEJA";
int n[5],i,s,g;
for(i=0;i<=4;i++)
{
cout<<"enter the elements of array:-";
cin>>n[i];
}
g=n[0];s=n[0];
for(i=0;i<=4;i++)
{
if(n[i]>g)
g=n[i];
if(n[i]<s)
s=n[i];
}
cout<<"the greatest no is="<<g<<endl ;
cout<<"the smallest no is="<<s;
getch();
}
:-
PROGRAM NO.10

WRITE A PROGRAM TO FIND NO. IS PALINDROME OR NOT.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int i, p,rev=0;
cout<<endl<<" enter hte no ";
cin>>p;
int num=0,key=p;
for(i=1;p!=0;i++)
{
num=p%10;
p=p/10;
rev=(rev*10)+num;
}
if(rev==key)
{
cout<<key<<" no is palindrome"<<endl;
}
else
{
cout<<" no is not palindrome";
}
getch();
}
:-




PROGRAM NO.11
WRITE A PROGRAM TO PERFORM THE ADDITION OF 2D ARRAY.
#include<iostream>
using namespace std;
#include<conio.h>
main()
{
int a[2][2],b[2][2],c[2][2],i,j;
cout<<"ANSHUL SUNEJA"<<endl;
cout<<"enter the elements of first array="<<endl;
for(i=0;i<=1;i++)
{
                        for(j=0;j<=1;j++)
                        {
                                    cin>>a[i][j];
                        }
            }
            cout<<"elements of first array are="<<endl;
            for(i=0;i<=1;i++)
            {
                        for(j=0;j<=1;j++)
                        {
                                    cout<<a[i][j];
                                    cout<<"\t";
                        }
                        cout<<"\n";
                       
            }
cout<<"enter the elements of second array="<<endl;
            for(i=0;i<=1;i++)
            {
                        for(j=0;j<=1;j++)
                        {
                                    cin>>b[i][j];
                        }
            }
            cout<<"elements of second array are="<<endl;
            for(i=0;i<=1;i++)
            {
                        for(j=0;j<=1;j++)
                        {
                                    cout<<b[i][j];   cout<<"\t";
                        }
            cout<<"\n";
            }
            cout<<"sum of both the matrix are="<<endl;
            for(i=0;i<=1;i++)
            {
                        for(j=0;j<=1;j++)
                        {
                        c[i][j]=a[i][j]+b[i][j];
                        }
            }
                        for(i=0;i<=1;i++)
            {
                        for(j=0;j<=1;j++)
                        {
                                    cout<<c[i][j];cout<<"\t";
                        }
                        cout<<"\n";
            }
            getch();
}
:-


PROGRAM NO.12
WRITE A PROGRAM TO PERFORM MULTIPLICATION OF TWO ARRAYS.
#include<iostream>
using namespace std;
#include<conio.h>
main()
{
            int a[2][2],b[2][2],c[2][2],i,j,k;
            cout<<"ANSHUL SUNEJA"<<endl;
            cout<<"enter the elements of first array="<<endl;
            for(i=0;i<=1;i++)
            {
                        for(j=0;j<=1;j++)
                        {
                                    cin>>a[i][j];
                        }
            }
            cout<<"elements of first array are="<<endl;
            for(i=0;i<=1;i++)
            {
                        for(j=0;j<=1;j++)
                        {
                                    cout<<a[i][j];
                                    cout<<"\t";
                        }
                        cout<<"\n";
                       
            }
cout<<"enter the elements of second array="<<endl;
            for(i=0;i<=1;i++)
            {
                        for(j=0;j<=1;j++)
                        {
                                    cin>>b[i][j];
                        }
            }
            cout<<"elements of second array are="<<endl;
            for(i=0;i<=1;i++)
            {
                        for(j=0;j<=1;j++)
                        {
                                    cout<<b[i][j];   cout<<"\t";
                        }
            cout<<"\n";
            }
            cout<<"multiplication of two array="<<endl;
           
            for(i=0;i<=1;i++)
            {
                        for(j=0;j<=1;j++)
                        {
                                    for(k=0;k<=1;k++)
                                    {
                                                c[i][j]=a[i][k]*b[k][j]+a[k][j]*b[i][k];
                                    }
                        }
            }
                        for(i=0;i<=1;i++)
            {
                        for(j=0;j<=1;j++)
                                    {cout<<c[i][j];
                                                cout<<"\t";}
                                    cout<<"\n";}
            getch();}
:-
PROGRAM NO.13
WRITE A PROGRAM TO PERFORM A LINEAR SEARCH.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int data[10],loc=0,i,n;
cout<<"Enter 10 elements for searching:\n";
for(i=1;i<=10;i++)
{
cin>>data[i];
}
cout<<"\nEnter the number to be searched:";
cin>>n;
for(i=1;i<=10;i++)
{
if(data[i]==n)
{loc=i;
break;}
else
continue;
}
if(loc==0)
cout<<"\nSEARCH UNSUCCESSFUL!!";
else
cout<<"\nSEARCHED ELEMENT "<<n<<" IS AT THE LOCATION "<<loc;
getch();
}
:-







PROGRAM NO.14
WRITE A PROGRAM TO DISPLAY A 2D ARRAY.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a[50][50],i,j,m,n;
cout<<" Enter the no of rows ";
cin>>m;
cout<<" Enter the columns ";
cin>>n;
cout<<" enter elements of array are ";
for (i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
cin>>a[i][j];
}}
cout<<endl<<" elements of array are "<<endl;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
cout<<a[i][j]<<" " ;
}
cout<<endl;
}
getch();
}
:-
PROGRAM NO.15

WRITE A PROGRAM TO PERFORM  A BUBBLE SORT.
#include<iostream.h>
#include<conio.h>
void main()
{clrscr();
int a[100],i,j,n,temp;
cout<<" enter the no o elements in array ";
cin>>n;
cout<<" elemets of array are ";
for(i=1;i<=n;i++)
{cin>>a[i];}
cout<<endl<<"elements of array are ";
for(i=1;i<=n;i++)
{
cout<<a[i];
}
for(i=1;i<=n-1;i++)
{
for(j=1;j<=n-1;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}}}
cout<<endl<<"aftr sorting  array is ";
for(i=1;i<=n;i++)
{
cout<<a[i]<<endl;
}
getch();
}
:-
 




PROGRAM NO.16
PROGRAM NO.16

WRITE A PROGRAM TO DELETE AN ELEMENT FROM THE ARRAY.
#include<iostream.h>
#include<conio.h>
void main()
{ clrscr();
int a[10],n,x,loc;
cout<<"Enter the number of elements you want to enter: ";
cin>>n;
cout<<"\n";
cout<<"enter elements: "<<"\n";
for( int i=1; i<=n; i++ )
cin>>a[i];
cout<<"\n";
cout<<"enter the element which you want to delete: ";
cin>>x;
cout<<"\n";
cout<<"enter the position from where to delete: ";
cin>>loc;
for(int j=loc; j<=n-1; j++ )
{
a[j] = a[j+1];
}
n=n-1;
cout<<"Modified array: "<<"\n";
for (i=1; i<=n ; i++ )
cout<<a[i]<<"\n";
getch();
 } 
PROGRAM NO.17

WRITE A PROGRAM TO ENTER TEMPERATURE OF TWO CITIES OF A WEEK IN A 2D ARRAY.

#include<conio.h>
#include<iostream.h>
void main ()
{
clrscr();
int i;
int city1[7],city2[7];
char day[7][15]={"Monday   ","Tuesday  ","Wednesday","Thursday ","Friday   ","Saturday ","Sunday   "};
cout<<"Enter the temperature of the first city on : \n";
for(i=0;i<7;i++)
{
cout<<day[i]<<" :\t";
cin>>city1[i];
}
cout<<"Enter the temperature of the second city on : \n";
for(i=0;i<7;i++)
{
cout<<day[i]<<" :\t";
cin>>city2[i];
}
cout<<"\n\nDay\t\t  City1   City2\n";
for(i=0;i<7;i++)
{
cout<<day[i]<<"\t\t  "<<city1[i]<<"\t"<<city2[i]<<endl;
}
getch();
}
PROGRAM NO.18
WRITE A PROGRAM TO TRANSPOSE A 2D ARRAY.
#include<iostream>
using namespace std;
#include<conio.h>
main()
{
            int a[2][2],b[2][2],i,j;
            cout<<"ANSHUL SUNEJA"<<endl;
            cout<<"enter the elements of first array="<<endl;
            for(i=0;i<=1;i++)
            {for(j=0;j<=1;j++){     cin>>a[i][j];}}
            cout<<"elements of first array are="<<endl;
            for(i=0;i<=1;i++)
            {
                        for(j=0;j<=1;j++)
                        {cout<<a[i][j];
                                    cout<<"\t";}
                        cout<<"\n";}
for(i=0;i<=1;i++)
            {for(j=0;j<=1;j++){
                                    b[i][j]=a[j][i];}
            }
                        cout<<"transpose of matrix is="<<endl;
                                    for(i=0;i<=1;i++)
            {
                        for(j=0;j<=1;j++)
                        {
                                    cout<<b[i][j];
                                                cout<<"\t";
                        }
                                    cout<<"\n";
            }
            getch();
}
:-
 





PROGRAM NO.19
WRITE A PROGRAM TO INSERT AN ELEMENT IN THE ARRAY.
#include<iostream,h>
#include<conio.h>
main()
{          int a[10],n,x,i,loc,item;
            cout<<"ANSHUL SUNEJA"<<endl;
cout<<"Enter the number of elements you want to enter: ";
cin>>n;
cout<<"\n";
cout<<"enter elements: "<<"\n";
for( int i=1; i<=n; i++ )
cin>>a[i];
cout<<"\n";
cout<<"enter the element which you want to insert: ";
cin>>item;
cout<<"\n";
cout<<"enter the position where to insert: ";
cin>>loc;
for(int i=n; i>=loc-1;i--)
{
a[i+1] = a[i];
a[loc-1]=item;
}
n++;
cout<<"Modified array: "<<"\n";
for (i=1; i<=n ; i++ )
cout<<a[i]<<"\n";
getch();
 }
:-

   










PRACTICAL NO.20

 WAP to perform binary search.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int n,s=0,i,a[100],item,beg,end,mid;
cout<<"enter the size of array";
cin>>n;
cout<<"enter the elements of array";
for(i=1;i<=n;i++)
{
cin>>a[i];
}
cout<<"elements of array are ";
for(i=1;i<=n;i++)
{
cout<<a[i];
}
cout<<" enter the item to be search ";
cin>>item;
beg=1;
end=n;
mid=(beg+end)/2;
while(beg<=end && a[mid]!=item)
{
if(item<a[mid])
{
end=mid-1;
}
if(item>a[mid])
{
beg=mid+1;
}
mid=(beg+end)/2;

if(a[mid]==item)
{
cout<<" item is found at location "<<mid;
 s=1;
break;
}}
if(s==0)

{
cout<<" item is not found ";
}
getch();
}


PRACTICAL NO.21
 WAP to create a simple link list
#include<iostream.h>
#include<conio.h>
int main()
{
clrscr();
int link[10];
int info[10],start,*ptr;
cout<<endl<<"enter start";
cin>>start;
ptr=&start;
while(*ptr!=0)
{
cout<<"enter info";
cin>>info[*ptr];
cout<<"enter new location ";
cin>>link[*ptr];
ptr=&link[*ptr];
}
ptr=&start;
cout<<"linked list";
while(*ptr!=0)
{
cout<<endl<<info[*ptr]<<" ";
ptr=&link[*ptr];
}
getch();
return 0;
}
:-

                                               
PRACTICAL NO.22

WAP to explain the concept of c++

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int *pc,c;
c=5;
cout<<"Address of c:"<<&c<<endl;
cout<<"contents of c:"<<c<<endl;
pc=&c;
cout<<" Address that pc holds"<<pc<<endl;
cout<<"Content that pc holds"<<*pc<<endl;
c=11;
cout<<"Address pointer that pc hold"<<pc<<endl;
cout<<"Content of address that pc holds"<<*pc<<endl;
*pc=2;
cout<<"Address of c:"<<&c<<endl;
cout<<"Value of c:"<<c<<endl;
getch();
}

:-




PRACTICAL NO.23

WAP to create link list using structure.

#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
struct node
{
int info;
node *link;
};
void main()
{
clrscr();
node *first,*ptr,*q;
char ch='y';
first=new node;
first->link='\0';
cout<<" enter value of first node ";
cin>>first->info;
ptr=first;

do
{
q=new node;
cout<<" enter value of next node ";
cin>>q->info;
q->link='\0';
ptr->link=q;
ptr=q;
cout<<" \n do you want to continue ";
cin>>ch;
}
while(ch=='y');
cout<<" \n entered link list is ";
ptr=first;
while(ptr!='\0')
{
cout<< setw(4)<<"->"<<ptr->info;
ptr=ptr->link;
}
delete ptr,first,q;
getch();
}
 PRACTICAL NO.24
WAP to create structure of an employee
#include<iostream.h>
#include<conio.h>
struct person
{
int age;
char name[50];
float salary;
};
int main()
{
clrscr();
person p1;
cout<<endl<<" enter name :";
cin>>p1.name;
cout<<" enter age :";
cin>>p1.age;
cout<<" enter salary ";
cin>>p1.salary;
cout<<endl<<" display employee record ";
cout<<endl<<" Name"<<p1.name<<endl;
cout<<" age" <<p1.age<<endl;
cout<<" salary"<<p1.salary;
getch();
return 0;
}
:-




PRACTICAL NO.25
WAP to create a linked list and insert a node in it.
#include<iomanip.h>
#include<iostream.h>
#include<conio.h>
struct node
{int val;node *next;
};main()
{cout<<"ANSHUL SUNEJA"<<endl;
            node *first,*ptr,*q;
            char ch='y';
            int x,flag=0;
            first= new node;
            first->next='\0';
            cout<<"Enter value of 1st node=";
            cin>>first->val;ptr=first;
            do{q= new node;
                        cout<<"Enter value of next node=";
                        cin>> q->val;
                        q->next='\0';ptr->next=q;ptr=q;
                        cout<<"Do you want to store new node=";
                        cin>>ch; }while(ch=='y');
            cout<<"Enter the number after which node is to be iserted=";
            cin>>x;ptr=first;
while(ptr!='\0')
            {if(ptr->val==x){flag=1;
                                    q= new node;
                                    cout<<"Enter value to be inserted=";
                                    cin>>q->val;
                                    q->next=ptr->next;
                                    ptr->next=q;
                                    break;
                                    }else
                                    ptr=ptr->next;}
                        if(flag==0){cout<<"Given no is not present in linked list";
}else{cout<<"No is inserted successfully";
                        cout<<"Linked list after insertion is="<<endl;
ptr=first;while(ptr!='\0'){cout<<ptr->val<<endl;
                                                ptr=ptr->next;}}getch();}
:-



PROGRAM NO.26
WAP for deletion from a link list.
#include<iomanip>
#include<iostream>
#include<conio.h>
struct node
{int val;
node *next;};
main()
{cout<<"ANSHUL SUNEJA"<<endl;
node *first,*ptr,*q;
char ch='y';int x;
first= new node;
first->next='\0';
cout<<"Enter value of 1st node=";
cin>>first->val;ptr=first;
do{
q= new node;
cout<<"Enter value of next node=";
cin>> q->val;q->next='\0';
ptr->next=q;ptr=q;
cout<<"Do you want to store new node=";
cin>>ch; }
while(ch=='y');
                       
cout<<"Enter value to be deleted=";cin>>x;
ptr=first;q=first->next;
if(first->val==x){
first=first->next;
delete ptr;}
ptr=ptr->next;
q=first;while(ptr!='\0')
{if(ptr->val==x)
{q->next=ptr->next;
cout<<"deleted value is="<<ptr->val<<endl;
delete ptr;}
else{    ptr=ptr->next;
q=q->next;}}
cout<<"list after deleting node is:-"<<endl;
ptr=first;
while(ptr!='\0')
{
cout<<setw(4)<<"->"<<ptr->val;
cout<<ptr->val<<endl;
ptr=ptr->next;}
delete ptr,q,first;
getch();
}
PRACTICAL NO. 27
 WAP to perform insertion in sorted linked list
#include<iostream.h>
#include<conio.h>
struct node
{
int val;
node *next;
};
void main()
{
clrscr();
node *first,*save,*q,*p;
char ch='y';
int ele;
first=new node;
cout<<"\n enter the value first node "
cin>>first->val;
first->next='\0';
p=first;
do
{
q=new node;
cout<<"enter the value of next node "
cin>>q->val;
q->next='\0';
p->next=q;
p=q;
cout<<" do you want to  insert more node "
cin>>ch ;
}
while(ch=='y');
p=first;
cout<<"\n entered list is "
while(p!=0)
{
cout<<p->val<<" "
p=p->next;
}
p=first;
save=first;
cout<<"\n enter the value to insert "
cin>>ele;
while(p->val<ele)
{
save=p;
p=p->next;}
q=new node;
q->val=ele;
q->next=p;
save->next=q;
p=first;
cout<<" after insertion list is "
while(p!='\0')
{
cout<<p->val<<" "
p=p->next;
 }
delete save ,p,q,first;
getch();
}
:-







PRACTICAL NO 28

WAP to read and display an entered list or string.

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
void main()
{
clrscr();
char str[100];
cout<<" enter a string ";
cin.get(str,100);
cout<<" your entered string is  "<<str<<endl;
getch();
}



PRACTICAL NO 29

WAP to explain function in c++
#include<iostream.h>
#include<conio.h>
int add(int,int);
void main()
{
clrscr();
int n1,n2,sum;
cout<<" enter two no ";
cin>>n1>>n2;
sum=add(n1,n2);
cout<<" Sum of two no are "<<sum;
getch();
}
int add(int a,int b)
{
int add;
add=a+b;
return add;
}



PRACTICAL NO 30
#include<iostream.h>
#include<conio.h>
int max=6;
int t=-1;
int i=0;
class stack{int s[7];
      public:void push(int);
      void pop();
      void top();
      void empty();
      void show();}
void stack::push(int y)
{  if(t<max)  {t=t+1;
                   s[t]=y;} else
 {cout<<endl<<" Stack Overflow\n"; }}
void stack::pop(){
      int item;if(t>0)
      {t=t-1;   item=s[t+1];
                   cout<<"\n Popped item";
                   cout<<item<<endl;}
      else{  cout<<endl; cout<<" Stack Underflow\n";
      }}void stack::top()
{if(t>=0) {   cout<<"\n Top most element: ";
                   cout<<s[t]<<endl;
      }
      else
      cout<<" Null \n";
}
void stack::empty()
{
      if(t<0)
      cout<<" Stack is empty";
      else
      cout<<" Stack is not empty\n";
}
void stack::show()
{
      if(t<0)
      cout<<"\n Stack is empty\n";
      else
      {
                   for(i=t;i>=0;i--)
                   {
                       cout<<endl<<s[i];
                   }
      }
}
void main()
{
      clrscr();int x,a;
      char ch;stack s1;
      do{  cout<<" Enter your choice:\n";
                   cout<<" 1-push  2-pop  3-top  4-empty  5-show\n";
                   cout<<" What is your choice:\n";  cin>>x;
                   switch(x){case 1:  {  cout<<" Enter value: " ;
                                                   cin>>a;  s1.push(a);}
                       break;
                       case 2:  s1.pop();break;
                       case 3:  s1.top();break;
                       case 4:  s1.empty();break;
                       case 5:  s1.show();break;
                       default: cout<<" Wrong input"; }
                   cout<<" Do u want to continue (y/n): ";
                   cin>>ch;}
      while(ch=='y')}
:-

 

PRACTICAL NO. 31
Wap to find elements in a linked list.
#include<iostream.h>
#include<conio.h>
struct Node
{
 int data;
 Node* next;
};
Node *head=NULL;
void insert(int x)
{
 Node* temp=new Node;
 temp->data=x;
 temp->next=NULL;
 if(head==NULL)
 {
  head=temp;
  return;
 }
 Node* temp1=head;
 while(temp1->next!=NULL)
 {
  temp1=temp1->next;
 }
 temp1->next=temp;
}

void print()
{
 Node* temp1=head;
 while(temp1==NULL)
 {
  cout<<temp1->data<<endl;
  temp1=temp1->next;
 }
}

void search(int s)
{
 Node* temp1=head;
 int count=1;
 while(temp1!=NULL)
 {
  count++;
  if(temp1->data==s)
  {
   cout<<"data is at pos="<<count;
   return;
  }
  temp1=temp1->next;
 }
}

void main()
{
 clrscr();
 int n,x;
 cout<<"Enter size of an array is=";
 cin>>n;
 for(int i=0;i<n;i++)
 {
  cout<<"Enter element no."<<i+1<<"=";
  cin>>x;
  insert(x);
 }
 cout<<"Element enter are:\n";
 print();
 int s;
 cout<<"Eneter element to be search:";
 cin>>s;
 search(s);
 getch();}

:-
PRACTICAL  NO. 32
Wap to implementing stack using linked list.
#include<iostream.h>
#include<conio.h>
class str
{
            int info;
            char a,ans;
            public:
            void push();
            void pop();
            str *next,*top,*temp,*node,*ptr;
};
void str::push()
{
            node=new str;
            cout<<"\nenter marks";
            cin>>node->info;
            if(node=='\0')
            {
                        cout<<"OVERFLOW";
            }
            if(top=='\0')
            {
                        top=node;
                        top->next='\0';
            }
            else
            {
                        node->next=top;
                        top=node;
            }
            cout<<"do you want to enter marks";
            cin>>ans;
            if(ans=='y'||ans=='Y')
            {
                        push();
            }
}
void str::pop()
{
            ptr=top;
            while(ptr->next!='\0')
            {
                        cout<<ptr->info<<" ";
                        ptr=ptr->next;
            }
            if(top->next=='\0')
            {
                        cout<<"\n UNDERFLOW";
            }
            else
            {
                        cout<<"\n element to pop is";
            }
            cout<<top->info;
            temp=top;
            top=top->next;
            delete(temp);
            cout<<"\ndo you want to delete more data";
            cin>>a;
            if(a=='y'||a=='Y')
            {
                        pop();
            }
}
void main()
{
            clrscr();
            str s;
            s.push();
            s.pop();
            getch();}

:-

 

PRACTICAL NO. 33
Wap  to implement queue.
#include<iostream.h>
#include<conio.h>
#include<process.h>
struct node
{
            int info;
            struct node *next;
};
class queue
{
            private:
                        node *rear;
                        node *front;
            public:
                        queue();
                        void enqueue();
                        void dequeue();
                        void display();
};
queue::queue()
{
            rear=NULL;
            front=NULL;
}
void queue::enqueue()
{
            int data;
            node *temp=new node;
            cout<<"enter data"<<endl;
            cin>>data;
            temp->info=data;
            temp->next=NULL;
            if(front==NULL)
            {
                        front=temp;
            }
            else
            {
                        rear->next=temp;
                        rear=temp;
            }
}
void queue::dequeue()
{
            node *temp=new node;
            if(front==NULL)
            {
                        cout<<"queue is empty\n";
            }
            else
            {
            temp=front;
            front=front->next;
            cout<<"data dequeued\n"<<temp->info;

            delete temp;
            }
}
void queue::display()
{
            node *p=new node;
            p=front;
            if(front==NULL)
            {
                        cout<<"nothing to display";
            }
            else
            {
                        while(p!=NULL)
                        {
                                    cout<<p->info;
                                    p=p->next;
                        }
            }
}
void main()
{
            clrscr();
            queue Queue;
            int choice;
                while(1)
                {
                        cout<<"1.enqueue , 2.dequeue , 3.display "<<endl;
                        cout<<"enter choice"<<endl;
                        cin>>choice;
                        switch(choice)
                        {
                        case 1:Queue.enqueue();
                        break;
                        case 2:Queue.dequeue();
                        break;
                        case 3:Queue.display();
                        break;
                        case 4:exit(0);
                        break;
                        default:cout<<"invalid input";
                        break;
                        }
                 }

                        getch();
}

:-










PRACTICAL NO. 34
Wap to perform selection sort
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a[20],i,temp,j,n;
cout<<"enter the no. the elements"<<endl;
cin>>n;
cout<<"enter elements";
for(i=1;i<=n;i++)
{
cin>>a[i];
}
for(i=1;i<=n-1;i++)
{
for(j=i+1;j<=n;j++)
{
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
cout<<"sorted list is";
for(i=1;i<=n;i++)
{cout<<"  "<<a[i];}
getch();}
:-
PRACTICAL NO. 35
Wap to perform insertion sort
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a[20],i,temp,j,n,k;
cout<<"enter the no. the elements"<<endl;
cin>>n;
cout<<"enter elements";
for(i=1;i<=n;i++)
{
cin>>a[i];
}
for(i=1;i<n;i++)
{
for(j=0;j<=i;j++)
{
if(a[j]>a[i])
{
temp=a[i];
k=i;
while(k!j)
{a[k]=a[k-1];
k--;}
a[j]=temp;
}}cout<<"\n Sorted list is ";
for(i=1;i<=n;i++)
cout<<"  "<<a[i];
getch();}
:-

PRACTICAL NO.36
Write a program to demonstrate the use of stack (implemented using linear array) in converting arithmetic expression from infix notation to postfix notation.
#include<iostream.h>
#include<ctype.h>
#include<conio.h>
#define size 30
class post
{
char s[size];
inttos;
public:
post()
{
tos=-1;
}
void push(char);
char pop();
};

void post::push(char a)
{
if (tos==(size-1))
{
cout<<"\nStack overflow";
return;
}
s[++tos]=a;
}

char post::pop()
{
if (tos==-1)
return(NULL);
return(s[tos--]);
}

int priority (char ch)
{
if (ch=='(')  return(0);
if (ch=='+' || ch=='-')  return(1);
if (ch=='*' || ch=='/' || ch=='%')  return(2);
if (ch=='^') return(3);
return(-1);
}

void main()
{
post p;
int i=0, j=0;
char a, b, infix[size], expr[size];
cout<<"\nEnter Infix Expression \n";
cin.getline(infix, size);
while((a=infix[i++])!=NULL)
{
if (a=='(')
p.push(a);
else
{
if (a==')')
{
while((b=p.pop())!='(')
expr[j++]=b;
}
else
{
if (a=='+' || a=='-' ||a=='*' ||a=='%' || a=='^' || a=='/')
{
if ((b=p.pop())!=NULL)
{
while(priority(b)>=priority(a))
{
expr[j++]=b;
   b=p.pop();
}
p.push(b);
p.push(a);
}
else
p.push(a);
}
else
{
if (isalpha(a)!=0 || isdigit(a)!=0)
{
expr[j++]=a;
}
}
}
}
}
while((a=p.pop())!='\0')
expr[j++]=a;
expr[j]='\0';
cout<<"\nInfix  : "<<infix;
cout<<"\nPostfix : "<<expr;
getch();
}




PRACTICAL NO. 37
 WAP TO INSERT & DELETE ELEMENTS IN QUEUE
#include<iostream.h>
#include<conio.h>
#include<process.h>
#define MAX 10
int r=-1;
int f=-1;
int q[MAX];
voidqinsert();
voidqdel();
void main()
{
int i;
clrscr();
do
{
cout<<endl;
cout<<"1.To insert element in queue"<<endl;
cout<<"2.To delete element from the queue"<<endl;
cout<<"3.To Exit"<<endl;
cout<<"Enter ur choice:";
cin>>i;
switch(i)
{
case 1:
qinsert();
break;
case 2:
qdel();
break;
default:
exit(0);
}
}while(i!=3);
getch();
}
voidqinsert()
{ int t;
if(q[r]==MAX-1)
{
cout<<"OVERFLOW";
}
else if(q[f]==-1)
{
q[f]=0;
q[r]=0;
}
else
cout<<"Enter element you want to insert:";
cin>>t;
q[++r]=t;
}
voidqdel()
{
int s;
if(q[f]==-1)
{
cout<<"EMPTY";
}
s=q[f];
cout<<s<<endl;
if(q[f]==q[r])
{
q[f]=-1;
q[r]=-1;
}
else
q[++f];
}

q[r]=-1;
}
else
q[++f];
}

 
 PRACTICAL NO.38
WAP TO CREATE A BINARY SEARCH TREE & SHOWING ITS PREORDER, POSTORDER& INORDER TRAVERSAL.
#include<iostream.h>
#include<conio.h>
#include<malloc.h>
#include<process.h>

int k=0,m=0;
structnodetype
{
int info;
structnodetype *lchild;
structnodetype *rchild;
}*root,*dnode,*pardnode;

voidbtreeins(int);
voidinorder(structnodetype *);
void preorder(structnodetype *);
voidpostorder(structnodetype *);
void main()
{
int n=0,ch,x,dn;
clrscr();
root=NULL;
do
{ cout<<endl;
cout<<"1. To Create Binary Tree"<<endl;
cout<<"2. For Inorder Traversing Of Tree"<<endl;
cout<<"3. For postorder Traversing Of Tree"<<endl;
cout<<"4. For Preorder Traversing Of Tree"<<endl;
cout<<"5. To Exit"<<endl;
cout<<"\nEnter Your Choice:";
cin>>ch;
switch(ch)
{
case 1:
while(n!=-1)
 {
cout<<"Enter node & Enter (-1) To Terminate:";
cin>>n;
if(n!=-1)
btreeins(n);
 }
 n=0;
break;

case 2:
inorder(root);
cout<<endl;
break;

case 3:
postorder(root);
cout<<endl;
break;

case 4:
preorder(root);
cout<<endl;
break;

default:
exit(0);
 }
}while(ch!=5);
getch();
 }

voidbtreeins(int x)
 {
structnodetype *p,*parent;
int found=0;
 p=root;
while((p!=NULL) &&(!found))
 {
parent=p;
if(x==p->info)
found=1;
else if(x<p->info)
 p=p->lchild;
else
 p=p->rchild;
 }
if(!found)
 {
 p=(structnodetype *)malloc(sizeof(structnodetype));
p->info=x;
p->lchild=NULL;
p->rchild=NULL;

if(root!=NULL)
 {
if(x<parent->info)
parent->lchild=p;
else
parent->rchild=p;
 }
else
root=p;
 }
 }

voidinorder(structnodetype *bt)
 {
if(bt)
 {
inorder(bt->lchild);
cout<<bt->info<<" ";
inorder(bt->rchild);
 }
 }

void preorder(structnodetype *bt)
 {
if(bt)
 {
cout<<bt->info<<" ";
preorder(bt->lchild);
preorder(bt->rchild);
 }
 }

voidpostorder(structnodetype *bt)
 {
if(bt)
 {
postorder(bt->lchild);
postorder(bt->rchild);
cout<<bt->info<<" ";
 }
 }
                                                                  
PRACTICAL NO.39
WAP TO SORT ELEMENTS USING MERGE SORT
#include<iostream.h>
#include<conio.h>
#define MAX 10
voidmergesort(intlow,inthigh,int a[20])
{
void merge(int,int,int,int[]);
int mid;
if(low<high)
{
mid=(low+high)/2;
mergesort(low,mid,a);
mergesort(mid+1,high,a);
merge(low,mid,high,a);
return;
}
void merge(intlow,intmid,inthigh,int a[])
{
int b[MAX],i,j,k,h;
h=low;
i=low;
j=mid+1;
while((h<=mid) && (j<=high))
{
if(a[h]<=a[j])
{
b[i]=a[h];
h=h+1;
}
else
{
b[i]=a[j];
j=j+1;
}
i=i+1;
}
if(h>mid)
{
for(k=j;k<=high;k++)
{
b[i]=a[k];
i=i+1;
}
}
else
for(k=h;k<=mid;k++)
{b[i]=a[k];
i=i+1;
}
for(k=low;k<=high;k++)
{
a[k]=b[k];
}
}
void main()
{
clrscr();
int a[MAX],i,j,k,low=0,high,mid,n;
voidmergesort(int,int,int[]);
cout<<"Enter the number of elements to be sorted:";
cin>>n;
cout<<"Enter unsorted list:\n";
for(i=0;i<n;i++)
{
cout<<"Enter element num"<<i+1<<":";
cin>>a[i];
}
high=n-1;
mergesort(low,high,a);
cout<<"Merge  is:\n";
for(i=0;i<n;i++)
cout<<a[i]<<endl;
getch();
PRACTICAL NO.40
 PROGRAM TO SORT ELEMENTS IN AN ARRAY USING QUICK SORT.
#include<iostream.h>
#include<conio.h>
int a[20];
void quick(int,int);
int par(int,int);
void main()
{
clrscr();
intn,i,j;
cout<<"Enter how many elements you want to enter:";
cin>>n;
for(i=1;i<=n;i++)
{
cout<<"Enter element num"<<i<<":";
cin>>a[i];
}
quick(1,n);
cout<<endl;
cout<<"/* Elements in sorted order are */"<<'\n'<<'\n';
for(i=1;i<=n;i++)
{
cout<<a[i]<<endl;
}
getch();
}
void quick(intp,int q)
{
if(p<q)
{ int n;
int j=par(p,q);
quick(p,j-1);
quick(j+1,q);
}
}
int par(intm,int q)
{
inti,j,v;
i=m;
v=a[m];
j=q;
while(i<j)
{
while(a[i]<=v)
{
i++;
}
while(a[j]>v)
{
j--;
}
if(i<j)
{
int temp;
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
a[m]=a[j];
a[j]=v;
return j;
}

PRACTICAL NO.41.
 WAP TO SORT ELEMENTS USING HEAP SORT.
#include<iostream.h>
#include<conio.h>
void insert(int a[],int n)
{
int i=n,item=a[n];

while((i>1) && (a[i/2]<item))
{
a[i]=a[i/2];
i=i/2;
}
a[i]=item;
}
void adjust(int a[],inti,int n)
{
int j=2*i,item=a[i];

while(j<=n)
{
if((j<n)&& (a[j]<a[j+1]))
j=j+1;
if(item>=a[j])
break;
a[j/2]=a[j];
j=2*j;
}
a[j/2]=item;
}
voiddelmax(int a[],int n)
{
int x;
x=a[1],a[1]=a[n];
a[n]=x;
adjust(a,1,n-1);
}
voidhsort(int a[],int n)
{
int i;
for(i=1;i<=n;i++)
insert(a,i);
for(i=n;i>0;i--)
delmax(a,i);
}
void main()
{
int x[10],n,i;
clrscr();
cout<<"Enter the number of elements:";
cin>>n;
cout<<"Enter the elements\n";
for(i=1;i<=n;i++)
{cout<<"Element num no"<<i<<":";
cin>>x[i];}
hsort(x,n);
cout<<"The sorted  is:\n";
for(i=1;i<=n;i++)
cout<<x[i]<<endl;
getch();
}

PRACTICAL NO.42.
WAP TO TRAVERSE A TREE USING BREADTH FIRST SEARCH.
#include<iostream.h>
#include<conio.h>
#define MAXSIZE 10
struct queue
{
int item[MAXSIZE];
int rear;
int front;
}q;
voidqueueins(struct queue*,int);
intqueuedel(struct queue*);
int empty(struct queue*);
voidadjmat(int[MAXSIZE][MAXSIZE],int);
voidbfs(int[MAXSIZE][MAXSIZE],int,int);
void main()
{
clrscr();
int a[MAXSIZE][MAXSIZE],v,g;
q.front=-1;
q.rear=-1;
cout<<"Enter no. of vertices in graph:";
cin>>v;
adjmat(a,v);
cout<<"Initialize the starting vertex:";
cin>>g;
bfs(a,g,v);
getch();
}
voidbfs(int a[MAXSIZE][MAXSIZE],intg,int n)
{
inti,j,visited[MAXSIZE],h;
for(i=0;i<=n;i++)
visited[i]=0;
h=g;
visited[h]=1;
cout<<"BFS sequence with start vertex is:";
while(1)
{
cout<<h<<"  ";
for(j=0;j<n;j++)
{
if((a[h][j]==1) && (visited[j]==0))
{
queueins(&q,j);
visited[j]=1;
   }
 }
if(empty(&q))
return;
else
 h=queuedel(&q);
}
}
voidqueueins(struct queue*q, int x)
{
if(q->rear==MAXSIZE-1)
cout<<"queue is full";
//return;
else
{
if(q->front==-1)
{
q->front=0;
q->rear=0;
}
else
q->rear=q->rear+1;
q->item[q->rear]=x;
}
}

intqueuedel(struct queue *q)
{
int x;
x=q->item[q->front];
if(q->front==q->rear)
    {
    q->front=-1;
q->rear-1;
    }
else
    q->front=q->front+1;
return x;
}

int empty (struct queue *q)
{
if(q->front==-1)
return (1);
else
return (0);                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    0;
}

voidadjmat(int a[MAXSIZE][MAXSIZE],int n)
{
inti,j,m;
cout<<"Input Adjacency Matrix:\n";
cout<<"Enter its num of rows & columns:";
cin>>n>>n;
for(i=0;i<n;i++)
for(j=0;j<n;j++)
cin>>a[i][j];
}

 



























PRACTICAL NO.43

WAP OF LINEAR QUEUE

#include<iostream.h>
#include<conio.h>
#include<process.h>
#define SIZE 10
static int front=0;
static int end=-1;
class queue
{ private: int ar[SIZE];
public: void insert(int item);
void delque();
void viewque();
};
void queue::insert(int item)
{ if(end==SIZE-1)
cout<<"\nThe Queue is Full";
else { ar[++end]=item;
cout<<"\nElement succesfully inserted in the Queue";
}
}
void queue::delque()
{ if(end<0)
cout<<"\nQueue Under flow";
else
 { front++;
 cout<<"\nElement sucessfully deleted from the Queue"; } }
 void queue::viewque()
 { if(end<0)
 cout<<"\nThe Queue is Empty it cannot be Viewed";
 else
 for(int i=front;i<=end;i++)
 cout<<ar[i]<<" "; }
 void main()
 { clrscr();
 char choice;
 int ch,num;
 queue ob;
 do
 { clrscr();
 cout<<"\nQUEUE OPERATIONS";
 cout<<"\n1.INSERT";
 cout<<"\n2.DELETE";
 cout<<"\n3.DISPLAY";
 cout<<"\n4.EXIT";
 cout<<"\n\nEnter your choice:";
 cin>>ch;
 switch(ch)
 { case 1: cout<<"\nEnter the Element you want to Push:";
cin>>num;
ob.insert(num);
break;
case 2: ob.delque();
break;
case 3: ob.viewque();
break;
case 4: exit(0);
default: cout<<"\nWRONG ENTRY"; }
cout<<"\nDo you want to Continue(Y/N):";
 cin>>choice; }
while(choice=='y' || choice=='Y');
 getch();
}



PRACTICAL NO.44
WAP TO PRINT THE FIBONACCI SERIES USING RECURSION
#include<iostream.h>
#include<conio.h>
int fib(int);void main()
{clrscr();inti,n,b;
cout<<"Enter the limit of FIBONACCI SERIES:";
cin>>n;cout<<endl;
for(i=1;i<=n;i++)
{cout<<fib(i)<<'\t';}
getch();}
int fib(int i)
{if(i==1){
return 0;}
else if(i==2)
{return 1;}else{
return (fib(i-1)+fib(i-2));}
return i;}
:-
PRACTICAL NO.45
WAP to demonstrate the use of stack (implemented using linear linked lists)  in evaluating arithmetic expression in postfix notation.

#include <conio.h>
#include <iostream>
#include <stdlib.h>
#include <stack>
#include <string.h>

using namespace std;

bool isOperator(char ch)
{
    if (ch=='+' || ch=='-' || ch=='*' || ch=='/')
        return true;
    else
        return false;
}int performOperation(int op1, int op2, char op)
{
    int ans;
    switch(op){
    case '+':
        ans = op2 + op1;
        break;
    case '-':
        ans = op2 - op1;
        break;
    case '*':
        ans = op2 * op1;
        break;
    case '/':
        ans = op2 / op1;
        break;
    }
    return ans;
}


int main()
{
    char exp[1000], buffer[15];
    int i,op1, op2, len, j, x;
    stack<int> s;
    cout<<"Enter a Postfix Expression: ( e.g. 23 34 * )\n";
    gets(exp);
    len = strlen(exp);
    j = 0;
    for(i=0; i<len;i++){

        if(exp[i]>='0' && exp[i]<='9'){
            buffer[j++] = exp[i];
        }
        else if(exp[i]==' '){
            if(j>0){
                buffer[j] = '\0';
                x = atoi(buffer);
                s.push(x);
                j = 0;  }}
else if(isOperator(exp[i])){
            op1 = s.top();
            s.pop();
            op2 = s.top();
            s.pop();
            s.push(performOperation(op1, op2, exp[i]));
        }  }
cout<<"Answer is \n"<<s.top();
  return 0;}



40+ programs in data structure in c 40+ programs in data structure in c Reviewed by Shobhit Goel on November 23, 2015 Rating: 5

No comments:

Airtel Hackaton 2017

Powered by Blogger.