cylist

 avatar
user_8384735
c_cpp
2 years ago
1.8 kB
6
Indexable
Never
//linklistHW.cpp
#include <iostream>
using namespace std;
#define shift(x) x = x->next
class Linklist;
class ListNode{
private:
	int data;
		ListNode *next;
public:
	ListNode():data(0),next(0){};
	ListNode(int x):data(x),next(0){};

	friend class Linklist;
};
class Linklist{
private:
	bool hascycle(){}
public:
	ListNode *head, *rear;
	ListNode *povit;
	Linklist() : head(0), rear(0){};
	void printList();
	void push(int x);
	void Delete(ListNode*, ListNode*);
	void game(int m);
	void setpovit(int k);
};
void Linklist::printList(){
	cout << head->data;
	for (ListNode *cur = head->next; cur; shift(cur)){
		if (cur == head) break;
		cout << " " << cur->data;
	}
	cout << endl;
}
void Linklist::push(int x){
	ListNode *newnode = new ListNode(x);
	if (!head){
		head = rear = newnode;
		head->next = rear->next = head;
	}
	else {
		rear->next = newnode;
		shift(rear);
		newnode->next = head;
	}

}
void Linklist::Delete(ListNode *Before, ListNode *x){

	ListNode *p = x;
	ListNode *ori = head;
	if (x == head){
		head = povit = x->next;
		do shift(x); while (x->next != ori);
		x->next = head;
	}else {
		p = Before->next;
		Before->next = povit = x->next;
		delete p;
		p->next = x->next;
	}
	
}
void Linklist::game(int m){
	
	ListNode *cur = povit, *p = NULL;
	for (int i = 1; i <= m; i++, shift(cur)){
		if (i == m){
			cout << endl << cur->data << " deleted\n";
			Delete(p, cur);
			break;
		}
		p = cur;
	}
	
}
void Linklist::setpovit(int k){
	povit = head;
	for (int i = 1; i < k; i++)
		shift(povit);
}
int main(){
	Linklist list1;
	int n, m, k;
	cin >> n >> k >> m;
	
	for (int i = 1; i <= n; i++)
		list1.push(i);
	

	list1.setpovit(k);
	for (int i = 0; i < n; i++){
		list1.game(m);
	}
}