软件技术基础课的作业,我到现在才开始补。挺简单的一道题,就是我比较钻牛角尖。非得弄一个类。竟然写了半天。 不过课程倒是很水的。所以今天没去上,专心调程序了。
Code
/**//*
Wednesday, March 25, 2009
3:40:37 PM
Problem: 使用链表编写幸存者游戏
X人坐飞艇,由于飞艇漏气,大家一致同意其中Y人跳伞。这些人围成一圈,由第一个人开始,依次报数,
数到第Z人,该人跳伞,然后从他的下一个人数起,同样数到第Z人,再跳伞,如此循环,直到Y个乘客跳伞为止。
试用单循环链表来解决本问题,当输入XYZ后输出跳伞的乘客(注:乘客用1~X的整数表示)
Environment: Visual Studio 2008 + Visual Assist + ViEmu
Comment: it seems like I'm customed to the coding style of emacs unber ubuntu.
*/
#include<iostream>
using namespace std;
const int NoPosition = -1;
/**//*
main class with a nested to solve this proble, but not only this. It include methods which can
transform a singly linked list to a circular linked list and reverse the order of a singly linked
list, and so on.
It's very difficult to write a perfect class, and this class should be improved in the following
aspects:
1, add template support
2, add exception handling
3, reuse of the code, via adding a template pointer, like iterators in STL
*/
class List
{
public:
List()
{
init();
}
List(int numberOfNode) //initialize List with numberOfNode '0's
{
init(numberOfNode);
ListNode *pivot = head;
for(int i = 0; i < numberOfNode; i++)
{
ListNode *LNode = new ListNode(i + 1, pivot->next);
pivot->next = LNode;
pivot = pivot->next;
}
pivot->next = tail;
}
~List()
{
ListNode *pivot = head;
while(pivot->next != tail)
{
ListNode *temp = pivot;
pivot = pivot->next;
delete temp;
}
delete tail;
}
int findFirst(int item)
{
int position = 1;
ListNode *pivot = head->next;
while(pivot->next != NULL)
{
if(pivot->data == item)
return position;
else
{
position++;
pivot = pivot->next;
}
}
}
void insert(int item, int position) //insert item at position
{
ListNode *pivot = head;
for(int i = 1; i < position; i++)
{
pivot = pivot->next;
}
ListNode *LNode = new ListNode(item, pivot->next);
pivot->next = LNode;
pivot = pivot->next;
itemNumber++;
}
void remove(int position) //remove Node at position
{
ListNode *pivot = head;
for(int i = 1; i < position; i++)
{
pivot = pivot->next;
}
ListNode *temp = pivot->next;
pivot->next = temp->next;
delete temp;
itemNumber--;
}
//main algorithm to solve this problem, operate on a circular linked list, easy to understand,
void removeInterval(int ElemNum, int Interval)
{
ListNode *pivot = head;
for(int i = 0; i < ElemNum; i++)
{
for(int j = 1; j < Interval; j++)
{
pivot = pivot->next;
}
ListNode *temp = pivot->next;
pivot->next = temp->next;
cout << "the number of the " << i + 1 << "th passenger who got off the ariplane is: " << temp->data << endl;
delete temp;
itemNumber--;
}
}
int getItem(int position) //return item in position
{
ListNode *pivot = head;
for(int i = 1; i <= position; i++)
pivot = pivot->next;
return pivot->data;
}
void showList(int beginPos, int endPos) //see what your list contains
{
ListNode *pivot = head;
for(int i = 1; i <= beginPos; i++)
pivot = pivot->next;
for(int i = beginPos; i <= endPos; i++)
{
if(pivot->data != NoPosition)
cout << pivot->data << ' ';
pivot = pivot->next;
}
cout << endl;
}
int size()
{
return itemNumber;
}
void reverse() //reverse the order of a singly linked list
{
ListNode *prev = NULL;
ListNode *pivot = head;
while(pivot != NULL)
{
ListNode *temp = pivot->next;
pivot->next = prev;
prev = pivot;
pivot = temp;
}
tail = head;
head = prev;
}
void toCircularLinkedList() //transform a singly linked list to a circular
{
ListNode *pivot = head;
while(pivot->next != tail)
pivot = pivot->next;
pivot->next = head->next;
ListNode *temptail = tail;
tail = pivot;
delete temptail;
}
void assign(int position, int item) //assign the item in position with another value
{
ListNode *pivot = head;
for(int i = 1; i <= position; i++)
{
pivot = pivot->next;
}
pivot->data = item;
}
private:
struct ListNode
{
public:
ListNode(int d = 0, ListNode *L = NULL)
: data(d), next(L){}
int data;
ListNode *next;
};
int itemNumber;
ListNode *head;
ListNode *tail;
void init(int n = 0)
{
tail = new ListNode();
head = new ListNode(0, tail);
itemNumber = n;
}
};
int main()
{
int x, y, z;
cout << "please input x, y, z in order: " << endl;
cin >> x >> y >> z;
List L(x);
L.toCircularLinkedList();
//L.showList(1, L.size());
L.removeInterval(y, z);
//L.showList(1, L.size());
cout << endl;
return 0;
}
/**//*
Wednesday, March 25, 2009
3:40:37 PM
Problem: 使用链表编写幸存者游戏
X人坐飞艇,由于飞艇漏气,大家一致同意其中Y人跳伞。这些人围成一圈,由第一个人开始,依次报数,
数到第Z人,该人跳伞,然后从他的下一个人数起,同样数到第Z人,再跳伞,如此循环,直到Y个乘客跳伞为止。
试用单循环链表来解决本问题,当输入XYZ后输出跳伞的乘客(注:乘客用1~X的整数表示)
Environment: Visual Studio 2008 + Visual Assist + ViEmu
Comment: it seems like I'm customed to the coding style of emacs unber ubuntu.
*/
#include<iostream>
using namespace std;
const int NoPosition = -1;
/**//*
main class with a nested to solve this proble, but not only this. It include methods which can
transform a singly linked list to a circular linked list and reverse the order of a singly linked
list, and so on.
It's very difficult to write a perfect class, and this class should be improved in the following
aspects:
1, add template support
2, add exception handling
3, reuse of the code, via adding a template pointer, like iterators in STL
*/
class List
{
public:
List()
{
init();
}
List(int numberOfNode) //initialize List with numberOfNode '0's
{
init(numberOfNode);
ListNode *pivot = head;
for(int i = 0; i < numberOfNode; i++)
{
ListNode *LNode = new ListNode(i + 1, pivot->next);
pivot->next = LNode;
pivot = pivot->next;
}
pivot->next = tail;
}
~List()
{
ListNode *pivot = head;
while(pivot->next != tail)
{
ListNode *temp = pivot;
pivot = pivot->next;
delete temp;
}
delete tail;
}
int findFirst(int item)
{
int position = 1;
ListNode *pivot = head->next;
while(pivot->next != NULL)
{
if(pivot->data == item)
return position;
else
{
position++;
pivot = pivot->next;
}
}
}
void insert(int item, int position) //insert item at position
{
ListNode *pivot = head;
for(int i = 1; i < position; i++)
{
pivot = pivot->next;
}
ListNode *LNode = new ListNode(item, pivot->next);
pivot->next = LNode;
pivot = pivot->next;
itemNumber++;
}
void remove(int position) //remove Node at position
{
ListNode *pivot = head;
for(int i = 1; i < position; i++)
{
pivot = pivot->next;
}
ListNode *temp = pivot->next;
pivot->next = temp->next;
delete temp;
itemNumber--;
}
//main algorithm to solve this problem, operate on a circular linked list, easy to understand,
void removeInterval(int ElemNum, int Interval)
{
ListNode *pivot = head;
for(int i = 0; i < ElemNum; i++)
{
for(int j = 1; j < Interval; j++)
{
pivot = pivot->next;
}
ListNode *temp = pivot->next;
pivot->next = temp->next;
cout << "the number of the " << i + 1 << "th passenger who got off the ariplane is: " << temp->data << endl;
delete temp;
itemNumber--;
}
}
int getItem(int position) //return item in position
{
ListNode *pivot = head;
for(int i = 1; i <= position; i++)
pivot = pivot->next;
return pivot->data;
}
void showList(int beginPos, int endPos) //see what your list contains
{
ListNode *pivot = head;
for(int i = 1; i <= beginPos; i++)
pivot = pivot->next;
for(int i = beginPos; i <= endPos; i++)
{
if(pivot->data != NoPosition)
cout << pivot->data << ' ';
pivot = pivot->next;
}
cout << endl;
}
int size()
{
return itemNumber;
}
void reverse() //reverse the order of a singly linked list
{
ListNode *prev = NULL;
ListNode *pivot = head;
while(pivot != NULL)
{
ListNode *temp = pivot->next;
pivot->next = prev;
prev = pivot;
pivot = temp;
}
tail = head;
head = prev;
}
void toCircularLinkedList() //transform a singly linked list to a circular
{
ListNode *pivot = head;
while(pivot->next != tail)
pivot = pivot->next;
pivot->next = head->next;
ListNode *temptail = tail;
tail = pivot;
delete temptail;
}
void assign(int position, int item) //assign the item in position with another value
{
ListNode *pivot = head;
for(int i = 1; i <= position; i++)
{
pivot = pivot->next;
}
pivot->data = item;
}
private:
struct ListNode
{
public:
ListNode(int d = 0, ListNode *L = NULL)
: data(d), next(L){}
int data;
ListNode *next;
};
int itemNumber;
ListNode *head;
ListNode *tail;
void init(int n = 0)
{
tail = new ListNode();
head = new ListNode(0, tail);
itemNumber = n;
}
};
int main()
{
int x, y, z;
cout << "please input x, y, z in order: " << endl;
cin >> x >> y >> z;
List L(x);
L.toCircularLinkedList();
//L.showList(1, L.size());
L.removeInterval(y, z);
//L.showList(1, L.size());
cout << endl;
return 0;
}