软件技术基础作业

      软件技术基础课的作业,我到现在才开始补。挺简单的一道题,就是我比较钻牛角尖。非得弄一个类。竟然写了半天。 不过课程倒是很水的。所以今天没去上,专心调程序了。

ContractedBlock.gif ExpandedBlockStart.gif Code
ExpandedBlockStart.gifContractedBlock.gif/**//*
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;

ExpandedBlockStart.gifContractedBlock.gif
/**//*
    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
ExpandedBlockStart.gifContractedBlock.gif
{
public:
    List()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        init();
    }

    List(
int numberOfNode)                        //initialize List with numberOfNode '0's
ExpandedSubBlockStart.gifContractedSubBlock.gif
    {
        init(numberOfNode);
        ListNode 
*pivot = head;
        
for(int i = 0; i < numberOfNode; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            ListNode 
*LNode = new ListNode(i + 1, pivot->next);
            pivot
->next = LNode;
            pivot 
= pivot->next;
        }

        pivot
->next = tail;
    }

    
~List()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        ListNode 
*pivot = head;
        
while(pivot->next != tail)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            ListNode 
*temp = pivot;
            pivot 
= pivot->next;
            delete temp;
        }

        delete tail;
    }

    
    
int findFirst(int item)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        
int position = 1;
        ListNode 
*pivot = head->next;
        
while(pivot->next != NULL)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
if(pivot->data == item)
                
return position;
            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                position
++;
                pivot 
= pivot->next;
            }

        }

    }

    
void insert(int item, int position)                    //insert item at position
ExpandedSubBlockStart.gifContractedSubBlock.gif
    {
        ListNode 
*pivot = head;
        
for(int i = 1; i < position; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            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
ExpandedSubBlockStart.gifContractedSubBlock.gif
    {
        ListNode 
*pivot = head;
        
for(int i = 1; i < position; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            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)    
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        ListNode 
*pivot = head;
        
for(int i = 0; i < ElemNum; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
for(int j = 1; j < Interval; j++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                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
ExpandedSubBlockStart.gifContractedSubBlock.gif
    {
        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
ExpandedSubBlockStart.gifContractedSubBlock.gif
    {
        ListNode 
*pivot = head;
        
for(int i = 1; i <= beginPos; i++)
            pivot 
= pivot->next;
        
for(int i = beginPos; i <= endPos; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
if(pivot->data != NoPosition)
                cout 
<< pivot->data << ' ';
            pivot 
= pivot->next;
        }

        cout 
<< endl;
    }

    
int size()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        
return itemNumber;
    }


    
void reverse()                                    //reverse the order of a singly linked list
ExpandedSubBlockStart.gifContractedSubBlock.gif
    {
        ListNode 
*prev = NULL;
        ListNode 
*pivot = head;
        
while(pivot != NULL)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            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
ExpandedSubBlockStart.gifContractedSubBlock.gif
    {
        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
ExpandedSubBlockStart.gifContractedSubBlock.gif
    {
        ListNode 
*pivot = head;
        
for(int i = 1; i <= position; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            pivot 
= pivot->next;
        }

        pivot
->data = item;
    }

    
private:
    
struct ListNode
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
    
public:
        ListNode(
int d = 0, ListNode *= NULL)
ExpandedSubBlockStart.gifContractedSubBlock.gif            : data(d), next(L)
{}
        
int data;
        ListNode 
*next;
    }
;

    
int itemNumber;
    ListNode 
*head;
    ListNode 
*tail;

    
void init(int n = 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        tail 
= new ListNode();
        head 
= new ListNode(0, tail);
        itemNumber 
= n;
    }

}
;

int main()
ExpandedBlockStart.gifContractedBlock.gif
{
    
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;
}

转载于:https://www.cnblogs.com/cnlox/archive/2009/03/25/1421451.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值