数据结构实验报告
实验名称:线性表及其应用 班 级:12级电气本2 学 号:2012081227 姓 名:赵雪磊 指导教师:梁海丽 日 期:2013年9月9日
数学与信息技术学院
一、 实验目的
1、掌握线性表的概念,理解线性表的顺序、链式存储。
2、掌握线性表的基本操作,插入、删除、查找,以及线性表合并等运算在顺序存储结构和链接存储结构上的运算。
二、 实验要求
1、建立顺序存储的线性表,并对之进行插入、删除操作。 2、建立链式存储的线性表,并对之进行插入、删除操作。;
三、 算法描述
#include #include
#include "myList.h"
using namespace std;
template class Link { public: T }
// 【代码2.7】 单链表的类型定义 template
class lnkList : public List { protected:
Link* head, tail; lnkList();
// 单链表的头、尾指针
public:
// 构造函数 // 析构函数 // 判断链表是否为空
~lnkList(); Link }
Link(Link* nextValue = NULL) { }
next = nextValue;
// 具有一个参数的Link 构造函数
data;
// 用于保存结点元素的内容
* next;
// 指向后继结点的指针
Link(const T info, Link* nextValue = NULL) { // 具有两个参数的Link 构造函数
data = info; next = nextValue;
bool isEmpty(); void clear();
// 将链表存储的内容清除,成为空表
// 在表尾添加一个元素value ,表的长度增1
// 在位置p 上插入一个元素value ,表的长度增1 // 删除位置p 上的元素,表的长度减 1
// 查找值为value 的元素,并返回第1次出现的位置
int length(); // 返回此顺序表的当前实际长度 bool insert(int p, T value); bool delete(int p);
int getPos(const T value);
bool append(T value);
}
template
class lnkList:: lnkList() { }
template
class lnkList:: ~lnkList() { }
template
int count = 0; if (i == -1)
return head;
// 若i 为0则定位到第一个结点
// i为-1则定位到" 虚" 头结点
// 假定线性表的元素类型为T
Link lnkList :: setPos(int i) { Link *p;
Link tmp; tmp = head; }
head = head->next; delete tmp;
while (head != NULL) {
head = tail = new Link; p = head->next;
p = p-> next;
while (p != NULL && count
count++; };
return p; }
template Link *p, *q; q = new Link; p = setPos(i-1); if (p == NULL )
{
// p是第i 个结点的前驱
// 假定线性表的元素类型为T
bool lnkList :: insert (int i, T value) {
// 指向第 i 结点,i =0,1, …,当链表中结点数小于i 时返回NULL
cout
}
q->next = p->next; q->data = value; p->next = q;
// 插入点在链尾,插入结点成为新的链尾
if (q->next == NULL ) tail = q;
}
// delete a node from singly linked list template Link *p, *q; p = setPos(i-1); if (p == NULL )
{
// p是第i 个结点的前驱
// 假定线性表的元素类型为T
bool lnkList :: delete(int i) {
cout
// q是真正待删结点
// 待删结点为尾结点,则修改尾指针
}
q = p->next;
if (q == tail)
tail = p;
// 删除结点q 并修改链指针
if (q != NULL) {
delete q;
p->next = q->next;
}
return true; }
template
// 假定顺序表的元素类型为T
void lnkList :: print() { while (head != NULL) { }
cout data;
// 从位置p 开始每个元素左移直到curLen, tmp = head;
}
head = head->next;
cout
四、 程序清单
#include
#define TRUE 1 #define FALSE 0 #define OK 1 #define ERROR 0 #define INFEASIBLE -1 #define OVERFLOW -2 typedef int Status; typedef int ElemType;
#define LIST_INIT_SIZE 10 #define LISTINCREMENT 2 typedef struct shunxubiao{ ElemType *list; int size;
int Maxsize; }SqList;
int InitList_Sq(SqList &L) { // 构造一个空的线性表L 。
L.list = new ElemType[LIST_INIT_SIZE];
if (!L.list) return OVERFLOW; // 存储分配失败 L.size = 0; // 长度
为0
L.Maxsize = LIST_INIT_SIZE; // 初始存储容量 return OK; } // InitList_Sq int InsertList_Sq(SqList &L, int i, ElemType e) { ElemType *p,*q;
if (i L.Maxsize+1) return ERROR; q = &(L.list[i-1]); // q指示插入位置 for (p=&(L.list[L.Maxsize-1]); p >= q; --p) *(p+1) = *p;
// 插入位置及之后的元素右移
*q = e; // 插入e ++L.size; // 表长增1 return OK; } // ListInsert_Sq
int LocateElem_Sq(SqList L, ElemType e) {
// 在顺序表中查询数据元素e ,若存在,则返回它的位序,否则返回 0 int i = 1; // i 的初值为第 1 元素的位序 ElemType *p = L.list; // p 的初值为第 1 元素的存储位置 while (i
if (i
Status InsertList_Sq(SqList &L,ElemType e,ElemType f,ElemType g) { int i=LocateElem_Sq(L,e); int j=LocateElem_Sq(L,f); if(i==j-1) { InsertList_Sq(L,j,g); return OK; }
else return ERROR; }
int GetList_Sq(SqList L,int i) { if(i>0 && i
Status ListDelete_Sq(SqList &L, int i, ElemType &e) { ElemType *p,*q;
if ((i L.Maxsize)) return ERROR; p = &(L.list[i-1]); // p为被删除元素的位置 e = *p; // 被删除元素的值赋给e q = L.list+L.size-1; // 表尾元素的位置 for (++p; p
*(p-1) = *p; // 被删除元素之后的元素左移 --L.size; // 表长减1 return OK; } // ListDelete_Sq
void Create_Sq(SqList &L) { cout>count;
for(int i=0;i>L.list[i]; ++L.size; } }
void Print_Sq(SqList &L) { cout
void main() { SqList myList;
ElemType e,f,g,sc; InitList_Sq(myList); Create_Sq(myList); cout>g;
cout>e>>f; if(!InsertList_Sq(myList,e,f,g)) cout
cout>wx;
if(!ListDelete_Sq(myList,wx,sc)) cout
五、 实验结果与分析
六、 实验心得
此次上机实验,不仅对此次编译程序的算法思想有了新的认识,还让我深刻的体会到了线性表的重要性以及其应用的方便,并且对指针加深了映象,应用了书本中的算法思想,对我以后的编译以及完成新的程序有很大的帮助。
数据结构实验报告
实验名称:线性表及其应用 班 级:12级电气本2 学 号:2012081227 姓 名:赵雪磊 指导教师:梁海丽 日 期:2013年9月9日
数学与信息技术学院
一、 实验目的
1、掌握线性表的概念,理解线性表的顺序、链式存储。
2、掌握线性表的基本操作,插入、删除、查找,以及线性表合并等运算在顺序存储结构和链接存储结构上的运算。
二、 实验要求
1、建立顺序存储的线性表,并对之进行插入、删除操作。 2、建立链式存储的线性表,并对之进行插入、删除操作。;
三、 算法描述
#include #include
#include "myList.h"
using namespace std;
template class Link { public: T }
// 【代码2.7】 单链表的类型定义 template
class lnkList : public List { protected:
Link* head, tail; lnkList();
// 单链表的头、尾指针
public:
// 构造函数 // 析构函数 // 判断链表是否为空
~lnkList(); Link }
Link(Link* nextValue = NULL) { }
next = nextValue;
// 具有一个参数的Link 构造函数
data;
// 用于保存结点元素的内容
* next;
// 指向后继结点的指针
Link(const T info, Link* nextValue = NULL) { // 具有两个参数的Link 构造函数
data = info; next = nextValue;
bool isEmpty(); void clear();
// 将链表存储的内容清除,成为空表
// 在表尾添加一个元素value ,表的长度增1
// 在位置p 上插入一个元素value ,表的长度增1 // 删除位置p 上的元素,表的长度减 1
// 查找值为value 的元素,并返回第1次出现的位置
int length(); // 返回此顺序表的当前实际长度 bool insert(int p, T value); bool delete(int p);
int getPos(const T value);
bool append(T value);
}
template
class lnkList:: lnkList() { }
template
class lnkList:: ~lnkList() { }
template
int count = 0; if (i == -1)
return head;
// 若i 为0则定位到第一个结点
// i为-1则定位到" 虚" 头结点
// 假定线性表的元素类型为T
Link lnkList :: setPos(int i) { Link *p;
Link tmp; tmp = head; }
head = head->next; delete tmp;
while (head != NULL) {
head = tail = new Link; p = head->next;
p = p-> next;
while (p != NULL && count
count++; };
return p; }
template Link *p, *q; q = new Link; p = setPos(i-1); if (p == NULL )
{
// p是第i 个结点的前驱
// 假定线性表的元素类型为T
bool lnkList :: insert (int i, T value) {
// 指向第 i 结点,i =0,1, …,当链表中结点数小于i 时返回NULL
cout
}
q->next = p->next; q->data = value; p->next = q;
// 插入点在链尾,插入结点成为新的链尾
if (q->next == NULL ) tail = q;
}
// delete a node from singly linked list template Link *p, *q; p = setPos(i-1); if (p == NULL )
{
// p是第i 个结点的前驱
// 假定线性表的元素类型为T
bool lnkList :: delete(int i) {
cout
// q是真正待删结点
// 待删结点为尾结点,则修改尾指针
}
q = p->next;
if (q == tail)
tail = p;
// 删除结点q 并修改链指针
if (q != NULL) {
delete q;
p->next = q->next;
}
return true; }
template
// 假定顺序表的元素类型为T
void lnkList :: print() { while (head != NULL) { }
cout data;
// 从位置p 开始每个元素左移直到curLen, tmp = head;
}
head = head->next;
cout
四、 程序清单
#include
#define TRUE 1 #define FALSE 0 #define OK 1 #define ERROR 0 #define INFEASIBLE -1 #define OVERFLOW -2 typedef int Status; typedef int ElemType;
#define LIST_INIT_SIZE 10 #define LISTINCREMENT 2 typedef struct shunxubiao{ ElemType *list; int size;
int Maxsize; }SqList;
int InitList_Sq(SqList &L) { // 构造一个空的线性表L 。
L.list = new ElemType[LIST_INIT_SIZE];
if (!L.list) return OVERFLOW; // 存储分配失败 L.size = 0; // 长度
为0
L.Maxsize = LIST_INIT_SIZE; // 初始存储容量 return OK; } // InitList_Sq int InsertList_Sq(SqList &L, int i, ElemType e) { ElemType *p,*q;
if (i L.Maxsize+1) return ERROR; q = &(L.list[i-1]); // q指示插入位置 for (p=&(L.list[L.Maxsize-1]); p >= q; --p) *(p+1) = *p;
// 插入位置及之后的元素右移
*q = e; // 插入e ++L.size; // 表长增1 return OK; } // ListInsert_Sq
int LocateElem_Sq(SqList L, ElemType e) {
// 在顺序表中查询数据元素e ,若存在,则返回它的位序,否则返回 0 int i = 1; // i 的初值为第 1 元素的位序 ElemType *p = L.list; // p 的初值为第 1 元素的存储位置 while (i
if (i
Status InsertList_Sq(SqList &L,ElemType e,ElemType f,ElemType g) { int i=LocateElem_Sq(L,e); int j=LocateElem_Sq(L,f); if(i==j-1) { InsertList_Sq(L,j,g); return OK; }
else return ERROR; }
int GetList_Sq(SqList L,int i) { if(i>0 && i
Status ListDelete_Sq(SqList &L, int i, ElemType &e) { ElemType *p,*q;
if ((i L.Maxsize)) return ERROR; p = &(L.list[i-1]); // p为被删除元素的位置 e = *p; // 被删除元素的值赋给e q = L.list+L.size-1; // 表尾元素的位置 for (++p; p
*(p-1) = *p; // 被删除元素之后的元素左移 --L.size; // 表长减1 return OK; } // ListDelete_Sq
void Create_Sq(SqList &L) { cout>count;
for(int i=0;i>L.list[i]; ++L.size; } }
void Print_Sq(SqList &L) { cout
void main() { SqList myList;
ElemType e,f,g,sc; InitList_Sq(myList); Create_Sq(myList); cout>g;
cout>e>>f; if(!InsertList_Sq(myList,e,f,g)) cout
cout>wx;
if(!ListDelete_Sq(myList,wx,sc)) cout
五、 实验结果与分析
六、 实验心得
此次上机实验,不仅对此次编译程序的算法思想有了新的认识,还让我深刻的体会到了线性表的重要性以及其应用的方便,并且对指针加深了映象,应用了书本中的算法思想,对我以后的编译以及完成新的程序有很大的帮助。