Skip to content

Instantly share code, notes, and snippets.

@PoojaB26
Last active August 8, 2017 17:30
Show Gist options
  • Save PoojaB26/4d8569e4e504d8a00bd2ad375dcc48ce to your computer and use it in GitHub Desktop.
Save PoojaB26/4d8569e4e504d8a00bd2ad375dcc48ce to your computer and use it in GitHub Desktop.
#include<stdio.h>
#include<stdlib.h>
typedef struct n{
int data;
struct n* next;
}node;
node* root=NULL;
node* makenull(){
node* nwd=(node*)malloc(sizeof(node));
nwd->next=NULL;
return nwd;
}
void insert(int info){
node* nw=makenull();
if(root==NULL){
nw->data=info;
root=nw;
}
else{
nw->data=info;
node *p=root;
node* q;
while(p!=NULL){
q=p;
p=p->next;
}
q->next=nw;
}
}
void moveToFirst(node *head){
node *p=root;
node* q=NULL;
while(p->next!=NULL){
p=p->next;
}
p->next=head;
root=head->next;
head->next=NULL;
}
void printList(node *nod)
{
while(nod->next != NULL)
{
printf("%d\n", nod->data);
nod = nod->next;
}
printf("%d\n", nod->data);
}
int main(){
int n,i,dist;
scanf("%d",&n);
scanf("%d",&dist);
for(i=1;i<=n;i++)
insert(i);
for(i=0;i<(dist-n);i++)
moveToFirst(root);
printList(root);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment