Skip to main content

DSL C23

Data Structure Lab : 


Practical C23 :

Write C++ program for storing binary number using doubly linked lists. Write functions

a) To compute 1‘s and 2‘s complement 

b) Add two binary numbers

The code for above problem is as follows:

   
   
 //Author : SPPU CSE GURU
 
 #include<iostream>  
   
 using namespace std;  
   
 struct dnode  
 {  
      int data;  
      dnode *next;  
      dnode *prev;  
 };  
   
 dnode *nnode;  
 int bit,bits,flag;  
   
 class Binary{  
        
      public:  
           dnode *head,*tail;  
             
           Binary(){  
                head=tail=NULL;  
           }  
             
      void create();  
      void display();  
      void rdisplay();  
      void OneCom();  
      void TwoCom();  
      void AddNode(int x);  
      void Addition(Binary B2);  
 };  
   
 void Binary::create(){  
      cout<<"Enter total no. of bits"<<endl;  
      cin>>bits;  
      cout<<"***Enter bits from left to right***"<<endl;  
      for(int i=1;i<=bits;i++)  
      {  
           nnode=new dnode;  
           nnode->next=NULL;  
           nnode->prev=NULL;  
           cout<<"Enter "<<i<<" bit "<<endl;  
           cin>>bit;  
           nnode->data=bit;  
           if(head==NULL||tail==NULL)  
           {  
                head=tail=nnode;  
           }  
           else  
           {  
                tail->next=nnode;  
                nnode->prev=tail;  
                tail=nnode;  
           }  
             
      }  
 }  
   
   
 void Binary::display()  
 {  
      dnode *temp;  
      temp=head;  
      cout<<"Number Is :"<<endl;  
      while(temp!=NULL)  
      {  
           cout<<temp->data<<" ";  
           temp=temp->next;  
      }  
 }  
 
   
 void Binary::OneCom()  
 {  
      dnode *temp;  
      temp=head;  
      cout<<"\n1's Complement : "<<endl;  
      while(temp!=NULL)  
      {  
           if (temp->data==0)  
           {  
                cout<<"1 ";  
           }  
           else  
           {  
                cout<<"0 ";  
           }  
           temp=temp->next;  
      }  
        
 }  
   
   
 void Binary::TwoCom()  
 {  
      dnode *temp;  
      temp=tail;  
      flag=0;  
      while(temp!=NULL)  
      {  
           if(flag==0)  
           {  
                  
             if(temp->data==1)  
                {  
                flag=1;  
              }  
                
           }  
           else  
           {  
                if (temp->data==0)  
                {  
                temp->data=1;  
             }  
           else  
             {  
                temp->data=0;  
             }  
                  
           }  
             
           temp=temp->prev;  
      }  
        
 }  
   
 void Binary::AddNode(int x)  
 {  
      nnode=new dnode;  
      nnode->next=NULL;  
      nnode->prev=NULL;  
      nnode->data=x;  
      if(head==NULL)  
      {  
           head=tail=nnode;  
      }  
      else  
      {  
           head->prev=nnode;  
           nnode->next=head;  
           head=nnode;       
      }  
        
   
 }  
   
 void Binary::Addition(Binary B2)  
 {  
      Binary B3;  
   dnode *temp1,*temp2;  
   int a,b,c=0;  
   temp1=tail;  
   temp2=B2.tail;  
   while (temp1!=NULL && temp2!=NULL)  
      {  
        a=temp1->data;  
        b=temp2->data;  
        if (a==0 && b==0 && c==0)  
           {  
             B3.AddNode(0);  
             c=0;  
           }  
             
           else if (((a==0 && b==1) || (a==1 && b==0)) && c==0)  
           {  
                B3.AddNode(1);  
             c=0;  
           }  
             
           else if (a==1 && b==1 && c==0)  
           {  
                B3.AddNode(0);  
             c=1;       
           }  
             
           else if (a==0 && b==0 && c==1)  
           {  
             B3.AddNode(1);  
             c=0;  
           }  
             
           else if (((a==0 && b==1) || (a==1 && b==0)) && c==1)  
           {  
                B3.AddNode(0);  
             c=1;  
           }  
             
           else if (a==1 && b==1 && c==1)  
           {  
                B3.AddNode(1);  
             c=1;  
           }  
             
           temp1=temp1->prev;  
           temp2=temp2->prev;  
             
      }  
        
      while (temp1!=NULL){  
           a=temp1->data;  
           if (a==0 && c==0)  
           {  
                B3.AddNode(0);  
                c=0;  
           }  
             
           else if ((a==0 && c==1)||(a==1 && c==0))  
           {  
                B3.AddNode(1);  
                c=0;  
           }  
             
           else if (a==1 && c==1)  
           {  
                B3.AddNode(0);  
                c=1;  
           }  
             
        temp1=temp1->prev;       
      }  
        
      while (temp2!=NULL)  
      {  
           b=temp2->data;  
           if (b==0 && c==0)  
           {  
                B3.AddNode(0);  
                c=0;  
           }  
             
           else if ((b==0 && c==1)||(b==1 && c==0))  
           {  
                B3.AddNode(1);  
                c=0;  
           }  
             
           else if (b==1 && c==1)  
           {  
                B3.AddNode(0);  
                c=1;  
           }  
             
           temp2=temp2->prev;  
      }  
        
      if (c==1)  
      {  
           B3.AddNode(1);  
      }  
        
      cout<<"\nBinary Addition ";  
   B3.display();  
 }  
   
 int main()  
 {  
        
      Binary B,B1,B2;  
        
      int ch;  
        
      do{  
           cout<<"\n 0.Exit\n 1.create\n 2.display\n 3. 1's Complement\n 4. 2's Complement\n 5.Addition\n ";  
           cout<<"Enter your Choice : "<<endl;  
           cin>>ch;  
           switch(ch)  
           {  
                case 0:    
            break;  
                case 1:  
                     B.create();  
                     break;  
                case 2:  
                     B.display();  
                     break;  
                case 3:  
                     B.OneCom();  
                     break;  
                case 4:  
                     B.TwoCom();  
                     cout<<"\n2's Complement ";  
                     B.display();  
                     break;  
                case 5:  
                     cout<<"\nEnter two Binary Numbers for Addition : "<<endl;  
                     B1.create();  
                     B2.create();  
                     cout<<"\n1. ";  
                     B1.display();  
                     cout<<"\n2. ";  
                     B2.display();  
                     B1.Addition(B2);  
                     break;  
                default:
	             cout<<"Wrong choice";
           }  
                        
      }while(ch!=0);  
        
        
      return 0;  
 }  

