| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 380 人关注过本帖
标题:数据结构中使用C++进行双向循环链表的相关操作,
只看楼主 加入收藏
chuandaobozi
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2011-12-9
收藏
 问题点数:0 回复次数:0 
数据结构中使用C++进行双向循环链表的相关操作,
#pragma once
#include<iostream>
using namespace std;
template<class T>
class CalDoubleNode;
template<class T>
class Node
{friend class CalDoubleNode<T>;
private:
    Node<T>*left,*right;
    T data;
};
template<class T>
class CalDoubleNode
{
public:
    CalDoubleNode(void);
    ~CalDoubleNode(void);
    bool IsEmpty(){return head==0;}
    bool Find(int k,T&x)const;
    int Length();
    CalDoubleNode<T>& Insert(int k,const T&x);
    CalDoubleNode<T>& Delete(int k,T&x);
    void Print()const;//正向输出
    void ResPrint()const;//逆向输出
private:
    Node<T>*head;//双向循环链表的头结点
};
template<class T>
CalDoubleNode<T>::CalDoubleNode()
{
    head->left =head;
    head->right =head;   
}
template<class T>
CalDoubleNode<T>::~CalDoubleNode()
{
    Node<T>*next;
    while(head!=head->left )
    {
        next=head->right ;
        delete head;
        head=next;
    }
    delete head;
}
template<class T>
int CalDoubleNode<T>::Length ()
{
    Node<T>*Curr=head;
    int index=0;
    while(Curr)
    {
        Curr=Curr->right ;
        index++;
    }
    return index;
}
template<class T>
bool CalDoubleNode<T>::Find (int k,T&x)const
{
    if(k<=0||k>Length())
    {
        cout<<"错误输入,退出!"<<endl;
        exit(1);
    }
    else
    {
        Node<T>*Curr=head;
        int index=1;
        while(Curr&&index<k)
        {
            Curr=Curr->right;
            index++;
        }
        if(k==index)
        {
            x=Curr->data;
            return true;
        }
        return false ;
    }
}
template<class T>
CalDoubleNode<T>&CalDoubleNode<T>::Insert(int k,const T&x)//Insert after k'th element;
{
    if(k<0||k>Length())
    {
        cout<<"错误输入,退出!"<<endl;
        exit(1);
    }
    else
    {
        Node<T>*Curr=head;
        Node<T>*newNode=new Node<T>;
        newNode->data =x;
        int index=1;
        if(k==0)
        {
            if(!Curr)//原始链表为空时
            {
                newNode->right =newNode->left =newNode;
                head=newNode;
            }
            else
            {
                newNode->right =head;
                newNode->left=head->left ;
                head->left =newNode;
                head=newNode;
            }
        }
        else
        {
            while(Curr&&index<k)
            {
                Curr=Curr->right ;
                index++;
            }
            if(Curr==head->left )// 作为最后一个元素插入的时候
            {
                newNode->left =Curr;
                Curr->right =newNode;
                newNode->right =head;
                head->left =newNode;
            }
            else
            {
                newNode->right =Curr->right ;
                Curr->right ->left =newNode;
                newNode->left =Curr;
                Curr->right =newNode;
            }

        }
    }
    return *this;
}
template<class T>
void CalDoubleNode<T>::Print()const
{
    Node<T>*CurrPtr=head;
    while(CurrPtr!=head->left)
    {
        cout<<CurrPtr->data <<" " ;
        CurrPtr=CurrPtr->right ;
    }
    cout<<CurrPtr->data <<" " ;
}
template<class T>
void CalDoubleNode<T>::ResPrint ()const
{
    Node<T>*CurrPtr=head;
    while(CurrPtr!=head->right )
    {
        cout<<CurrPtr->data <<" ";
        CurrPtr=CurrPtr->left ;
    }
    cout<<CurrPtr->data <<" ";
}
请问这个构造函数和插入函数为何好像编译不过啊,求大婶指点哈子
搜索更多相关主题的帖子: class private include public return 
2011-12-09 16:11
快速回复:数据结构中使用C++进行双向循环链表的相关操作,
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.025754 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved