Rotate an array and a linked list

Go down

Rotate an array and a linked list

Post  Admin on Sun Aug 03, 2008 6:03 am

Suppose we have an array A having n elements. You have to rotate the array by k positions.
For e.g. if we have the array A={1,2,3,4,5} and k=2 then the resulting array should be {3,4,5,1,2}. Try to post an in-place solution. The array need not be sorted originally. The question has multiple solutions. Try to discuss all.

Now what will be the approach if you want to rotate a singly linked list instead of an array.

Admin
Admin

Posts : 39
Join date : 2008-08-03

View user profile http://computerclub09mnnit.forumotion.net

Back to top Go down

Re: Rotate an array and a linked list

Post  mnnit.rahul on Mon Aug 11, 2008 3:20 am

# include<iostream>
using namespace std;
main()
{int i,n,k;
cout<<"enter size of array";
cin>>n;
int a[n];
for(i=0;i<n;++i)
cin>>a[i];
cout<<"\nenter k";
cin>>k;
k=k%n;
for(i=0;i<n;++i)
{if(i+k<n)
cout<<a[i+k]<<" ";
else
cout<<a[i+k-n]<<" ";
}
// system("pause");
}

mnnit.rahul

Posts : 16
Join date : 2008-08-10

View user profile

Back to top Go down

mnnit.rahul

Post  bhagalpur on Tue Aug 12, 2008 1:58 am

You dont have to print the answer ...
you have to change the original array ..

bhagalpur

Posts : 3
Join date : 2008-08-03

View user profile

Back to top Go down

Re: Rotate an array and a linked list

Post  sweetesh singh on Tue Aug 12, 2008 9:30 am

printf("enter the size and k");
scanf("%d%d",&n,&k);
for(j=0;j<k;j++)
{
c=a[0];
for(i=0;i<n-1;i++)
a[i]=a[i+1];
a[n-1]=c;
}

now print array a[];

sweetesh singh

Posts : 8
Join date : 2008-08-12

View user profile

Back to top Go down

Re: Rotate an array and a linked list

Post  sweetesh singh on Tue Aug 12, 2008 9:34 am

for rotating the list , first break the list at pont k(measured from front),then attach it to the last point.point of attachment should be tail->head.

sweetesh singh

Posts : 8
Join date : 2008-08-12

View user profile

Back to top Go down

rotating the list.......

Post  prani on Tue Aug 12, 2008 1:05 pm

this should work...
1->get to the kth element from the last
2->point this element to the head
3->reverse the list beyond k

prani

Posts : 15
Join date : 2008-08-12

View user profile

Back to top Go down

Re: Rotate an array and a linked list

Post  sweetesh singh on Wed Aug 13, 2008 2:18 am

tiwari ji
please explain the working by taking an example, i am unable to get it

sweetesh singh

Posts : 8
Join date : 2008-08-12

View user profile

Back to top Go down

rotate a singly linked list

Post  harit on Sat Sep 13, 2008 1:41 pm

void rotate(int n)
{
node *r;
r=start;
while(r->next!=NULL)
{
r=r->next;

}
while(n--)
{
r->next=start;
r=start;
start=start->next;
cout<<r->n<<endl;
r->next=NULL;
}
}

harit

Posts : 2
Join date : 2008-09-09

View user profile

Back to top Go down

Re: Rotate an array and a linked list

Post  Sponsored content


Sponsored content


Back to top Go down

Back to top

- Similar topics

 
Permissions in this forum:
You cannot reply to topics in this forum