Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <stdlib.h>
- #include <time.h>
- using namespace std;
- struct node {
- string val;
- node *next, *prev;
- };
- struct DLCL {
- node *head, *tail;
- void init() {
- head = NULL;
- tail = NULL;
- }
- void insertFirst(string x) {
- node *new_node = new node;
- new_node->val = x;
- if(head == NULL) {
- head = new_node;
- head->next = head;
- head->prev = head;
- tail = head;
- }
- else {
- new_node->next = head;
- head->prev = new_node;
- head = new_node;
- head->prev = tail;
- }
- }
- void insertLast(string x) {
- node *new_node = new node;
- new_node->val = x;
- if(head == NULL) {
- head = new_node;
- head->next = head;
- head->prev = head;
- tail = head;
- }
- else {
- tail->next = new_node;
- new_node->next = head;
- head->prev = new_node;
- new_node->prev = tail;
- tail = new_node;
- }
- }
- void deleteFirst() {
- if(head != NULL) {
- if(head->next == head) {
- delete head;
- head = NULL;
- tail = NULL;
- }
- else {
- node *tmp = head;
- head = head->next;
- head->prev = tail;
- tail->next = head;
- delete tmp;
- }
- }
- }
- void deleteLast() {
- if(head != NULL) {
- if(head->next == head) {
- delete head;
- head = NULL;
- tail = NULL;
- }
- else {
- node *tmp = tail;
- tail = tail->prev;
- tail->next = head;
- head->prev = tail;
- delete tmp;
- }
- }
- }
- void deleteAll() {
- while(head != NULL) {
- deleteFirst();
- }
- }
- void delete_node(node * tmp) {
- if(head != NULL) {
- if(head == tmp) {
- deleteFirst();
- return;
- }
- if(tail == tmp) {
- deleteLast();
- return;
- }
- tmp->prev->next = tmp->next;
- tmp->next->prev = tmp->prev;
- delete tmp;
- }
- }
- void print() {
- node *tmp = head;
- while(tmp != tail) {
- cout << tmp->val << " <--> ";
- tmp = tmp->next;
- }
- cout << tmp->val << endl;
- }
- };
- void igra(DLCL & igraci, DLCL & broevi) {
- node * igrac = igraci.head;
- node * broj = broevi.head;
- srand(time(NULL));
- int p = 0;
- int niza[] = {4, 2, 6};
- while(igraci.head->next != igraci.head) {
- int sluchaen;
- if(p < 2) {
- sluchaen = niza[p++];
- }
- else {
- sluchaen = 1 + (rand() % 6);
- }
- cout << "Na poteg e " << igrac->val <<", i ja frla kockat: " << sluchaen << endl;
- for(int i = 0; i < sluchaen; i++) {
- cout << broj->val << " " ;
- broj = broj->next;
- }
- cout << endl;
- cout << "Zastanuvame na pole: " << broj->val << endl;
- string br = "";
- br += (sluchaen + '0');
- if(broj->val == br) {
- cout << "Ovoj igrac ostanuva vo igra" << endl;
- node *tmp = broj;
- broj = broj->next;
- broevi.delete_node(tmp);
- igrac = igrac->next;
- }
- else {
- cout << "Igracot ispagja od igrata" << endl;
- node *tmp = igrac;
- igrac = igrac->next;
- igraci.delete_node(tmp);
- broj = broj->next;
- }
- cout << endl;
- }
- cout << "Pobednik: " << igraci.head->val << endl;
- }
- int main() {
- DLCL igraci, broevi;
- igraci.init();
- broevi.init();
- int br_na_igraci;
- cin >> br_na_igraci;
- for(int i = 0; i < br_na_igraci; i++) {
- string ime;
- cin >> ime;
- igraci.insertLast(ime);
- }
- int broj_na_broevi;
- cin >> broj_na_broevi;
- for(int i = 0; i < broj_na_broevi; i++) {
- string broj;
- cin >> broj;
- broevi.insertLast(broj);
- }
- igra(igraci, broevi);
- return 0;
- }
- /**
- 4
- Dejan
- Tina
- Marija
- Jane
- 7
- 1 6 5 3 4 1 5
- /*/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement