实验4群体类和群体大数据吕恩在.doc

上传人:夺命阿水 文档编号:16327 上传时间:2022-06-30 格式:DOC 页数:14 大小:296.50KB
返回 下载 相关 举报
实验4群体类和群体大数据吕恩在.doc_第1页
第1页 / 共14页
实验4群体类和群体大数据吕恩在.doc_第2页
第2页 / 共14页
实验4群体类和群体大数据吕恩在.doc_第3页
第3页 / 共14页
实验4群体类和群体大数据吕恩在.doc_第4页
第4页 / 共14页
实验4群体类和群体大数据吕恩在.doc_第5页
第5页 / 共14页
点击查看更多>>
资源描述

《实验4群体类和群体大数据吕恩在.doc》由会员分享,可在线阅读,更多相关《实验4群体类和群体大数据吕恩在.doc(14页珍藏版)》请在课桌文档上搜索。

1、群体类和群体数据一、 实验目的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中

2、声明一个整型队列或栈对象,插入5个整数,压入队列或栈,再依次取出并显示出来。4.选做声明course课程类,有属性:课程名char name21、成绩short score;在实验七的student类中增加属性;所修课程course,为课程类对象的链表。在测试程序中测试这个类,学生类与课程类关系如图5. 将直接插入排序、直接选择排序、冒泡排序、顺序查找函数封装到第九章的数组类中,作为成员函数,实现并测试这个类三、实验内容:#ifndef NODE_CLASS#define NODE_CLASStemplate class Node private: Node *next; /指向后继节点的指针

3、 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* ptrnext) : data(item), next(ptrnext) template Node *Node:NextNode(void) const return next;template void Node:Insert

4、After(Node *p) p-next = next; /p节点指针域指向当前节点的后继节点 next = p; /当前节点的指针域指向ptemplate Node *Node:DeleteAfter(void)Node *tempPtr = next; /将欲删除的节点地址存储到tempPtr中 if (next = NULL) /如果当前节点没有后继节点,如此返回NULL return NULL; next = tempPtr-next; /使当前节点的指针域指向tempPtr的后继节点 return tempPtr; /返回被删除的节点的地址#endif / NODE_CLASS#i

5、fndef NODE_LIBRARY#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;enu

6、m AppendNewline noNewline,addNewline;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; /从第一个结

7、点开始遍历 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, *

8、currPtr = head; if (currPtr = NULL) InsertFront(head,item); else 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-NextNod

9、e(); / 将表头指针head移向第二个结点 delete p; /删除原第一个结点 template void Delete (Node* & head, T key) Node *currPtr = head, *prevPtr = NULL; if (currPtr = NULL) return; while (currPtr != NULL & currPtr-data != key) / currPtr前行,prevPtr跟随其后 prevPtr = currPtr; currPtr = currPtr-NextNode(); if (currPtr != NULL) if(pre

10、vPtr = 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 data) break; / currPtr前行,prevPtr跟随其后 prev

11、Ptr = currPtr; currPtr = currPtr-NextNode(); 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 cu

12、rrPtr; currPtr = nextPtr; /使指针currPtr指向下一个结点 head = NULL; /将头结点置为NULL,标志着链表为空#endif / NODE_LIBRARY#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 10;i+) coutplease input i+1 number To insert a header :item; Inser

13、tFront(head, 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; ClearLi

14、st(head);运行结果:please input 1 number To insert a header :1please input 2 number To insert a header :2please input 3 number To insert a header :3please input 4 number To insert a header :4please input 5 number To insert a header :5please input 6 number To insert a header :6please input 7 number To ins

15、ert a header :7please input 8 number To insert a header : 8please input 9 number To insert a header :9please input 10 number To insert a header :10List: 10 9 8 7 6 5 4 3 2 1请输入一个需要删除的整数: 2List: 10 9 8 7 6 5 4 3 1#ifndef LINKEDLIST_CLASS#define LINKEDLIST_CLASS#include #include using namespace std;#i

16、fndef NULLconst int NULL = 0;#endif / NULL#include 9_5.htemplate 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); Linke

17、dList(const LinkedList& L); /拷贝构造函数 LinkedList(void); LinkedList& operator= (const LinkedList& L); int ListSize(void) const; /返回链表中元素个数size int ListEmpty(void) const; /size等于0时返回TRUE,否如此返回FALSE void Reset(int pos = 0); void Next(void); /使prevPtr和currPtr移动到下一个节点 int EndOfList(void) const; int Current

18、Position(void) const; /返回数据成员position 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);templ

19、ate 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;template void LinkedList:CopyList(const LinkedList& L) Node *p = L.front; int pos; whi

20、le (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), prevPtr(NULL),currPtr(NULL), size(0),

21、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 = currPositio

22、n-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 *th

23、is; 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 LinkedL

24、ist: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: pos NextNode(); prevPtr = front; startPos = 1; for(po

25、sition=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) if (front != NULL) Reset(); InsertAt(item); / 在表头插入tem

26、plate 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& ite

27、m) 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(it

28、em); 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;

29、 Reset(); if (front = NULL) cerr Invalid deletion! data; DeleteAt(); return item;template void LinkedList:DeleteAt(void) Node *p; if (currPtr = NULL) cerr Invalid deletion! NextNode(); else p = prevPtr-DeleteAfter(); if (p = rear) rear = prevPtr; position-; currPtr = p-NextNode(); FreeNode(p); size-

30、;#endif / LINKEDLIST_CLASS#include link.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;

31、cout 把B中的元素插入A中. endl; B.Ret(); while(!B.EndOfList() A.InsertRear(B.Data(); B.Next(); A.Reset(); cout 此时,链表A的元素为: ; while(!A.EndOfList() cout A.Data() ; A.Next(); cout endl;#ifndef QUEUE_CLASS#define QUEUE_CLASS#include #include using namespace std;#include link.htemplate class Queue private: Linked

32、List 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 retu

33、rn queueList.ListEmpty();template void Queue:QClear(void) queueList.ClearList();template void Queue:QInsert(const T& elt) queueList.InsertRear(elt);template T Queue:QDelete(void) if (queueList.ListEmpty() cerr Calling QDelete for an empty queue! endl; exit(1); return queueList.DeleteFront();template T Queue:QFront(void) if (queueList.ListEmpty() cerr Calling QFront for an empty queue! en

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 在线阅读 > 生活休闲


备案号:宁ICP备20000045号-1

经营许可证:宁B2-20210002

宁公网安备 64010402000986号