实验 4 报告
学号: 15331281 院系专业: 数据科学与计算机学院
完成日期: 2016 年 10 月 13 日
实验题目: 线性表
实验内容: 1.线性表在顺序存储结构上的插入、删除运算; 2.线性表在链接存储结构上的插入、删除运算;
实验要求: 1.编写用顺序存储的线性表进行建立、插入和删除的算法函数; 2.编写带表头的对单链表进行建立、插入和删除的算法函数; 3.编写主函数(使用字符选单形式),使得可以做建立、插入或删除选择; 4.请用‘C’、‘O’ 、‘M’、 ‘P’、 ‘U’、‘T’、 ‘E’、 ‘R’为数据调试、运行程序,并写出 相应的输出结果和溢出测试
实验步骤: 1.编写用顺序存储的线性表类 sList:私有成员:一个数组 data, 一个整型 size。公有成 员:插入函数,删除函数,打印函数,返回大小的函数 2. 编写带表头的对单链表类 cList:私有成员:头指针 head, 一个整型 size。公有成员: 插入函数,删除函数,打印函数,返回大小的函数 3.插入均为位置若大于 size,插在最后,其他合法插入则插入在 pos 前 4.编写 main 函数,主要为 test 函数,test 函数字符选单选择顺序表或者链接表,然后 根据选择调用函数 L,函数 L 需要一个参数区分两种表,并且提供字符选单选择插入操 作,在头部插入,在尾部插入,删除,在头部删除,在尾部删除,打印列表退回主菜单 (test 函数)等操作。
实验结果: 略
代码:
main:
#include "sequentialList.hpp"
#include "chainList.hpp"
#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;
void test();
void L(char s);
int main() {
test();
system("pause");
return 0;
}
void test() {
bool isQuit = false;
while (isQuit == false) {
cout << "-----------------------------welcome--------------------------------" << endl
<< "s -- creat a sequential linked list" << endl
<< "c -- creat a chain linked list" << endl
<< "q -- quit" << endl
<< "--------------------------------------------------------------------" << endl
<< "please input c/s to choose the type of list you want to creat : ";
char t;
cin >> t;
cout << endl;
switch (t) {
case 's':
L('s');
break;
case 'c':
L('c');
break;
case 'q':
cout << "-------------------------------bye----------------------------------"
<< endl;
break;
default:
cout << "wrong input" << endl;
break;
}
}
}
void L(char s) {
sList sl;
cList cl;
bool isQuit = false;
while (isQuit == false) {
cout << "------------------------------linked list-----------------------" << endl
<< "i -- insert an element into the list by pos" << endl
<< "if -- insert an element to the front of the list" << endl
<< "ib -- insert an element to the back of the list" << endl
<< "e -- erase an element from the list by pos" << endl
<< "ef -- erase an element from the front of the list" << endl
<< "eb -- erase an element from the front of the list" << endl
// dispaly the list type and the whole list
<< "d -- display the list" << endl
<< "q -- quit to main menu" << endl
<< "----------------------------------------------------------------" << endl
<< "please input the operation to the list : ";
cout << endl;
std::string o;
cin >> o;
int pos;
bool flag;
if (o == "i" || o == "if" || o == "ib") {
char e;
if (o == "i") {
cout << "please input the insert pos : ";
cin >> pos;
} else if (o == "if") {
if (s == 's') pos = 0;
else if (s == 'c') pos = 1;
} else {
if (s == 's') pos = sl.getSize();
else if (s == 'c') pos = cl.getSize()+1;
}
cout << endl << "please input the insert element : ";
cin >> e;
cout << endl;
if (s == 's') flag = sl.insert(pos, e);
else if (s == 'c') flag = cl.insert(pos, e);
if (flag) cout << "insert successfull." << endl;
else cout << "insert fail." << endl;
} else if (o == "e" || o == "ef" || o == "eb") {
if (o == "e") {
cout << "please input the erase pos : ";
cin >> pos;
}
else if (o == "ef") {
if (s == 's') pos = 0;
else if (s == 'c') pos = 1;
}
else {
if (s == 's') pos = sl.getSize();
else if (s == 'c') pos = cl.getSize();
}
cout << endl;
if (s == 's') flag = sl.erase(pos);
else if (s == 'c') flag = cl.erase(pos);
if (flag) cout << "erase successfull." << endl;
else cout << "erase fail." << endl;
} else if (o == "d") {
if (s == 's') {
cout << "the sequential linked list is :" << endl;
sl.display();
}
else if (s == 'c') {
cout << "the chain linked list is :" << endl;
cl.display();
}
cout << endl;
} else if (o == "q") {
isQuit = true;
}
}
}
sequentialList.hpp
#ifndef SEQUENTIAL_LIST_HPP
#define SEQUENTIAL_LIST_HPP
//线性表在顺序存储结构
#define maxSize 3
typedef char elementType;
class sList { // 顺序线性表(下标从0开始
public:
sList();
~sList();
bool insert(int pos, elementType data_);
bool erase(int pos);
void display();
int getSize();
private:
elementType data[maxSize];
int size;
};
#endif
sequentialList.cpp
#include "sequentialList.hpp"
#include <iostream>
using std::cout;
sList::sList() {
size = 0;
}
sList::~sList() {}
bool sList::insert(int pos, elementType e) {
/* 插入函数
输入插入位置和插入元素
如果顺序表已满,返回错误,否则
如果插入位置pos小于0,返回错误false,
如果插入位置pos大于size,则在最后一个位置插入并返回真true
否则,在插入位置pos前一位置插入元素e,并返回真true
*/
if (size == maxSize || pos < 0) return false; // 无法插入
if (pos < size) { // 在pos位之前插入
for (int i = size; i >= pos; i--)
data[i] = data[i - 1];
data[pos] = e;
} else { // pos>size, 在最后插入
data[size] = e;
}
size++;
return true;
}
bool sList::erase(int pos) {
/* 删除函数
输入删除位置
如果删除位置pos小于0或者大于等于size,返回错false
否则,删除pos上的元素,并返回真true;
*/
if (pos < 0 || pos >= size) return false;
for (int i = pos; i < size - 1; i++)
data[i] = data[i + 1];
size--;
}
void sList::display() {
/* 以a b c的格式输出线性表
如果是空表, 输出empty*/
if (size == 0) std::cout << "empty list.";
for (int i = 0; i < size; i++)
cout << data[i] << " ";
}
int sList::getSize(){
return size;
}
chainList.hpp
#ifndef CHAIN_LIST_HPP
#define CHAIN_LIST_HPP
#include <iostream>
//线性表在连接存储结构
typedef char elementType;
struct node{
elementType e;
node* next;
node (elementType e_ , node* next_) :e(e_), next(next_){}
};
class cList { // 有表头的链接线性表(下标从1开始
public:
cList();
~cList();
bool insert(int pos, elementType data_);
bool erase(int pos);
void display();
int getSize();
private:
node* head;
int size;
};
#endif
chainList.cpp
#include "chainList.hpp"
#include <iostream>
cList::cList() {
/* 构造有表头的链接链表 */
head = new node(' ', NULL);
size = 0;
}
cList::~cList() {
node* temp;
while (head != NULL) {
temp = head->next;
delete head;
head = temp;
}
}
bool cList::insert(int pos, elementType e) {
/* 插入函数
输入插入位置和插入元素
如果插入位置pos小于1,返回错误false,
如果插入位置pos大于size,则在最后一个位置插入并返回真true
否则,在插入位置pos前一位置插入元素e,并返回真true
*/
if (pos < 1) return false;
if (pos > size) { // pos大于size, 在最后插入
node *temp = head;
while (temp->next != NULL)
temp = temp->next;
temp->next = new node(e, NULL);
} else { // 一般情况
node *pre = head;
while (--pos) {
pre = pre->next;
}
node* next = pre->next;
pre->next = new node(e, next);
}
size++;
return true;
}
bool cList::erase(int pos) {
/* 删除函数
输入删除位置
如果删除位置pos小于1或者大于size,返回错false
否则,删除位置pos上的元素,并返回真true;
*/
if (pos < 1 || pos > size) return false;
node *pre = head;
while (--pos) {
pre = pre->next;
}
node *tmp = pre->next;
pre->next = pre->next->next;
delete tmp;
size--;
return true;
}
void cList::display() {
/* 以a b c的格式输出线性表
如果是空表, 输出empty*/
if (size == 0) std::cout << "empty list.";
node *tmp = head->next;
while (tmp != NULL) {
std::cout << tmp->e << " ";
tmp = tmp->next;
}
}
int cList::getSize(){
return size;
}