之前用c語(yǔ)言的方式實(shí)現(xiàn)過(guò)單鏈表,現(xiàn)在用c++的方式實(shí)現(xiàn)單鏈表。
為桓臺(tái)等地區(qū)用戶提供了全套網(wǎng)頁(yè)設(shè)計(jì)制作服務(wù),及桓臺(tái)網(wǎng)站建設(shè)行業(yè)解決方案。主營(yíng)業(yè)務(wù)為成都網(wǎng)站建設(shè)、網(wǎng)站建設(shè)、桓臺(tái)網(wǎng)站設(shè)計(jì),以傳統(tǒng)方式定制建設(shè)網(wǎng)站,并提供域名空間備案等一條龍服務(wù),秉承以專業(yè)、用心的態(tài)度為用戶提供真誠(chéng)的服務(wù)。我們深信只要達(dá)到每一位用戶的要求,就會(huì)得到認(rèn)可,從而選擇與我們長(zhǎng)期合作。這樣,我們也可以走得更遠(yuǎn)!
以c++的類實(shí)現(xiàn)單鏈表,寫(xiě)完代碼有了許多不一樣的體會(huì)。感受到了兩種語(yǔ)言的差異。
#includeusing namespace std; class Slist { private: struct Node { int data; Node* pNext; }; int size; Node* pHead; Node* pTail; Node* ByeNode(int _data) { Node* pNode = NULL; pNode = new Node; pNode->data = _data; pNode->pNext = NULL; return pNode; } public: Slist() { pHead = NULL; pTail = NULL; size = 0; } Slist(const Slist& my_Node) { Node* pNode = (my_Node).pHead; for (int i = 0; i < my_Node.size; i++) { Pushback(pNode->data); pNode = pNode->pNext; } } ~Slist(); bool Empty(); Node* operator[](int index) { if ((index<0) || (index>size - 1)) return NULL; Node* pNode = pHead; for (int i = 0; i < index; i++) pNode = pNode->pNext; return pNode; } Slist& operator=(const Slist& my_Node) { Clear(); Node* pNode = my_Node.pHead; for (int i = 0; i < my_Node.size; i++) { Pushback(pNode->data); pNode = pNode->pNext; } return *this; } void Pushback(int _data); void Popback(); void PushFront(int _data); void PopFront(); Node* Find(int _data) { if (Empty()) return NULL; Node* pNode = pHead; while (pNode->pNext!=NULL) { if (pNode->data == _data) return pNode; pNode = pNode->pNext; } return NULL; } void Erase(Node* DelNode) { Node* pNode = pHead; while (pNode->pNext != DelNode) pNode = pNode->pNext; pNode->pNext = pNode->pNext->pNext; delete DelNode; DelNode = NULL; } void Clear() { if (Empty()) return; while (size != 0) { PopFront(); } } void sort(); }; Slist::~Slist() { while (!Empty()) { size--; Node* pNode = pHead; pHead = pHead->pNext; delete pNode; pNode = NULL; } pTail = NULL; } void Slist::Pushback(int _data) { size += 1; Node* NewNode = ByeNode(_data); if (NULL == pHead) { pHead = NewNode; pTail = NewNode; return; } pTail->pNext = NewNode; pTail = pTail->pNext; } void Slist::Popback() { if (Empty()) { cout << "鏈表為空" << endl; return; } if (size == 1) { size -= 1; delete pHead; pHead = NULL; pTail = NULL; return; } size -= 1; delete pTail; Node* pNode = pHead; while (pNode->pNext != NULL) pNode = pNode->pNext; pTail = pNode; } void Slist::PushFront(int _data) { Node* NewNode = ByeNode(_data); if (pHead == NULL) { pHead = NewNode; pTail = NewNode; size += 1; return; } Node* pNode = pHead; pHead = NewNode; pHead->pNext = pNode; size += 1; } void Slist::PopFront() { if (Empty()) return; if (1 == size) { delete pHead; pHead = NULL; pTail = NULL; return; } size -= 1; Node* pNode = pHead; pHead = pHead->pNext; delete pNode; pNode = NULL; } bool Slist::Empty() { if (size == 0) return true; return false; } void Slist::sort() { if (size <= 1) return; Node* pNode = pHead; while (pNode->pNext != NULL) { if ((pNode->data) > (pNode->pNext->data)) { int tmp = pNode->data; pNode->data = pNode->pNext->data; pNode->pNext->data = tmp; pNode = pNode->pNext; } else { pNode = pNode->pNext; } } }