Skip to main content

DSL

8: Problem statement:
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

#include <iostream>

#include<vector>

using namespace std;

struct node

{

int data;

node* next;

node* prev;

};

node* Create(int x)

{

node* temp=new node;

temp->data=x;

temp->next=NULL;

temp->prev=NULL;

return temp;

}

void Insert(node** head,int x)

{

node *temp=Create(x);

if(*head==NULL)

{

*head=temp;

}

else {

(*head)->prev=temp;

temp->next=*head;

*head=temp;

}

}



void print(node* head)

{

node* temp=head;

cout<<"\ndata: ";

while(temp!=NULL)

{

cout<<temp->data<<" ";

temp=temp->next;

}

cout<<endl;

}

int sum(int a,int b,int c)

{

return a^b^c;

}

int carry(int a,int b,int c)

{

int x=a*b;

int y=b*c;

int z=c*a;

int s=sum(x,y,z);

return s;

}

node* get_sum(node** head1,node** head2,node** result)

{

* result=NULL;

node* temp1=*head1;

node* temp2=*head2;

int c=0;

while(temp1!=NULL && temp2!=NULL)

{

int s=sum(temp1->data,temp2->data,c);

c=carry(temp1->data,temp2->data,c);

Insert(result,s);

temp1=temp1->next;

temp2=temp2->next;

}

Insert(result,c);

return *result;

}

int flip(int x)

{

if(x == 1)

return 0;

return 1;

}

node* first_comp(node** head,node** result)

{

node* temp=*head;

*result=NULL;

while(temp!=NULL)

{

Insert(result,flip(temp->data));

temp=temp->next;

}

return *result;

}

int main() {

int ch,n,m;

node* pnode,*qnode,*rnode,*res,*flip,*twos;

node* p;

p=NULL;

pnode=qnode=rnode=flip=res=twos=NULL;

cout<<"Enter size of first list : "<<endl;

cin>>n;

do{

cout<<"\tWelcome\n";

cout<<" 1.Create List 1\n";

cout<<" 2.Create List 2\n";

cout<<" 3.1's compliment\n";

cout<<" 4.2's compliment\n";

cout<<" 5.Binary Addition\n";

cout<<" 6.Exit\n";

cout<<"Enter your choice : \n";

cin>>ch;

switch (ch)

{

case 1:

cout<<"Enter elements in first node."<<endl;

for(int i=0;i<n;++i) {

cin>>m;

Insert(&pnode, m);

//print(pnode);

}

cout<<"printing first list : ";

print(pnode);

break;

case 2:

cout<<"Enter elements in second node."<<endl;

for(int i=0;i<n;++i) {

cin>>m;

Insert(&qnode, m);

}

break;

case 3:

flip=first_comp(&pnode,&flip);

print(flip);

break;

case 4:

for(int i=0;i<n;++i){

if(i<n-1)

Insert(&p,0);

else

Insert(&p,1);

}

twos=get_sum(&flip,&p,&twos);

print(twos);

break;

case 5:

res=get_sum(&pnode,&qnode,&res);

print(res);

break;

}

}while (ch>=1 && ch<=6);

return 0;

}

Comments

Popular posts from this blog

DSL

9: Program Setatement A palindrome is a string of character that‘s the same forward and backward. Typically, punctuation, capitalization, and spaces are ignored. For example, ‖Poor Dan is in a droop‖ is a palindrome, as can be seen by examining the characters ―poor danisina droop‖ and observing that they are the same forward and backward. One way to check for a palindrome is to reverse the characters in the string and then compare with them the original-in a palindrome, the sequence will be identical. Write C++ program with functions- 1. to check whether given string is palindrome or not that uses a stack to determine whether a string is a palindrome. 2. to remove spaces and punctuation in string, convert all the Characters to lowercase, and then call above Palindrome checking function to check for a palindrome 3. to print string in reverse order using stack Program: #include<iostream> #include<string.h> #define MAX 100 using namespace std;  struct stack ...

DSL

13: Problem Statement: Pizza parlor accepting maximum M orders. Orders are served in first come first served basis. Order once placed cannot be cancelled. Write C++ program to simulate the system using circular queue using array. Program Code: #include <iostream> using namespace std; #define size 5 class pizza {     int porder[size];     int front,rear; public:     pizza()     {      front=rear=-1;     }     int qfull()     {      if((front==0)&&(rear==(size-1))||(front==(rear+1)%size))          return 1;      else          return 0;     }     int qempty()     {         if(front==-1)             return 1;         else             return 0;    ...

DSL

14.Problem statement Write C++ program to store roll numbers of student in array who attended training program in random order. Write function for-  a) Searching whether particular student attended training program or not using linear search and sentinel search.  b) Searching whether particular student attended training program or not using binary search and Fibonacci search.    program #include<iostream> using namespace std; class search {  public:         int n,i,data[50],son[50],sortdata[50],s;  void intake() {     cout<<"Enter No. of Elements=";   cin>>n;   cout<<"\nEnter Elements=\n";   for(i=1;i<=n;i++)   {   cin>>data[i];       son[i]=data[i];   } } void search_lini(int item) {   s=0;   for(i=1;i<=n;i++)   {  if(data[i]==item) ...