In this assignment, you write a C++ program for logical cyclic shift of n-bit nu
ID: 3667294 • Letter: I
Question
In this assignment, you write a C++ program for logical cyclic shift of n-bit numbers by given number of positions (<= n). You are required to create a circular list to store a given binary number. Then your program will be able to rotate the number left by any given number of positions for the cyclic left operation, and rotate the number by right by a given number of positions for the cyclic right operation. The provided sample input below elaborates on these operations. In this assignment you are required to use a circular linked list to create a circular representation for the n-bit number. The cyclic shift operations will change where the first bit starts in the number. The cyclic shift left by one position shifts every bit to the left by one position; and the leftmost bit moves cyclically to the vacated rightmost position. Similarly, the cyclic shift right shifts every bit to the right by one position; and the rightmost bit is moved cyclically to the vacated leftmost position. These generalize to cyclic shifts (left and right) by any given number of positions in an obvious way. You may choose to use doubly-linked circular linked list, or make a full circular traversal on each cyclic shift as we discussed in the lecture. /* first input n = */ 8 /* next inputs are n bits, from leftmost bit to rightmost bit =*/ 1 1 1 1 0 0 0 0 L 1 /* rotate left by one position */ result=11100001 L 2 /* rotate left by two positions*/ result=10000111 L 1 /* rotate left */ result=00001111 R 3 /* rotate right */ result=11100001 The above is one
Explanation / Answer
#include <iostream>
using namespace std;
struct node
{
int info;
struct node *next;
}*last;
class clist
{
public:
void createnode(int);
void addbegin(int);
void display();
void lshift();
void rshift();
} ;
int main()
{
clist c1;
int number,rem,i=1;
cout<<"Enter any number:";
cin>>number;
do
{
rem=number%2;
if(i==1)
c1.createnode(rem);
else
c1.addbegin(rem);
number=number/2;
i++;
} while(number>0);
c1.display();
int t,choice;
cout<<" choose 1 for left shiftand 2 for right shift"<<endl;
cin>>choice;
switch(choice)
{
case 1:
cout<<"enter how many times to shift";
cin>>t;
for(int i=1;i<=t;i++)
c1.lshift();
c1.display();
break;
case 2:
cout<<"enter how many times to shift";
cin>>t;
for(int i=1;i<=t;i++)
c1.rshift();
c1.display();
break;
default:cout<<"invalid choice"<<endl;
}
}
void clist::createnode(int element)
{
struct node *temp;
temp = new(struct node);
temp->info = element;
if (last == NULL)
{
last = temp;
temp->next = last;
}
else
{
temp->next = last->next;
last->next = temp;
last = temp;
}
}
void clist::addbegin(int element)
{
if (last!= NULL)
{
struct node *temp;
temp = new(struct node);
temp->info = element;
temp->next = last->next;
last->next = temp;
}
}
void clist::display()
{
struct node *temp;
if (last == NULL)
{
cout<<"List is empty, nothing to display"<<endl;
return;
}
temp=last->next;
cout<<"Circular Link List: "<<endl;
while (temp!= last)
{
cout<<temp->info<<"->";
temp=temp->next;
}
cout<<temp->info<<endl;
}
void clist::lshift(){
struct node *temp,*p;
temp=last->next;//starting node(headnode)
int tval=temp->info;
p=temp->next;
while(temp!=last)
{
temp->info=p->info;
temp=p;
p=p->next;
}
temp->info=tval;
}
void clist::rshift()
{
int tval,t1;
struct node *p,*q;
p=last->next;
tval=last->info;
while(p!=last)
{
q=p->next;
t1=q->info;
q->info=p->info;
p->info=tval;
tval=q->info;
p=p->next;
}
}