实验报告4:群体类和群体数据

更新时间:2024-05-03 04:21:01 阅读量: 综合文库 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

学校代码: 10128 学 号: 201220905060

《面向对象程序设计》实验报告

题 目:群体类和群体数据 学生姓名:燕飞 学 院:理学院 系 别:数学系

专 业:信息与计算科学 班 级:信计12-2 任课教师:侯睿

二 〇 一 五 年 十 一 月

一、实验目的

1、了解节点类的声明和实现,学习其使用方法 2、了解链表类的声明和实现,学习其使用方法 3、了解栈类的声明和实现,学习其使用方法 4、了解队列类的声明和实现,学习其使用方法 5、掌握对数组元素排序的方法 6、掌握对数组元素查找的方法

二、实验内容

1、编写程序Node.h实现例9-5的节点类,并编写测试程序lab9_1.cpp,实现链表的基本操作。 2、编写程序link.h实现例9-6的链表类,在测试程序lab_2.cpp中声明两个整型链表A和B,分别插入5元素,然后把B中的元素加入A的尾部。 3、编写程序queue.h,用链表实现队列(或栈),在测试程序lab9_3.cpp中声明一个整型队列(或栈)对象,插入5个整数,压入队列(或栈),再依次取出并显示出来。 4、(选做)声明course(课程)类,有属性:课程名char name[21]、成绩short score;在实验七的student类中增加属性;所修课程course,为课程类对象的链表。在测试程序中测试这个类,学生类与课程类关系如图

5、将直接插入排序、直接选择排序、冒泡排序、顺序查找函数封装到第九章的数组类中,作为成员函数,实现并测试这个类。

三、实验程序

1、

#ifndef NODE_CLASS #define NODE_CLASS template class Node {

private:

Node *next; public: T data;

Node (const T& item, Node* ptrnext = NULL); void InsertAfter(Node *p); Node *DeleteAfter(void); Node *NextNode(void) const; };

template

2

Node::Node(const T& item, Node* ptrnext) : data(item), next(ptrnext) {}

template

Node *Node::NextNode(void) const {

return next; }

template

void Node::InsertAfter(Node *p) {

p->next = next; next = p; }

template

Node *Node::DeleteAfter(void) {

Node *tempPtr = next; if (next == NULL) return NULL;

next = tempPtr->next; return tempPtr; }

#endif

#ifndef NODE_LIBRARY #define NODE_LIBRARY #include #include #include %using namespace std; template

Node *GetNode(const T& item, Node *nextPtr = NULL) {

Node *newNode;

newNode = new Node(item, nextPtr); if (newNode == NULL) {

cerr << \ exit(1); }

return newNode; }

enum AppendNewline {noNewline,addNewline}; template

3

void PrintList(Node *head, AppendNewline addnl = noNewline) {

Node *currPtr = head; while(currPtr != NULL) {

if(addnl == addNewline) cout << currPtr->data << endl; else

cout << currPtr->data << \ currPtr = currPtr->NextNode(); } }

template

int Find(Node *head, T& item, Node* &prevPtr) {

Node *currPtr = head; prevPtr = NULL;

while(currPtr != NULL) {

if (currPtr->data == item) return 1;

prevPtr = currPtr;

currPtr = currPtr->NextNode(); }

return 0; }

template

void InsertFront(Node* & head, T item) {

head = GetNode(item,head); }

template

void InsertRear(Node* & head, const T& item) {

Node *newNode, *currPtr = head; if (currPtr == NULL) InsertFront(head,item); else {

while(currPtr->NextNode() != NULL) currPtr = currPtr->NextNode(); newNode = GetNode(item); currPtr->InsertAfter(newNode); }

4

} template

void DeleteFront(Node* & head) {

Node *p = head; if (head != NULL) {

head = head->NextNode(); delete p; } }

template

void Delete (Node* & head, T key) {

Node *currPtr = head, *prevPtr = NULL; if (currPtr == NULL) return;

while (currPtr != NULL && currPtr->data != key) {

prevPtr = currPtr;

currPtr = currPtr->NextNode(); }

if (currPtr != NULL) {

if(prevPtr == NULL)

head = head->NextNode(); else

prevPtr->DeleteAfter(); delete currPtr; } }

template

void InsertOrder(Node* & head, T item) {

Node *currPtr, *prevPtr, *newNode; prevPtr = NULL; currPtr = head;

while (currPtr != NULL) {

if (item < currPtr->data) break;

prevPtr = currPtr;

currPtr = currPtr->NextNode(); }

5

if (prevPtr == NULL) InsertFront(head,item); else {

newNode = GetNode(item);

prevPtr->InsertAfter(newNode); } }

