1、实验报告群体类和群体数据学校代码: 10128学 号: 201220905060面向对象程序设计实验报告(题 目:群体类和群体数据学生姓名:燕飞学 院:理学院系 别:数学系专 业:信息与计算科学班 级:信计12-2任课教师:侯睿二 一 五 年 十 一 月1、实验目的1、了解节点类的声明和实现,学习其使用方法2、了解链表类的声明和实现,学习其使用方法3、了解栈类的声明和实现,学习其使用方法4、了解队列类的声明和实现,学习其使用方法5、掌握对数组元素排序的方法6、掌握对数组元素查找的方法2、实验内容1、编写程序Node.h实现例9-5的节点类,并编写测试程序lab9_1.cpp,实现链表的基本操作
2、。2、编写程序link.h实现例9-6的链表类,在测试程序lab_2.cpp中声明两个整型链表A和B,分别插入5元素,然后把B中的元素加入A的尾部。3、编写程序queue.h,用链表实现队列(或栈),在测试程序lab9_3.cpp中声明一个整型队列(或栈)对象,插入5个整数,压入队列(或栈),再依次取出并显示出来。4、(选做)声明course(课程)类,有属性:课程名char name21、成绩short score;在实验七的student类中增加属性;所修课程course,为课程类对象的链表。在测试程序中测试这个类,学生类与课程类关系如图5、将直接插入排序、直接选择排序、冒泡排序、顺序查找
3、函数封装到第九章的数组类中,作为成员函数,实现并测试这个类。3、实验程序1、#ifndef NODE_CLASS#define NODE_CLASStemplate 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 Node:Node(const T& item, Node* ptrnex
4、t) : 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#
5、define NODE_LIBRARY#include #include #include 9_5.husing namespace std;template Node *GetNode(const T& item, Node *nextPtr = NULL) Node *newNode; newNode = new Node(item, nextPtr); if (newNode = NULL) cerr Memory allocation failure! endl; exit(1); return newNode;enum AppendNewline noNewline,addNewli
6、ne;template void PrintList(Node *head, AppendNewline addnl = noNewline) Node *currPtr = head; while(currPtr != NULL) if(addnl = addNewline) cout data endl; else cout data NextNode(); template int Find(Node *head, T& item, Node* &prevPtr) Node *currPtr = head; prevPtr = NULL; while(currPtr != NULL) i
7、f (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
8、 while(currPtr-NextNode() != NULL) currPtr = currPtr-NextNode(); newNode = GetNode(item); currPtr-InsertAfter(newNode); 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, *prevPt
9、r = 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 *currP
10、tr, *prevPtr, *newNode; prevPtr = NULL; currPtr = head; while (currPtr != NULL) if (item data) break; prevPtr = currPtr; currPtr = currPtr-NextNode(); if (prevPtr = NULL) InsertFront(head,item); else newNode = GetNode(item); prevPtr-InsertAfter(newNode); template void ClearList(Node * &head) Node *c
11、urrPtr, *nextPtr; currPtr = head; while(currPtr != NULL) nextPtr = currPtr-NextNode(); delete currPtr; currPtr = nextPtr; head = NULL;#endif #include #include 9_5.h#include node.husing namespace std;int main() Node *head = NULL, *prevPtr, *delPtr; int i, key, item; for (i=0;i item; InsertFront(head,
12、 item); cout List: ; PrintList(head,noNewline); cout endl; cout key; prevPtr = head; while (Find(head,key,prevPtr) != NULL) if(prevPtr = NULL) head = head-NextNode(); else delPtr=prevPtr-DeleteAfter(); delete delPtr; cout List: ; PrintList(head,noNewline); cout endl; ClearList(head);2、#include link.
13、hint main() LinkedList A, B; for(int i=0;i5;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中. endl; B.Reset(); while(
14、!B.EndOfList() A.InsertRear(B.Data(); B.Next(); A.Reset(); cout 此时,链表A的元素为: ; while(!A.EndOfList() cout A.Data() ; A.Next(); cout endl;#ifndef LINKEDLIST_CLASS#define LINKEDLIST_CLASS#include #include using namespace std;#ifndef NULLconst int NULL = 0;#endif / NULL#include 9-3.htemplate class Linked
15、List 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 Li
16、nkedList& 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 D
17、eleteFront(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 Memory allocation failure!n; exit(1); return p;template void LinkedList:FreeNode(Node *p) delete p;templa
18、te 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:L
19、inkedList(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, *nextPositi
20、on; 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:op
21、erator=(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
22、= 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 size-1) cerr Reset: Invalid list position:
23、pos 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 Data: invalid reference! data;template void LinkedList:InsertFront(const T& item) i
24、f (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; size+;template void LinkedLis
25、t: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 =
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1