Comments

Popular posts from this blog

Circle Triangle Pattern

  Group A   Practical 3 a   Problem Statement :   a) Write C++ program to draw the following pattern. Use DDA line and Bresenham‘s circle drawing algorithm. Apply the concept of encapsulation. Check Out Code Here  ðŸ‘‡ Output :  Code can get updated so also come back later to see if there is any changes. Also if there is any problem with code you can comment below. If you like it, do share with your friends.😊

DSL A2

  Write a Python program to store marks scored in subject “Fundamental of Data Structure” by N students in the class. Write functions to compute following:  a) The average score of class  b) Highest score and lowest score of class  c) Count of students who were absent for the test  d) Display mark with highest frequency The Code for above problem is as follows : """ @author: SPPU SE GURU """ total=int(input ("Enter total no. of students in your class ")) pre=int(input("No. of students appeared for FDS exam : ")) U=[] for i in range (total): i=i+1 U.append(i) R=[] for i in range(pre): roll=int(input("Enter Roll no. of students present for test :- ")) R.append(roll) M=[] for i in range(pre): print("Enter Roll no. ",R[i],end=" ") marks=int(input("Marks:- ")) M.append(marks) print("********** Solutions Are as fo...

DSL E29

 Data Structure Lab : Practical 29 : Problem Statement:  Queues are frequently used in computer programming, and a typical example is the creation of a job queue by an operating system. If the operating system does not use priorities, then the jobs are processed in the order they enter the system. Write C++ program for simulating job queue. Write functions to add job and delete job from queue Check Out Code Here  ðŸ‘‡ // Author : SPPU CSE GURU #include<iostream> using namespace std; class Queue{ private: int front,rear,max; public: int Q[10]; Queue() { front=0; rear=-1; max=10; } void EnQueue(); void DeQueue(); void Display(); }; void Queue::EnQueue() { int data; cout<<"Enter Job no. to Add "<<endl; cin>>data; if(rear...