template

void ClearList(Node * &head) {

Node *currPtr, *nextPtr; currPtr = head; while(currPtr != NULL) {

nextPtr = currPtr->NextNode(); delete currPtr; currPtr = nextPtr; }

head = NULL; }

#endif

#include #include \#include %using namespace std; int main() {

Node *head = NULL, *prevPtr, *delPtr; int i, key, item;

for (i=0;i < 10;i++) {

cin>>item;

InsertFront(head, item); }

cout << \

PrintList(head,noNewline); cout << endl;

cout << \请输入一个需要删除的整数: \ cin >> key;

prevPtr = head;

while (Find(head,key,prevPtr) != NULL) {

if(prevPtr == NULL)

6

head = head->NextNode(); else

delPtr=prevPtr->DeleteAfter(); delete delPtr; }

cout << \

PrintList(head,noNewline); cout << endl; ClearList(head); } 2、

#include \int main() {

LinkedList A, B; for(int i=0;i<5;i++) {

A.InsertRear(2*i+1); B.InsertRear(2*i+2); }

A.Reset();

cout << \链表A的元素为:\ while(!A.EndOfList()) {

cout << A.Data() << \ A.Next(); }

cout << endl; B.Reset();

cout << \链表B的元素为:\ while(!B.EndOfList()) {

cout << B.Data() << \ B.Next(); }

cout << endl;

cout << \把B中的元素插入A中...\ B.Reset();

while(!B.EndOfList()) {

A.InsertRear(B.Data()); B.Next(); }

A.Reset();

7

cout << \此时,链表A的元素为:\ while(!A.EndOfList()) {

cout << A.Data() << \ A.Next(); }

cout << endl; }

#ifndef LINKEDLIST_CLASS #define LINKEDLIST_CLASS #include #include using namespace std; #ifndef NULL

const int NULL = 0; #endif // NULL #include \template class LinkedList {

private:

Node *front, *rear;

Node *prevPtr, *currPtr; int size; int position;

Node *GetNode(const T& item,Node *ptrNext=NULL); void FreeNode(Node *p);

void CopyList(const LinkedList& L); public:

LinkedList(void);

LinkedList(const LinkedList& L); ~LinkedList(void);

LinkedList& operator= (const LinkedList& L); int ListSize(void) const; int ListEmpty(void) const; void Reset(int pos = 0); void Next(void);

int EndOfList(void) const;

int CurrentPosition(void) const; void InsertFront(const T& item); void InsertRear(const T& item); void InsertAt(const T& item); void InsertAfter(const T& item); T DeleteFront(void);

8

void DeleteAt(void); T& Data(void); void ClearList(void); };

template

Node *LinkedList::GetNode(const T& item, Node* ptrNext) {

Node *p;

p = new Node(item,ptrNext); if (p == NULL) {

cout << \ exit(1); }

return p; }

template

void LinkedList::FreeNode(Node *p) {

delete p; }

template

void LinkedList::CopyList(const LinkedList& L) {

Node *p = L.front; int pos;

while (p != NULL) {

InsertRear(p->data); p = p->NextNode(); }

if (position == -1) return;

prevPtr = NULL; currPtr = front;

for (pos = 0; pos != position; pos++) {

prevPtr = currPtr;

currPtr = currPtr->NextNode(); } }

template

LinkedList::LinkedList(void): front(NULL), rear(NULL),

9

prevPtr(NULL),currPtr(NULL), size(0), position(-1) {}

template

LinkedList::LinkedList(const LinkedList& L) {

front = rear = NULL;

prevPtr = currPtr = NULL; size = 0;

position = -1; CopyList(L); }

template

void LinkedList::ClearList(void) {

Node *currPosition, *nextPosition; currPosition = front;

while(currPosition != NULL) {

nextPosition = currPosition->NextNode(); FreeNode(currPosition);

currPosition = nextPosition; }

front = rear = NULL;

prevPtr = currPtr = NULL; size = 0;

position = -1; }

template

LinkedList::~LinkedList(void) {

ClearList(); }

template

LinkedList& LinkedList::operator=(const LinkedList& L) {

if (this == &L) return *this; ClearList(); CopyList(L); return *this; }

template

int LinkedList::ListSize(void) const {

10

return size; }

template

int LinkedList::ListEmpty(void) const {

return size == 0; }

template

void LinkedList::Next(void) {

if (currPtr != NULL) {

prevPtr = currPtr;

currPtr = currPtr->NextNode(); position++; } }

template

