




版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
1、學校代碼: 10128學 號: 201220905060 面向對象程序設計實驗報告(題 目:群體類和群體數據學生姓名:燕飛學 院:理學院系 別:數學系專 業:信息與計算科學班 級:信計12-2任課教師:侯睿 二 一 五 年 十 一 月1、 實驗目的1、了解節點類的聲明和實現,學習其使用方法2、了解鏈表類的聲明和實現,學習其使用方法3、了解棧類的聲明和實現,學習其使用方法4、了解隊列類的聲明和實現,學習其使用方法5、掌握對數組元素排序的方法6、掌握對數組元素查找的方法2、 實驗內容1、編寫程序Node.h實現例9-5的節點類,并編寫測試程序lab9_1.cpp,實現鏈表的基本操作。2、編寫程序l
2、ink.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* ptrnext) : da
4、ta(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
5、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,addNewline;temp
6、late 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) if (currPtr
7、-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(curr
8、Ptr-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, *prevPtr = NULL; i
9、f (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, *prevPt
10、r, *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 *currPtr, *ne
11、xtPtr; 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, item); cou
12、t 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.hint main()
13、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(!B.EndOfList()A.InsertRear(B.
14、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 LinkedList private: Node *front, *rear; Nod
15、e *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) cons
16、t; 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)
17、; 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;template void LinkedList:CopyList(const Lin
18、kedList& 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(NU
19、LL), 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(currP
20、osition != 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
21、 = &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+; temp
22、late 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: pos NextNode(); prevPtr = front; star
23、tPos = 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) if (front != NULL) Reset(); InsertAt(i
24、tem);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 *newNo
25、de; 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
26、 = 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
27、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-;#endif3、#ifndef QUEUE_CLASS#defi
28、ne QUEUE_CLASS#include #include using namespace std;#include link.htemplate 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)templa
29、te 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) queueList.InsertRear(elt);template T Queue:QDelete(void) if (queueList.List
30、Empty() 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! endl; exit(1); queueList.Reset(); return queueList.Data();#endif#ifndef LINKEDLIST_CLASS#define LINKEDLIST_CL
31、ASS#include #include using namespace std;#ifndef NULLconst int NULL = 0;#endif#include 9-3.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&
32、 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(c
33、onst 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 = NUL
34、L) 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; while (p != NULL) InsertRear(p-data); p = p-NextNode(); if (position = -1) return; prevPtr = NULL; currPtr = f
35、ront; 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 = NU
36、LL; 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 =
37、 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 return size;template int LinkedList:ListEmp
38、ty(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 Li
39、nkedList: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(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) if (front != NULL) Reset(); InsertAt(item);template void LinkedList:In
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經權益所有人同意不得將文件中的內容挪作商業或盈利用途。
- 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 糧食代理合同范本
- 個人自建包工合同范本
- 學校證訂書合同范本
- 個人藏品交易合同范本
- 臨時設施 勞務合同范本
- 房屋工程終止合同范本
- 海邊出售地皮合同范本
- 個人定車合同范本
- 2025工程合同范本簡化、實際案例解析
- 2025商業辦公樓租賃合同模板
- 青島版四年級數學下冊全冊教學設計含教學計劃及進度表
- GB/T 44333-2024綠色產品評價耐火材料
- 2024年廣東省廣州市中考英語試卷附答案
- 前程無憂國企招聘筆試題庫
- 產業園區開發全流程實操解析
- 2024版滴灌購銷合同滴灌合同
- TD/T 1057-2020 國土調查數據庫標準(正式版)
- 【含答案】高處安裝、維護、拆除理論考試200題
- 太極拳文化與養生智慧樹知到期末考試答案章節答案2024年寧波財經學院
- 鉆孔灌注樁施工工藝流程圖
- 2024年開封大學單招職業適應性測試題庫及答案解析
評論
0/150
提交評論