Showing posts with label DS-I. Show all posts

STL for Sorting and searching with user-defined records

, by Prashant Gunjal


/*
5. Use STL for Sorting and searching with user-defined records such as Person Record
(Name, birth date, telephone no), item record (item code, item name, quantity and cost)
*/


#include<iostream>
#include<process.h>
#include<list>
using namespace std;

typedef struct preson
{
int id;
char name[30];
int cost;
int quantity;
}node;
node p1;
list<node> s1;
list<node>::iterator p;
bool operator <(node &p,node &q)
{
if(p.id<q.id)
return true;
else
return false;

}
void create()
{
int i,n;
cout<<"\n\nHow many records u want to insert= ";
cin>>n;

for(i=0;i<n;i++)
{
cout<<"\n\nEnter code=";
cin>>p1.id;
cout<<"\n\nEnter Name= ";
cin>>p1.name;

cout<<"\n\nEnter cost= ";
cin>>p1.cost;

cout<<"\n\nEnter quantity= ";
cin>>p1.quantity;

s1.push_back(p1);

}

}
void display()
{
cout<<"\nThe Contents of the List = ";
cout<<"\n\nID\tName \tCost\tQuantity\n\n";

for(p=s1.begin();p!=s1.end();p++)
{
cout<<"\n"<<(*p).id;
cout<<"\t"<<(*p).name;
cout<<"\t"<<(*p).cost;
cout<<"\t\t"<<(*p).quantity;

}

}
void search()
{
int temp;
cout<<"\nThe Searching Opeartion = ";
cout<<"\n\nEnter the Product_ID= ";
cin>>temp;
     int flag=0;
for(p=s1.begin();p!=s1.end();p++)
{
if((*p).id==temp)
{
cout<<"\nEntered Product is found";
cout<<"\n\nID\tName \tCost\tQuantity\n\n";
cout<<"\n"<<(*p).id;
cout<<"\t"<<(*p).name;
cout<<"\t"<<(*p).cost;
cout<<"\t\t"<<(*p).quantity;
flag=1;
break;
}
}
if(!flag)
cout<<"\nSuch Record is not found. ";

}
void sort()
{
s1.sort();
cout<<"\nRecords sorted successfully. ";

}


void main()
{
int ch;
do{
cout<<"\nPerson Database Program:-";
cout<<"\n1. Create \n2. Display \n3. Search\n4. Sort\n5. Exit.";
cout<<"\nEnter UR choice= ";
cin>>ch;
switch(ch)
{
case 1:
  create();
  break;
case 2:
  display();
  break;
case 3:
  search();
  break;
case 4:
  sort();
  break;
case 5:
  exit(0);


}
}while(ch!=5);

}

OUTPUT:-


Person Database Program:-
1. Create
2. Display
3. Search
4. Sort
5. Exit.
Enter UR choice= 1


How many records u want to insert= 3


Enter code=101


Enter Name= abc1


Enter cost= 100


Enter quantity= 10


Enter code=103


Enter Name= abc3


Enter cost= 225


Enter quantity= 7


Enter code=102


Enter Name= abc2


Enter cost= 150


Enter quantity= 4

Person Database Program:-
1. Create
2. Display
3. Search
4. Sort
5. Exit.
Enter UR choice= 2

The Contents of the List =

ID      Name    Cost    Quantity


101     abc1    100             10
103     abc3    225             7
102     abc2    150             4
Person Database Program:-
1. Create
2. Display
3. Search
4. Sort
5. Exit.
Enter UR choice= 3

The Searching Opeartion =

Enter the Product_ID= 102

Entered product is found

ID      Name    Cost    Quantity


102     abc2    150             4
Person Database Program:-
1. Create
2. Display
3. Search
4. Sort
5. Exit.
Enter UR choice= 4

Records sorted successfully.
Person Database Program:-
1. Create
2. Display
3. Search
4. Sort
5. Exit.
Enter UR choice= 2

The Contents of the List =

ID      Name    Cost    Quantity


101     abc1    100             10
102     abc2    150             4
103     abc3    225             7
Person Database Program:-
1. Create
2. Display
3. Search
4. Sort
5. Exit.
Enter UR choice=5
read more

Implement Dqueue (Double ended queue) using STL.

, by Prashant Gunjal





#include <iostream>

#include<deque>

using namespace std;

int main()

{

 deque<int> q;
 deque<int> ::iterator p;

 int op,x;

  do

   {

    cout<<"\n1.insert front";
    cout<<"\n2.insert rear";

    cout<<"\n3.delete front";
    cout<<"\n4.delete rear";

    cout<<"\n5.display";

    cout<<"\n6.quit";

    cout<<"\nenter u'r choice";

    cin>>op;

    switch(op)

     {

      case 1:
                                  cout<<"\nenter an elements";

              cin>>x;
                                                                        q.push_front(x);
   
              break;

      case 2:

              cout<<"\nenter an elements";
 
              cin>>x;
   
              q.push_back(x);
   
              break;

      case 3:
 
              if(!q.empty())
   
               {
         
                 cout<<"\n deleted data is: "<<q.frot();

q.pop_front();

              }

              else
                               cout<<"q is empty";
     
                  break;
   
      case 4:

     if(!q.empty())
 
               {

                cout<<"\n deleted data is:"<<q.back();
         q.pop_back();

               }
                else

                 cout<<"q is empty";

        break;


      case 5:
   
              cout<<"\n";
                                                for(p=q.begin();p<q.end();p++)
       
              {

cout<<*p<<" ";

              }
     
              break;

    }

   }while(op!=6);

}



               OUTPUT:


1.insert front

2.insert rear

3.delete front

4.delete rear

5.display

6.quit


enter u'r choice 1


enter an elements 10


1.insert front

2.insert rear

3.delete front

4.delete rear

5.display

6.quit


enter u'r choice 1


enter an elements15


1.insert front

2.insert rear

3.delete front

4.delete rear

5.display

6.quit


enter u'r choice 5



15 10

1.insert front

2.insert rear

3.delete front

4.delete rear

5.display

6.quit


enter u'r choice 2



enter an elements 5


1.insert front

2.insert rear

3.delete front

4.delete rear

5.display

6.quit


enter u'r choice 5



15 10 5


1.insert front

2.insert rear

3.delete front

4.delete rear

5.display

6.quit


enter u'r choice 3


deleted data is: 15


1.insert front

2.insert rear

3.delete front

4.delete rear

5.display

6.quit


enter u'r choice 4

deleted data is: 5


1.insert front

2.insert rear

3.delete front

4.delete rear

5.display

6.quit

enter u'r choice 5

10

1.insert fron
2.insert rear
3.delete front
4.delete rear
5.display
6.quit

enter u'r choice 6
read more

Write a program to add binary numbers Use STL stack.

, by Prashant Gunjal




#include <iostream>

#include<stack>

#include<string.h>

using namespace std;

stack<int>s1;

stack<int>s2;

stack<int>s3;

int main()

 {


    char number1[5],number2[5];

    int i,a,b;



    cout<<"\nEnter the 1st binary number=";

    cin>>number1;

     for(i=0;number1[i]!='\0';i++)

      {

        if(number1[i]=='0')

         s1.push(0);

        else

         s1.push(1);

      }


    cout<<"\nEnter the 2nd binary number=";

    cin>>number2;

    for(i=0;number2[i]!='\0';i++)

     {

       if(number2[i]=='0')

s2.push(0);

         else

 s2.push(1);

     }


int carry=0,result,sum;

while(!s1.empty()||!s2.empty())

{

          a=0;

          b=0;

          if(!s1.empty())

           {

             a=s1.top();

             s1.pop();

           }

if(!s2.empty())

{

                  b=s2.top();

           s2.pop();

      }

           sum=carry+a+b;

           result=sum%2;

           s3.push(result);

           carry=sum/2;

}

     cout<<"\nAddition is = ";

      if(carry==1)

     s3.push(carry);



     while(!s3.empty())

      {

        cout<<s3.top();

        s3.pop();

      }

      cout<<"\n";

      return(0);

}


                      output:



Enter the 1st binary number=1111


Enter the 2nd binary number=1010


Addition is = 11001

read more

QUEUE using SLL

, by Prashant Gunjal



#include<iostream.h>
#include<conio.h>
template<class t>
struct sll
{
t data;
struct sll * next;
};
typedef struct sll<t> node;
node * r,*f;

class Q
{
public:
node * temp;
Q()
{
f=r=NULL;
}
void insert();
void remove();
void display();
};
template<class t>
void Q<t>::insert()
{
cout<<"\nEnter data to be insert : ";
if(f==NULL)
{
f=new node;
cin>>f->data;
r=f;
f->next=NULL;
}
else
{
temp=new node;
temp->next=NULL;
cin>>temp->data;
r->next=temp;
r=temp;
}
}
template<class t>
void Q<t>::display()
{
cout<<"\nData of Queue : ";
if(f==NULL)
{
cout<<"\nEmpty Queue ";
}
else
{
temp=f;
while(temp!=NULL)
{

cout<<"\t"<<temp->data;
temp=temp->next;
}
}
}
template<class t>
void Q<t>::remove()
{

if(f==NULL)
{
cout<<"\nNot possible ";
}
else
{
cout<<"\ndata removed : "<<f->data;
f=f->next;
if(f==NULL)
r=NULL;

}
}
void main()
{
Q <int>q;
int ch;
clrscr();
do
{
cout<<"\n1.Insert \n2.Display \n3.Delete\nexit";
cout<<"\nEnter your choice no : ";
cin>>ch;
switch(ch)
{
case 1:
q.insert();
break;
case 2:
q.display();
break;
case 3:
q.remove();
break;
}
}while(ch!=4);
getch();
}
/*

1.Insert                                                                      
2.Display
3.Delete                                                                      
exit                                                                          
Enter your choice no : 1                                                      
                                                                               
Enter data to be insert : 12                                                  
                                                                               
1.Insert                                                                      
2.Display                                                                      
3.Delete                                                                      
exit                                                                          
Enter your choice no : 1                                                      
                                                                               
Enter data to be insert : 34                                                  
                                                                               
1.Insert                                                                      
2.Display                                                                      
3.Delete                                                                      
exit                                                                          
Enter your choice no : 1                                                      
                                                                               
Enter data to be insert : 67                                                  
                                                                               
1.Insert                                                                      
2.Display                                                                      
3.Delete                                                                      
exit                                                                          
Enter your choice no : 2                                                      
                                                                               
Data of Queue :         12      34      67                                    
1.Insert                                                                      
2.Display                                                                      
3.Delete                                                                      
exit                                                                          
Enter your choice no : 3                                                      
                                                                               
data removed : 12                                                              
1.Insert                                                                      
2.Display                                                                      
3.Delete                                                                      
exit                                                                          
Enter your choice no : 4                                                      

*/
read more

STACK using SLL

, by Prashant Gunjal