int LinkedList::EndOfList(void) const {

return currPtr == NULL; }

template

int LinkedList::CurrentPosition(void) const {

return position; }

template

void LinkedList::Reset(int pos) {

int startPos;

if (front == NULL) return;

if (pos < 0 || pos > size-1) {

cerr << \ return; }

if(pos == 0) {

prevPtr = NULL; currPtr = front; position = 0; }

11

else {

currPtr = front->NextNode(); prevPtr = front; startPos = 1;

for(position=startPos; position != pos; position++) {

prevPtr = currPtr;

currPtr = currPtr->NextNode(); } } }

template

T& LinkedList::Data(void) {

if (size == 0 || currPtr == NULL) {

cerr << \ exit(1); }

return currPtr->data; }

template

void LinkedList::InsertFront(const T& item) {

if (front != NULL) Reset(); InsertAt(item); }

template

void LinkedList::InsertRear(const T& item) {

Node *newNode; prevPtr = rear;

newNode = GetNode(item); if (rear == NULL)

front = rear = newNode; else {

rear->InsertAfter(newNode); rear = newNode; }

currPtr = rear; position = size;

12

size++; }

template

void LinkedList::InsertAt(const T& item) {

Node *newNode; if (prevPtr == NULL) {

newNode = GetNode(item,front); front = newNode; } else {

newNode = GetNode(item);

prevPtr->InsertAfter(newNode); }

if (prevPtr == rear) {

rear = newNode; position = size; }

currPtr = newNode; size++; }

template

void LinkedList::InsertAfter(const T& item) {

Node *p;

p = GetNode(item); if (front == NULL) {

front = currPtr = rear = p; position = 0; } else {

if (currPtr == NULL) currPtr = prevPtr;

currPtr->InsertAfter(p); if (currPtr == rear) {

rear = p;

position = size; }

13

else

position++;

prevPtr = currPtr; currPtr = p; }

size++; }

template

T LinkedList::DeleteFront(void) {

T item; Reset();

if (front == NULL) {

cerr << \ exit(1); }

item = currPtr->data; DeleteAt(); return item; }

template

void LinkedList::DeleteAt(void) {

Node *p;

if (currPtr == NULL) {

cerr << \ exit(1); }

if (prevPtr == NULL) {

p = front;

front = front->NextNode(); } else

p = prevPtr->DeleteAfter(); if (p == rear) {

rear = prevPtr; position--; }

currPtr = p->NextNode(); FreeNode(p);

14

size--; }

#endif 3、

#ifndef QUEUE_CLASS #define QUEUE_CLASS #include #include using namespace std; #include \template class Queue {

private:

LinkedList queueList; public:

Queue(void);

void QInsert(const T& elt); T QDelete(void); T QFront(void);

int QLength(void) const; int QEmpty(void) const; void QClear(void); };

template Queue::Queue(void) {}

template

int Queue::QLength(void) const {

return queueList.ListSize(); }

template

int Queue::QEmpty(void) const {

return queueList.ListEmpty(); }

template

void Queue::QClear(void) {

queueList.ClearList(); }

template

void Queue::QInsert(const T& elt)

15

{

queueList.InsertRear(elt); }

template

T Queue::QDelete(void) {

if (queueList.ListEmpty()) {

cerr << \ exit(1); }

return queueList.DeleteFront(); }

template

T Queue::QFront(void) {

if (queueList.ListEmpty()) {

cerr << \ exit(1); }

queueList.Reset();

return queueList.Data(); }

#endif

#ifndef LINKEDLIST_CLASS #define LINKEDLIST_CLASS #include #include using namespace std; #ifndef NULL

const int NULL = 0; #endif

#include \template class LinkedList {

private:

Node *front, *rear;

Node *prevPtr, *currPtr; int size; int position;

Node *GetNode(const T& item,Node *ptrNext=NULL); void FreeNode(Node *p);

16

void CopyList(const LinkedList& L); public:

LinkedList(void);

LinkedList(const LinkedList& L); ~LinkedList(void);

LinkedList& operator= (const LinkedList& L); int ListSize(void) const; int ListEmpty(void) const; void Reset(int pos = 0); void Next(void);

int EndOfList(void) const;

int CurrentPosition(void) const; void InsertFront(const T& item); void InsertRear(const T& item); void InsertAt(const T& item); void InsertAfter(const T& item); T DeleteFront(void); void DeleteAt(void); T& Data(void);

void ClearList(void); };

template

Node *LinkedList::GetNode(const T& item, Node* ptrNext) {

Node *p;

p = new Node(item,ptrNext); if (p == NULL) {

cout << \ exit(1); }

return p; }

template

void LinkedList::FreeNode(Node *p) {

delete p; }

template

void LinkedList::CopyList(const LinkedList& L) {

Node *p = L.front; int pos;

while (p != NULL)

17

{

InsertRear(p->data); p = p->NextNode(); }

if (position == -1) return;

prevPtr = NULL; currPtr = front;

for (pos = 0; pos != position; pos++) {

prevPtr = currPtr;

currPtr = currPtr->NextNode(); } }

template

LinkedList::LinkedList(void): front(NULL), rear(NULL), prevPtr(NULL),currPtr(NULL), size(0), position(-1) {}

template

LinkedList::LinkedList(const LinkedList& L) {

front = rear = NULL;

prevPtr = currPtr = NULL; size = 0;

position = -1; CopyList(L); }

template

void LinkedList::ClearList(void) {

Node *currPosition, *nextPosition; currPosition = front;

while(currPosition != NULL) {

nextPosition = currPosition->NextNode(); FreeNode(currPosition);

currPosition = nextPosition; }

front = rear = NULL;

prevPtr = currPtr = NULL; size = 0;

position = -1; }

template

18

LinkedList::~LinkedList(void) {

ClearList(); }

template

LinkedList& LinkedList::operator= (const LinkedList& L) {

if (this == &L) return *this; ClearList(); CopyList(L); return *this; }

template

int LinkedList::ListSize(void) const {

return size; }

template

int LinkedList::ListEmpty(void) const {

return size == 0; }

template

void LinkedList::Next(void) {

if (currPtr != NULL) {

prevPtr = currPtr;

currPtr = currPtr->NextNode(); position++; } }

template

int LinkedList::EndOfList(void) const {

return currPtr == NULL; }

template

int LinkedList::CurrentPosition(void) const {

return position; }

19

template

void LinkedList::Reset(int pos) {

int startPos;

if (front == NULL) return;

if (pos < 0 || pos > size-1) {

cerr << \ return; }

if(pos == 0) {

prevPtr = NULL; currPtr = front; position = 0; } else {

currPtr = front->NextNode(); prevPtr = front; startPos = 1;

for(position=startPos; position != pos; position++) {

prevPtr = currPtr;

currPtr = currPtr->NextNode(); } } }

template

T& LinkedList::Data(void) {

if (size == 0 || currPtr == NULL) {

cerr << \ exit(1); }

return currPtr->data; }

template

void LinkedList::InsertFront(const T& item) {

if (front != NULL) Reset();

20

InsertAt(item); }

template

void LinkedList::InsertRear(const T& item) {

Node *newNode; prevPtr = rear;

newNode = GetNode(item); if (rear == NULL)

front = rear = newNode; else {

rear->InsertAfter(newNode); rear = newNode; }

currPtr = rear; position = size; size++; }

template

void LinkedList::InsertAt(const T& item) {

Node *newNode;

if (prevPtr == NULL) {

newNode = GetNode(item,front); front = newNode; } else {

newNode = GetNode(item);

prevPtr->InsertAfter(newNode); }

if (prevPtr == rear) {

rear = newNode; position = size; }

currPtr = newNode; size++; }

template

void LinkedList::InsertAfter(const T& item) {

21

Node *p;

p = GetNode(item); if (front == NULL) {

front = currPtr = rear = p; position = 0; } else {

if (currPtr == NULL) currPtr = prevPtr;

currPtr->InsertAfter(p); if (currPtr == rear) {

rear = p;

position = size; } else

position++;

prevPtr = currPtr; currPtr = p; }

size++; }

template

T LinkedList::DeleteFront(void) {

T item; Reset();

if (front == NULL) {

cerr << \ exit(1); }

item = currPtr->data; DeleteAt(); return item; }

template

void LinkedList::DeleteAt(void) {

Node *p;

if (currPtr == NULL) {

22

cerr << \ exit(1); }

if (prevPtr == NULL) {

p = front;

front = front->NextNode(); } else

p = prevPtr->DeleteAfter(); if (p == rear) {

rear = prevPtr; position--; }

currPtr = p->NextNode(); FreeNode(p); size--; }

#endif // LINKEDLIST_CLASS #include \int main() {

Queue A;

for(int i=0;i<5;i++) {

A.QInsert(2*i+1); }

cout << \队列A的元素为:\ while(!A.QEmpty()) {

cout << A.QFront() << \ A.QDelete(); }

cout << endl; } 4、 5、

#include #include %using namespace std; int main() {

23

Array A(10); int i,k;

int SortType; int SearchNum;

cout << \请输入10个整数\ for(i=0;i<10;i++) {

cout << \输入第\个数字:\ cin >> A[i]; }

cout << \输入的数组为:\ for(i=0;i<10;i++)

cout << A[i] << \ cout << endl;

cout << \请选择排序方法:1.直接插入排序 2.直接选择排序 3.冒泡排序:\ cin >> SortType; switch(SortType) {

case 1:

A.InsertionSort(); break; case 2:

A.SelectionSort(); break; case 3:

A.BubbleSort(); break; default:

cout << \输入错误,程序退出!\ exit(0); }

cout << \排序后的数组为:\ for(i=0;i<10;i++)

cout << A[i] << \ cout << endl;

cout << \请输入待查找的数字:\ cin >> SearchNum;

k= A.SeqSearch(SearchNum); if (k<0)

cout << \没有找到数字\ else

cout << SearchNum << \是第\个数字\}

#ifndef ARRAY1_CLASS

24

#define ARRAY1_CLASS #include #include using namespace std; #ifndef NULL

const int NULL = 0; #endif

enum ErrorType

{invalidArraySize, memoryAllocationError, indexOutOfRange}; char *errorMsg[] = {

\ \};

template class Array {

private:

T* alist; int size;

void Error(ErrorType error,int badIndex=0) const; public:

Array(int sz = 50); Array(const Array& A); ~Array(void);

Array& operator= (const Array& rhs); T& operator[](int i); operator T* (void) const; int ListSize(void) const; void Resize(int sz); void InsertionSort(); void SelectionSort(); void BubbleSort(); int SeqSearch(T key); };

template

void Array::Error(ErrorType error, int badIndex) const {

cerr << errorMsg[error];

if (error == indexOutOfRange) cerr << badIndex; cerr << endl; exit(1); }

25

template

Array::Array(int sz) {

if (sz <= 0)

Error(invalidArraySize); size = sz;

alist = new T[size]; if (alist == NULL)

Error(memoryAllocationError); }

template Array::~Array(void) {

delete [] alist; }

template

Array::Array(const Array& X) {

int n = X.size; size = n;

alist = new T[n]; if (alist == NULL)

Error(memoryAllocationError); T* srcptr = X.alist; T* destptr = alist; while (n--)

*destptr++ = *srcptr++; }

template

Array& Array::operator= (const Array& rhs) {

int n = rhs.size; if (size != n) {

delete [] alist; alist = new T[n]; if (alist == NULL)

Error(memoryAllocationError); size = n; }

T* destptr = alist; T* srcptr = rhs.alist; while (n--)

*destptr++ = *srcptr++;

26

return *this; }

template

T& Array::operator[] (int n) {

if (n < 0 || n > size-1) Error(indexOutOfRange,n); return alist[n]; }

template

Array::operator T* (void) const {

return alist; }

template

int Array::ListSize(void) const {

return size; }

template

void Array::Resize(int sz) {

if (sz <= 0)

Error(invalidArraySize); if (sz == size) return;

T* newlist = new T[sz]; if (newlist == NULL)

Error(memoryAllocationError); int n = (sz <= size) ? sz : size; T* srcptr = alist; T* destptr = newlist; while (n--)

*destptr++ = *srcptr++; delete[] alist; alist = newlist; size = sz; }

template

void Array::InsertionSort() {

int i, j; T temp;

for (i = 1; i < size; i++)

27

{

j = i;

temp = alist[i];

while (j > 0 && temp < alist[j-1]) {

alist[j] = alist[j-1]; j--; }

alist[j] = temp; } }

template

void Array::SelectionSort() {

int smallIndex; int i, j;

for (i = 0; i < size-1; i++) {

smallIndex = i;

for (j = i+1; j < size; j++)

if (alist[j] < alist[smallIndex]) smallIndex = j;

Swap(alist[i], alist[smallIndex]); } }

template void Swap (T &x, T &y) {

T temp; temp = x; x = y; y = temp; }

template

void Array::BubbleSort() {

int i,j;

int lastExchangeIndex; i = size-1; while (i > 0) {

lastExchangeIndex = 0; for (j = 0; j < i; j++)

if (alist[j+1] < alist[j])

28

{

Swap(alist[j],alist[j+1]); lastExchangeIndex = j; }

i = lastExchangeIndex; } }

template

int Array::SeqSearch(T key) {

for(int i=0;i < size;i++) if (alist[i] == key) return i; return -1; }

#endif

四、实验结果

1、

2、

3、

4、 5、

29

30

本文来源:https://www.bwwdw.com/article/g5mg.html

Top