#include
#include
class SingleList;
class Node{
private:
int data;
Node*link;
friend class SingleList;
};
class SingleList{
public:
SingleList(){ first=NULL; n=0; }
~SingleList();
//
bool IsEmpty() const;
//
int Length() const;
//
bool Find(int i,int & x) const;//将链表的每i个结点的值通过x返回
//
int Search(intx) const;
//查找给定的x,返回是链表中的第几个结点,若不存在则返回0
bool Insert(int i,intx);
//在链表的每i个结点后插入值为x的结点
//
bool Delete(int i);
//删除链表的每i个结点
//
bool Update(int i,intx);
//对链表的每i个结点的值用x的值更新
void Output(ostream& out) const; //输出链表每一个结点的值
friend ostream& operatorlink;
delete q;
}
}
bool SingleList::Insert(int i,intx)
//在链表的每i个结点后插入值为x的结点
{
Node *p=first,*q;
q=new Node;
q->data=x;
q->link=NULL;
if(i>0){
while(--i)
p=p->link;
p->link=q;
}
else if(i==0)
first=q;
else
return false;
n++;
return true;
}
void SingleList::Output(ostream& out) const //输出链表每一个结点的值
{
Node *p=first;
for(int i=0;idatalink;
}
}
ostream& operator<<(ostream& out, const SingleList &t) //重载输出流调用Output函数,输出链表
{
t.Output(out);
return out;
}
|