我想用一个循环链表,保存鼠标的最后5次操作,第六次操作覆盖第一个节点,第七次操作覆盖第二个节点,依次往下
节点为  5个,
1                 2                       3                   4                    5 |                 |
6(第六次操作)   7(第七次操作)
想用循环链表实现
给个思路 或者实现代码,相关的也行
我就90分了 
全给了 
谢谢

解决方案 »

  1.   

    数组的话也行。弄个长度为5的结构数组每次获得新的操作后,将a[1]放到a[0]中....a[4]放到a[3]中,最后将你的最新的那个放到空出来的a[4]中这样就能保证每次都是最后的五个。只是这样的话,每次都要4次元素移动
      

  2.   

    你去看看<数据结构>就知道了,队列很多都是用循环链表实现的
      

  3.   


    #include <iostream>using namespace std;typedef struct _Node
    {     
    _Node(){data = 0;}
    int   data;   
    struct _Node *Next;   
    }Node;   class   CChain   
    {   
    public:   
    CChain()   {first =   NULL;}   
    ~CChain() {Clear();};   
        
        
    void InitChain();
    void Insert(int);
        void Clear();
    void Output();
    private:   
    void DeleteNext();
    Node   *first;   
    Node   *last;
    };   
    void CChain::Clear()
    {
    if(first)
    {
    Node* p = first->Next;
    while(p != first)
    {
    Node* pTemp = p;
    p = p->Next;
    delete pTemp;
    } delete p;
    // delete first;
    } first = NULL;
    last = NULL;
    }void CChain::Insert(int iData)
    {
    last->data = iData;
    last = last->Next;
    }void CChain::InitChain()
    {
    Clear();
    for(int i = 0; i < 5; i++)
    {
    Node* node = new Node;
    if(i == 0)
    {
    first = node;
    last = node;
    }
    else 
    {
    last->Next = node;
    last = node;
    } if(i == 4)
    {
    last->Next = first;
    last = first; // 从first开始输入数据
    }
    }

    }void CChain::Output()
    {
    cout << first->data << "-";
    Node* p = first->Next;
    while(p != first)
    {
    cout << p->data << "-";
    p = p->Next;
    }}int main(int argc, char* argv[])
    {
    CChain chain;
    chain.InitChain();
    chain.Insert(0);
    chain.Insert(4);
    chain.Insert(2);
    chain.Insert(8);
    chain.Insert(1);
    chain.Insert(2);
    chain.Insert(5);
    chain.Insert(6);
    chain.Output();
    return 0;
    }
    很久没做数据结构方面的东西了
    按照lz的意思,自己练练手希望高手们指出错误。
      

  4.   

    struct  TYPE;
    int g_curIndex = 0;
    #define MAXTYPE 5
    TYPE g_ty[5];
    TYPE GetCurType(int n)
    {
        return g_ty[n];
    }
    TYPE AddCurType()
    {
        g_ty[g_curIndex++%MAXTYPE];
        if((g_curIndex % MAXTYPE) == 0)g_curIndex = 0;
    }
    TYPE 是任意自定义结构,也可以int,double等基本类型
    g_curIndex  是全局指针下标
    MAXTYPE 是数组长度
    g_ty  是全局指针下标
      

  5.   

    上面TYPE 没加进去;
    struct  TYPE; 
    int g_curIndex = 0; 
    #define MAXTYPE 5 
    TYPE g_ty[5]; 
    TYPE GetCurType(int n) 

        return g_ty[n]; 

    TYPE AddCurType(TYPE &ty) 

        g_ty[g_curIndex++%MAXTYPE] = ty; 
        if((g_curIndex % MAXTYPE) == 0)g_curIndex = 0; 

    TYPE 是任意自定义结构,也可以int,double等基本类型 
    g_curIndex  是全局指针下标 
    MAXTYPE 是数组长度 
    g_ty  是全局指针下标
      

  6.   

    struct BrowseHistory
    {
    CString HYLink;
    BrowseHistory *front;
    BrowseHistory *next;
    };BrowseHistory *head= new BrowseHistory;
    BrowseHistory *temp = head;
    head->front=NULL;
    head->next =NULL;
    BrowseHistory *end;
    for(int i = 0;i<6;i++)
    {
    end = new BrowseHistory;
    end->front = head;
    end->next = NULL;
    head->next = end;
    head = end;
    }
    head->next = temp;
    temp->front = head;
    //7个节点的很简单的双循环链表,记录浏览历史用的,刚写的不知道LZ能用不,