Skip to content

Instantly share code, notes, and snippets.

@imintegral
Last active August 29, 2015 14:20
Show Gist options
  • Save imintegral/041a71748bf5f6a1184b to your computer and use it in GitHub Desktop.
Save imintegral/041a71748bf5f6a1184b to your computer and use it in GitHub Desktop.
2 directions list
/*
Zadanie polegające na tasowaniu talii kart od 1...n.
Operacja A: przełożenie pierwszej karty na koniec.
Operacja B: przełożenie drugiej karty na koniec.
Wejście: n oraz operacje w jednym stringu (np. abaaabbabaababbaa)
Wyjście: potasowana talia
*/
#include<iostream>
using namespace std;
struct element
{
element* previous;
element* next;
int value;
};
element* first_element = NULL;
element* last_used_element = NULL;
element* final_element = NULL;
element* temp = NULL;
void add_element(int adding_value)
{
if(first_element == NULL)
{
first_element = new element;
first_element->previous = NULL; //rooting
first_element->next = NULL;
first_element->value = adding_value; //add val
last_used_element = first_element; //temp
}
else
{
final_element = new element;
final_element->previous = last_used_element; //connecting
last_used_element->next = final_element; //connecting
final_element->next = NULL;
final_element->value = adding_value; //add val
last_used_element = final_element; //temp
}
}
void operation(char op)
{
if(op == 'A' || op == 'a')
{
add_element(first_element->value);
first_element = first_element->next;
first_element->previous = NULL;
}
if(op == 'B' || op == 'b')
{
temp = first_element->next; //temp == 2nd element
add_element(temp->value); //add 2nd element at the end
first_element->next = temp->next; //connecting 1st with 3rd element
temp->previous = first_element; //connecting 3rd (now 2nd) element with 1st aback
delete temp;
}
}
void create_deck(int n)
{
for(int i=1;i<=n;i++)
add_element(i);
}
void do_operations(string s)
{
for(int i=0;i<s.length();i++)
operation(s[i]);
}
void show_list()
{
element* iterator_of_list = NULL;
iterator_of_list = first_element;
while(iterator_of_list!=NULL)
{
cout<<iterator_of_list->value<<' ';
iterator_of_list=iterator_of_list->next;
}
}
int main()
{
int n;
string ops;
cin>>n>>ops; //input
create_deck(n);
do_operations(ops);
show_list();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment