读书人

求帮忙数据结构基于链表的线性表的实

发布时间: 2012-10-20 14:12:48 作者: rapoo

求帮忙,数据结构基于链表的线性表的实现问题?
#include<iostream>
using namespace std;
template <class Elem>
class Link{
public:
Elem element;
Link*next;
Link(const Elem& elemval,Link* nextval=NULL)
{
element=elemval;
next=nextval;
}
Link(Link* nextval=NULL)
{
next=nextval;
}
};

template <class Elem>
class LList:public Link<Elem>{
private:
Link<Elem>* head;
Link<Elem>* tail;
Link<Elem>* fence;
int leftcnt;
int rightcnt;
void init(){
fence=tail=head=new Link<Elem>;
leftcnt=rightcnt=0;
}
void removeall(){
while(head!=NULL){
fence=head;
head=head->next;
delete fence;
}
}
public:
LList(int DefaultListSize){
init();
}
~LList(){
removeall();
}
void clear(){
removeall();
init();
}
bool insert(const Elem&);
bool append(const Elem&);
bool remove(Elem&);
void setStart(){
fence=head;
rightcnt+=leftcnt;
leftcnt=0;
}
void setEnd(){
fence=tail;
leftcnt+=rightcnt;
rightcnt=0;
}
void prev();
void next(){
if(fence!=tail){
fence=fence->next;
leftcnt++;
righgtcnt--;
}
}
int leftLength() const{
return leftcnt;
}
int rightLength() const{
return rightcnt;
}
bool setPos(int pos);
bool getValue(Elem& it) const{
if(rightcnt==0) return false;
it=fence->next->element;
return true;
}
void print() const;
//新增的成员函数:实现倒置线性表元素的顺序的功能
bool reset(Link<Elem> * &newhead);
};
template<class Elem>
bool LList<Elem>::insert(const Elem& item){
fence->next=new Link<Elem>(item,fence->next);
if(fence==tail) tail=fence->next;
rightcnt++;
return true;
}
template<class Elem>
bool LList<Elem>::append(const Elem& item){
tail=tail->next=new Link<Elem>(item,NULL);
rightcnt++;
return true;
}
template<class Elem>
bool LList<Elem>::remove(Elem& item){
if(fence->next==NULL) return false;
item=fence->next->element;
Link<Elem>* Itemp=fence->next;
fence->next=Itemp->next;
if(tail==Itemp) tail=fence;
delete Itemp;
rightcnt--;
return true;
}
template<class Elem>
void LList<Elem>::prev(){
Link<Elem>* Temp=head;
if(fence==head) return;
while(Temp->next!=fence)
Temp=Temp->next;
fence=Temp;
leftcnt--;
rightcnt++;
}
template<class Elem>
bool LList<Elem>::setPos(int pos){
if(pos<0||pos>rightcnt+leftcnt) return false;
fence=head;
for(int i=0;i<pos;i++)
fence=fence->next;
rightcnt=rightcnt+leftcnt-pos;
leftcnt=pos;
return true;
}
template<class Elem>
void LList<Elem>::print() const{
Link<Elem> *temp=head;
cout<<"<";
while(temp!=fence){
cout<<temp->next->element<<" ";
temp=temp->next;
}
cout<<"|";
while(temp->next!=NULL){
cout<<temp->next->element<<" ";
temp=temp->next;
}
cout<<">\n";
}
template<class Elem>
bool LList<Elem>::reset(Link<Elem>* & newhead){
Link<Elem>* ptr=head;
Link<Elem>* sp;
while(ptr!=NULL){
if(newhead==NULL)
newhead=head;
else{
sp=new Link<Elem>();
sp=ptr;
sp->next=newhead;
newhead=sp;
}
ptr=ptr->next;
delete sp;
}
return true;


}

int main()
{
double num;
bool sign;
LList<double>* project=NULL;
cout<<"任意输入10个double型数据可以实现倒置输出:\n";
for(int i=0;i<10;i++){
cin>>num;
sign=project->append(num);
if(!sign) cout<<"数据存储失败\n";
}
project->print();
}

在单步调试中发现append函数执行时,出现内存读取错误~~我检查n遍都没有觉得有哪不对的!

[解决办法]
你就不能用这种格式贴代码么:

C/C++ code
void CGMIPlayerDemoDlg::OnBnClickedPtzZoomOut(){     ;    CVideoWnd* lpVideoWnd = GetSelectedWindowClass() ;    if( lpVideoWnd )    {        if( m_IsDptz )            {            lpVideoWnd->Dptz_zoom(-1) ;        }else{            //PTZ//!todo:!需要定义ptz的速度区间,暂用100            static BOOL ctrl_start_stop = FALSE ;            lpVideoWnd->Ptz_zoom(0,ctrl_start_stop,m_HSpeed,m_VSpeed) ;            ctrl_start_stop=!ctrl_start_stop ;        }        lpVideoWnd->Invalidate(TRUE);    }}
[解决办法]
给你科普下,添加代码,]是这[

读书人网 >C++

热点推荐