/*
Stack using link list

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

struct sll
{
int data;
struct sll * next;
};
typedef struct sll node;
node * s,*top;

class stack
{
public:
node * temp;
stack()
{
s=top=NULL;
}
void push();
void pop();
void display();
};
void stack::push()
{
cout<<"\nEnter data to be insert : ";
if(top==NULL)
{
temp=new node;
temp->next=NULL;
cin>>temp->data;
s=top=temp;
}
else
{
temp=new node;
temp->next=NULL;
cin>>temp->data;
temp->next=top;
top=temp;
}
}
void stack::display()
{
if(top==NULL)
{
cout<<"\nEmpty Stack ";
}
else
{
cout<<"\nData of Stack : ";
temp=top;
while(temp!=NULL)
{

cout<<"\t"<<temp->data;
temp=temp->next;
}
}
}
void stack::pop()
{

if(top==NULL)
{
cout<<"\nNot possible ";
}
else
{
cout<<"\ndata removed : "<<top->data;
top=top->next;
if(top==NULL)
s=NULL;

}
}
void main()
{
stack q;
int ch;
clrscr();
do
{
cout<<"\n1.push \n2.Display \n3.pop\n4.exit";
cout<<"\nEnter your choice no : ";
cin>>ch;
switch(ch)
{
case 1:
q.push();
break;
case 2:
q.display();
break;
case 3:
q.pop();
break;
}
}while(ch!=4);
getch();
}
/*

1.push                                                                        
2.Display                                                                      
3.pop                                                                          
4.exit                                                                        
Enter your choice no : 1                                                      
                                                                               
Enter data to be insert : 12
                                                                               
1.push                                                                        
2.Display                                                                      
3.pop                                                                          
4.exit                                                                        
Enter your choice no : 1                                                      
                                                                               
Enter data to be insert : 13                                                  
                                                                               
1.push                                                                        
2.Display                                                                      
3.pop                                                                          
4.exit                                                                        
Enter your choice no : 1                                                      
                                                                               
Enter data to be insert : 14                                                  
                                                                               
1.push                                                                        
2.Display                                                                      
3.pop                                                                          
4.exit                                                                        
Enter your choice no : 2                                                      
                                                                               
Data of Stack :         14      13      12                                    
1.push                                                                        
2.Display                                                                      
3.pop                                                                          
4.exit                                                                        
Enter your choice no : 3                                                      
                                                                               
data removed : 14                                                              
1.push                                                                        
2.Display                                                                      
3.pop                                                                          
4.exit                                                                        
Enter your choice no : 4                                                      


*/
read more

DLL

, by Prashant Gunjal


#include<iostream.h>
#include<conio.h>
#include<alloc.h>
#include<stdio.h>
struct list
{
int data;
struct list * next;
struct list * prev;

};
typedef struct list node;
node * head=NULL;
class sll
{
public:
node * getnode();
void create();
void display();
void deletef();
void deletel();
node * insertf();
void insertl();
void deletepos();
void insertpos();
};
node * sll::getnode()
{
node * temp;
temp = new node();
//or you can use//temp=(node *)malloc(sizeof(node));
cout<<"Enter Data";
cin>>temp->data;
temp->next=NULL;
temp->prev=NULL;
return temp;
}
void sll ::create()
{
int no,i,x;
node * last,*temp;
cout<<"\nEnter no of nodes :";
cin>>no;
for(i=0;i<no;i++)
{
temp=getnode();
if(head==NULL)
{
head=temp;
head->next=NULL;
head->prev=NULL;
}
else
{
last=head;
while(last->next!=NULL)
{
last=last->next;
}
last->next=temp;
temp->prev=last;
}

}
}
void sll::display()
{
node *p;
p=head;
cout<<"\nContent\n";
do
{
cout<<p->data<<" ";
p=p->next;

}while(p!=NULL);

}

node * sll::insertf()
{
node * p=NULL;
int x;
p= getnode();
p->next=head;
head->prev=p;
head=p;
return head;
}
void sll::insertl()
{
node * p=NULL,*temp=NULL;
p=getnode();
temp=head;
while(temp->next!=NULL)
{
temp=temp->next;
}
temp->next=p;
p->prev=temp;
}

void sll::insertpos()
{
node * temp=NULL,*last,*nnext;
int pos,i;
cout<<"Enter position no. aeter to which to insert ";
cin>>pos;
if(pos>1)
{
i=1;
last=head;
while(last->next!=NULL&&i<pos)
{
last=last->next;
i++;
}
nnext=last->next;
if(last!=NULL)
{
temp=getnode();
last->next=temp;
temp->prev=last;
nnext->prev=temp;
temp->next=nnext;
}
}
else
cout<<"Not possible" ;
}

void sll ::deletef()
{
node * p,* last=NULL;
p=head;
last=head;
while(last->next!=NULL)
{
last=last->next;
}
head=head->next;
head->prev=NULL;
delete (p);
}

void sll:: deletel()
{
node * p=NULL;
p=head;
while(p->next->next!=NULL)
{
p=p->next;

}
delete (p->next);
p->next=NULL;

}

void sll::deletepos()
{
node * temp=NULL,*last,*nnext=NULL;
int pos,i;
cout<<"Enter position no. to delete node ";
cin>>pos;
if(pos>=1)
{
i=1;
last=head;
while(last->next!=NULL&&i<pos-1)
{
last=last->next;
i++;
}
if(last!=NULL)
{
nnext=last->next;
temp=nnext->next;
last->next=temp;
temp->prev=last;

}
delete nnext;
}
else
cout<<"Not possible" ;
}
void main()
{
int ch,i;
sll l;
clrscr();
do
{
cout<<"\n1.Create\n2.Display\n3.Insert First";
cout<<"\n4.Insert Last\n5.Delete First\n6.Delete Last";
cout<<"\n7.Insert by pos after\n8.delete by pos\n9.Exit";
cout<<"\nEnter your choice\t";
cin>>ch;

switch(ch)
{
case 1:
l.create();
break;

case 2:
l.display();
break;
case 3:
head=l.insertf();
break;
case 4:
l.insertl();
case 5:
l.deletef();
break;
case 6:
l.deletel();
break;
case 7:
l.insertpos();
break;
case 8:
l.deletepos();
break;
}
}while(ch<9);
       getch();

}
read more

CLL

, by Prashant Gunjal


#include<iostream.h>
#include<conio.h>
#include<alloc.h>
#include<stdio.h>
struct list
{
int data;
struct list * next;

};
typedef struct list node;
node * head=NULL;
class sll
{
public:
node * getnode();
void create();
void display();
void deletef();
void deletel();
node * insertf();
void insertl();
void deletepos();
void insertpos();
};
node * sll::getnode()
{
node * temp;
temp = new node();
//or you can use//temp=(node *)malloc(sizeof(node));
cout<<"Enter Data";
cin>>temp->data;
temp->next=NULL;
return temp;
}
void sll ::create()
{
int no,i,x;
node * last,*temp;
cout<<"\nEnter no of nodes :";
cin>>no;
for(i=0;i<no;i++)
{
temp=getnode();
if(head==NULL)
{
head=temp;
head->next=head;
}
else
{
last=head;
while(last->next!=head)
{
last=last->next;
}
last->next=temp;
temp->next=head;
}

}
}
void sll::display()
{
node *p;
p=head;
cout<<"\nContent\n";
do
{
cout<<p->data<<" ";
p=p->next;

}while(p!=head);

}


node * sll::insertf()
{
node * p=NULL,*last=NULL;
int x;
p= getnode();
last=head;
while(last->next!=head)
{
last=last->next;
}
p->next=head;
head=p;
last->next=head;
return head;
}
void sll::insertl()
{
node * p=NULL,*temp=NULL;
p=getnode();
temp=head;
while(temp->next!=head)
{
temp=temp->next;
}
temp->next=p;
p->next=head;
}

void sll::insertpos()
{
node * temp=NULL,*last;
int pos,i;
cout<<"Enter position no. aeter to which to insert ";
cin>>pos;
if(pos>1)
{
i=1;
last=head;
while(last->next!=head&&i<pos)
{
last=last->next;
i++;
}
if(last!=NULL)
{
temp=getnode();
temp->next=last->next;
last->next=temp;
}
}
else
cout<<"Not possible" ;
}

void sll ::deletef()
{
node * p,* last=NULL;
p=head;
last=head;
while(last->next!=head)
{
last=last->next;
}
head=head->next;
last->next=head;
delete (p);
}
void sll:: deletel()
{
node * p=NULL;
p=head;
while(p->next->next!=head)
{
p=p->next;

}
delete (p->next);
p->next=head;

}

void sll::deletepos()
{
node * temp=NULL,*last;
int pos,i;
cout<<"Enter position no. to delete node ";
cin>>pos;
if(pos>1)
{
i=1;
last=head;
while(last->next!=head&&i<pos-1)
{
last=last->next;
i++;
}
if(last!=head)
{
temp=last->next;
last->next=temp->next;
}
}
else
cout<<"Not possible" ;
}
void main()
{
int ch,i;
sll l;
clrscr();
do
{
cout<<"\n1.Create\n2.Display\n3.Insert First";
cout<<"\n4.Insert Last\n5.Delete First\n6.Delete Last";
cout<<"\n7.Insert by pos after\n8.delete by pos\n9.Exit";
cout<<"\nEnter your choice\t";
cin>>ch;

switch(ch)
{
case 1:
l.create();
break;

case 2:
l.display();
break;
case 3:
head=l.insertf();
break;
case 4:
l.insertl();
case 5:
l.deletef();
break;
case 6:
l.deletel();
break;
case 7:
l.insertpos();
break;
case 8:
l.deletepos();
break;
}
}while(ch<9);
       getch();

}

/* OUTPUT
1.Create
2.Display
3.Insert First
4.Insert Last
5.Delete First
6.Delete Last
7.Insert by pos after
8.delete by pos
9.Exit
Enter your choice       1

Enter no of nodes :5
Enter Data2
Enter Data3
Enter Data4
Enter Data5
Enter Data6

1.Create
2.Display
3.Insert First
4.Insert Last
5.Delete First
6.Delete Last
7.Insert by pos after
8.delete by pos
9.Exit
Enter your choice       2

Content
2 3 4 5 6
1.Create
2.Display
3.Insert First
4.Insert Last
5.Delete First
6.Delete Last
7.Insert by pos after
8.delete by pos
9.Exit
Enter your choice       3
Enter Data1

1.Create
2.Display
3.Insert First
4.Insert Last
5.Delete First
6.Delete Last
7.Insert by pos after
8.delete by pos
9.Exit
Enter your choice       2

Content
1 2 3 4 5 6
1.Create
2.Display
3.Insert First
4.Insert Last
5.Delete First
6.Delete Last
7.Insert by pos after
8.delete by pos
9.Exit
Enter your choice       4
Enter Data56

1.Create
2.Display
3.Insert First
4.Insert Last
5.Delete First
6.Delete Last
7.Insert by pos after
8.delete by pos
9.Exit
Enter your choice       5

1.Create
2.Display
3.Insert First
4.Insert Last
5.Delete First
6.Delete Last
7.Insert by pos after
8.delete by pos
9.Exit
Enter your choice       2

Content
3 4 5 6 56
1.Create
2.Display
3.Insert First
4.Insert Last
5.Delete First
6.Delete Last
7.Insert by pos after
8.delete by pos
9.Exit
Enter your choice       6

1.Create
2.Display
3.Insert First
4.Insert Last
5.Delete First
6.Delete Last
7.Insert by pos after
8.delete by pos
9.Exit
Enter your choice       2

Content
3 4 5 6
1.Create
2.Display
3.Insert First
4.Insert Last
5.Delete First
6.Delete Last
7.Insert by pos after
8.delete by pos
9.Exit
Enter your choice 7
Enter position no. aeter to which to insert 2
Enter Data34

1.Create
2.Display
3.Insert First
4.Insert Last
5.Delete First
6.Delete Last
7.Insert by pos after
8.delete by pos
9.Exit
Enter your choice       2


Content
3 4 34 5 6
1.Create
2.Display
3.Insert First
4.Insert Last
5.Delete First
6.Delete Last
7.Insert by pos after
8.delete by pos
9.Exit
Enter your choice       8
Enter position no. to delete node 4

1.Create
2.Display
3.Insert First
4.Insert Last
5.Delete First
6.Delete Last
7.Insert by pos after
8.delete by pos
9.Exit
Enter your choice       2

Content
3 4 34 6
1.Create
2.Display
3.Insert First
4.Insert Last
5.Delete First
6.Delete Last
7.Insert by pos after
8.delete by pos
9.Exit
Enter your choice       9
*/
read more