Skip to content

Instantly share code, notes, and snippets.

@lsiddiqsunny
Created September 6, 2017 19:09
Show Gist options
  • Save lsiddiqsunny/b2542a7e5762775c26f4e1fcd3193cf1 to your computer and use it in GitHub Desktop.
Save lsiddiqsunny/b2542a7e5762775c26f4e1fcd3193cf1 to your computer and use it in GitHub Desktop.
#include<bits/stdc++.h>
using namespace std;
struct linkedlist
{
int data;
linkedlist *next;
} *root;
void appand(int newdata)
{
if(root==NULL)
{
root=new linkedlist();
root->data=newdata;
// cout<<"here";
root->next=root;
}
else if(root->next==root)
{
root->next=new linkedlist();
root->next->data=newdata;
root->next->next=root;
}
else
{
linkedlist *current_node=root;
while(current_node->next!=root)
{
current_node=current_node->next;
}
linkedlist *temp=new linkedlist();
temp->data=newdata;
temp->next=root;
current_node->next=temp;
}
// cout<<"here"<<endl;
}
linkedlist *Search( int data)
{
linkedlist *ptr = root;
while(ptr != NULL && ptr->data != data)
{
ptr = ptr->next;
}
return ptr;
}
void Delete(int olddata)
{
linkedlist *n = Search(olddata);
linkedlist *ptr = root;
if(ptr == NULL)
{
cout << "List is empty";
}
else if(ptr == n)
{
linkedlist *temp=root;
while(temp->next!=root)
{
temp=temp->next;
}
ptr->next = n->next;
temp->next=n->next;
}
else
{
while(ptr->next != n)
{
ptr = ptr->next;
}
ptr->next = n->next;
}
root=ptr->next;
}
void print()
{
linkedlist *current_node=root;
do
{
cout<<current_node->data<<" ";
current_node=current_node->next;
}
while(current_node!=root);
cout<<endl;
}
int main()
{
int n,k;
cin>>n>>k;
for(int i=1; i<=n; i++)
{
appand(i);
}
int SIZE=n;
int y;
for(int i=0; i<k; i++)
{
cin>>y;
print();
if(y<=SIZE)
{
int ans;
linkedlist *temp=root;
while(y--)
{
temp=temp->next;
}
cout<<temp->data<<" ";
Delete(temp->data);
SIZE--;
}
else
{
//print();
int z=y-SIZE+1;
z=z%SIZE;
linkedlist *temp=root;
if(z==0)
{
while(temp->next!= root)
{
temp=temp->next;
}
cout<<temp->data<<" ";
Delete(temp->data);
SIZE--;
continue;
}
while(z!=1)
{
temp=temp->next;
z--;
}
cout<<temp->data<<" ";
Delete(temp->data);
SIZE--;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment