}
a'sage:
5
a'sage:
6
b'sage:
6
a'sage:
7
b'sage:
7
是进行的是浅拷贝,b与最后出现错误信息,原因是:
声明对象ba共用同一个buffer,程序结束前调用析构函数时对同一内存区进行了两次释放。
三、阅读下列程序及说明和注释信息,在方框中填写适当的程序段,使程序完成指定的功能
程序功能说明:
从键盘读入两个分别按由小到大次序排列的整数序列,每个序列10个整数,整数间以空白符分隔。
用这两个序列分别构造两个单链表,每个链表有10个结点,结点的数据分别按由小到大次序排列。
然后将两个链表合成为一个新的链表,新链表的结点数据仍然按由小到大次序排列。
最后按次序输出合并后新链表各结点的数据。
程序运行结果如下,带下划线部分表示输入内容,其余是输出内容:
135791113151719
2468101214161820
1234567891011121314151617181920
#include
5/9
#include
//类定义部分
template
classNode
{
private:
Node*next。
//指向后继节点的指针
public:
Tdata。
//数据域
Node(constT&item,Node*ptrnext=NULL)。
//构造函数
voidInsertAfter(Node*p)。
//在本节点之后插入一个同类节点p
Node*DeleteAfter(void)。
//删除本节点的后继节点,返回其地址
Node*NextNode(void)const。
//获取后继节点的地址
}。
template
classLinkedList
{
private:
Node*front,*rear。
//表头和表尾指针
Node*prevPtr,*currPtr。
//记录表当前遍历位置的指针,由插入和删除操作更新
intsize。
//表中的元素个数
intposition。
//当前元素在表中的位置序号。
由函数Reset使用
Node*GetNode(constT&item,Node*ptrNext=NULL)。
//生成新节点,数据域为item,指针域为ptrNext
voidFreeNode(Node*p)。
//释放节点
voidCopyList(constLinkedList&L)。
//将链表L拷贝到当前表//(假设当前表为空)。
被拷贝构造函数、operator=调用
public:
LinkedList(void)。
//构造函数
LinkedList(constLinkedList&L)。
//拷贝构造函数
~LinkedList(void)。
//析构函数
LinkedList&operator=(constLinkedList&L)。
//重载赋值运算符
intListSize(void)const。
//返回链表中元素个数(size)
-6-/9
intListEmpty(void)const。
//size为0时返回TRUE,否则返回FALSE
voidReset(intpos=0)。
//将指针currPtr移动到序号为pos的节点,//prevPtr相应移动,position记录当前节点的序号
voidNext(void)。
//使prevPtr和currPtr移动到下一个节点
intEndOfList(void)const。
//currPtr等于NULL时返回TRUE,否则返回FALSE
intCurrentPo