主题:线性表的链式存储
#include<iostream>
using namespace std;
struct Node
{
int data;
Node * next;
Node(int item,Node * link);
};
Node::Node(int item,Node * link)
{
data = item;
next = link;
}
class SimpleLinkList
{
protected:
Node * head;
Node * GetElemPtr(int position);
public:
~SimpleLinkList();
int Length();
bool Empty();
void Clear();
void Traverse();
void GetElem(int position);
void SetElem(int position,int e);
void Delete(int position);
void Insert(int position,int e);
};
Node * SimpleLinkList::GetElemPtr(int position)
{
Node * tmptr = head;
int curposition = 0;
while(tmptr->next != NULL&&curposition < position)
{
tmptr = tmptr->next;
curposition ++;
}
if(tmptr != NULL&&curposition == position)
{
return tmptr;
}
else
{
return NULL;
}
}
SimpleLinkList::~SimpleLinkList()
{
Clear();
delete head;
}
int SimpleLinkList::Length()
{
int count = 0;
for(Node * tmptr = head->next;tmptr != NULL;tmptr = tmptr->next)
{
count ++;
}
return count;
}
bool SimpleLinkList::Empty()
{
return head->next == NULL;
}
void SimpleLinkList::Clear()
{
while(Length() > 0)
{
Delete(1);
}
}
void SimpleLinkList::Traverse()
{
for(Node * tmptr = head->next;tmptr != NULL;tmptr = tmptr->next)
{
cout<<tmptr->data;
}
}
void SimpleLinkList::GetElem(int position)
{
if(position<1||position>Length())
{
cout<<"范围错"<<endl;
}
else
{
Node * tmptr;
tmptr = GetElemPtr(position);
}
}
void SimpleLinkList::SetElem(int position,int e)
{
if(position<1||position>Length())
{
cout<<"范围错"<<endl;
}
else
{
Node * tmptr;
tmptr = GetElemPtr(position);
tmptr->data=e;
}
}
void SimpleLinkList::Insert(int position,int e)
{
if(position<1||position>Length()+1)
{
cout<<"范围错"<<endl;
}
else
{
Node * tmptr;
tmptr = GetElemPtr(position-1);
Node * newptr;
newptr = new Node(e,tmptr->next);
tmptr->next = newptr;
}
}
void SimpleLinkList::Delete(int position)
{
if(position<0||position>Length())
{
cout<<"范围错"<<endl;
}
else
{
Node * tmptr;
tmptr = GetElemPtr(position-1);
Node * nextptr = tmptr->next;
tmptr->next = nextptr->next;
delete nextptr;
}
}
void print()
{
cout<<"以下是对线性表的操作:"<<endl;
cout<<"1.插入数据"<<endl;
cout<<"2.删除数据"<<endl;
cout<<"3.显示线性表长度"<<endl;
cout<<"4.该变某位置的数据"<<endl;
cout<<"5.显示线性表数据"<<endl;
cout<<"6.退出"<<endl;
}
int main()
{
SimpleLinkList list;
print();
int tu;
int place,data;
while(1)
{
int num;
cin>>num;
switch(num)
{
case 1:
cout<<"请输入插入数据的位置和数据:"<<endl;
cin>>place>>data;
list.Insert(place,data);
break;
case 2:
cout<<"请输入要删除数据的位置:"<<endl;
cin>>place;
list.Delete(place);
break;
case 3:
cout<<"线性表的长度为:";
cout<<list.Length();
break;
case 4:
cout<<"请输入要改变数据的位置和数据:"<<endl;
cin>>place>>data;
list.SetElem(place,data);
case 5:
cout<<"线性表中的数据为:"<<endl;
list.Traverse();
}
cin>>tu;
if(tu=6)
break;
}
return 0;
}
谁给看看这个程序的问题,我插入数据就停止了?
using namespace std;
struct Node
{
int data;
Node * next;
Node(int item,Node * link);
};
Node::Node(int item,Node * link)
{
data = item;
next = link;
}
class SimpleLinkList
{
protected:
Node * head;
Node * GetElemPtr(int position);
public:
~SimpleLinkList();
int Length();
bool Empty();
void Clear();
void Traverse();
void GetElem(int position);
void SetElem(int position,int e);
void Delete(int position);
void Insert(int position,int e);
};
Node * SimpleLinkList::GetElemPtr(int position)
{
Node * tmptr = head;
int curposition = 0;
while(tmptr->next != NULL&&curposition < position)
{
tmptr = tmptr->next;
curposition ++;
}
if(tmptr != NULL&&curposition == position)
{
return tmptr;
}
else
{
return NULL;
}
}
SimpleLinkList::~SimpleLinkList()
{
Clear();
delete head;
}
int SimpleLinkList::Length()
{
int count = 0;
for(Node * tmptr = head->next;tmptr != NULL;tmptr = tmptr->next)
{
count ++;
}
return count;
}
bool SimpleLinkList::Empty()
{
return head->next == NULL;
}
void SimpleLinkList::Clear()
{
while(Length() > 0)
{
Delete(1);
}
}
void SimpleLinkList::Traverse()
{
for(Node * tmptr = head->next;tmptr != NULL;tmptr = tmptr->next)
{
cout<<tmptr->data;
}
}
void SimpleLinkList::GetElem(int position)
{
if(position<1||position>Length())
{
cout<<"范围错"<<endl;
}
else
{
Node * tmptr;
tmptr = GetElemPtr(position);
}
}
void SimpleLinkList::SetElem(int position,int e)
{
if(position<1||position>Length())
{
cout<<"范围错"<<endl;
}
else
{
Node * tmptr;
tmptr = GetElemPtr(position);
tmptr->data=e;
}
}
void SimpleLinkList::Insert(int position,int e)
{
if(position<1||position>Length()+1)
{
cout<<"范围错"<<endl;
}
else
{
Node * tmptr;
tmptr = GetElemPtr(position-1);
Node * newptr;
newptr = new Node(e,tmptr->next);
tmptr->next = newptr;
}
}
void SimpleLinkList::Delete(int position)
{
if(position<0||position>Length())
{
cout<<"范围错"<<endl;
}
else
{
Node * tmptr;
tmptr = GetElemPtr(position-1);
Node * nextptr = tmptr->next;
tmptr->next = nextptr->next;
delete nextptr;
}
}
void print()
{
cout<<"以下是对线性表的操作:"<<endl;
cout<<"1.插入数据"<<endl;
cout<<"2.删除数据"<<endl;
cout<<"3.显示线性表长度"<<endl;
cout<<"4.该变某位置的数据"<<endl;
cout<<"5.显示线性表数据"<<endl;
cout<<"6.退出"<<endl;
}
int main()
{
SimpleLinkList list;
print();
int tu;
int place,data;
while(1)
{
int num;
cin>>num;
switch(num)
{
case 1:
cout<<"请输入插入数据的位置和数据:"<<endl;
cin>>place>>data;
list.Insert(place,data);
break;
case 2:
cout<<"请输入要删除数据的位置:"<<endl;
cin>>place;
list.Delete(place);
break;
case 3:
cout<<"线性表的长度为:";
cout<<list.Length();
break;
case 4:
cout<<"请输入要改变数据的位置和数据:"<<endl;
cin>>place>>data;
list.SetElem(place,data);
case 5:
cout<<"线性表中的数据为:"<<endl;
list.Traverse();
}
cin>>tu;
if(tu=6)
break;
}
return 0;
}
谁给看看这个程序的问题,我插入数据就停止了?