实验三 线性表操作

来源:百度知道 编辑:UC知道 时间:2024/05/12 03:47:52
一、 实验目的
1、 掌握线性表的基本操作:插入、删除、查找。
2、 掌握链表遍历器的使用方法。
二、实验内容
1、 创建线性表类。线性表的存储结构使用链表。
2、 提供操作:自表首插入元素、删除指定元素、搜索表中是否有指定元素、输出链表。
3、 接收键盘录入的一系列整数(例10,25,8,33,60)作为节点的元素值,创建链表。输出链表内容。
4、 输入一个整数(例33),在链表中进行搜索,输出其在链表中的位置。如果不存在输出0。
5、 使用链表遍历器实现链表的反序输出。
6、 创建两个有序链表,使用链表遍历器实现链表的合并。

#include<iostream>
using namespace std;

class ChainNode{
friend class Chain;
public:
int data ;
ChainNode *link;
};

class Chain{
friend class ChainIterator;
public:
Chain(){first=0;}
~Chain();
bool isEmpty() const {return first==0;}
int length() const;
bool find(int k,int x) const;
int search(int x) const;
Chain& Delete(int k,int x);
Chain& insert(int k,const int x);
void output(ostream& out) const;
private:
ChainNode *first ;
};

Chain::~Chain(){
ChainNode *next;
while(first){
next=first->link;
delete first;
first=next;
}
}

int Chain::length() const{
ChainNode *current=first;
int len=0;
while(current){
len++;
current=current->link;
}
return len;
}

bool