[英]C++ Rebuilding Singly Linked List Links
我正在嘗試使用singly linked list
建立priority queue
。 我的想法是,我總是將最小int x
存儲在Node* head
。 調用deleteMin()
方法時。 它應該返回最小值( head->x
),並且還將head更新為列表中的下一個最小值。
一旦發現新的lowestNode
,我似乎遇到的問題就是更新鏈接。
SLList.hpp
const int deleteMin() {
int returnVal = this->head->x;
// we need to find the next lowest in the list and update our head, then relink the list.
// first update our head to the next of the current head
this->head = this->head->next;
cout << "head now: " << this->head->x << endl;
// iterate through our nodes searching for a smaller value
Node* currentNode = this->head;
Node* nextNode = NULL;
Node* lowestNode = NULL;
Node* prevNode = NULL;
// Node* prevHead = NULL;
// Node* nextHead = NULL;
while(currentNode != NULL) {
if (currentNode->next->x < this->head->x) {
nextNode = currentNode->next->next;
cout << "nextNode: " << nextNode->x << endl;
lowestNode = currentNode->next;
cout << "lowestNode: " << lowestNode->x << endl;
prevNode = currentNode;
cout << "prevNode: " << prevNode->x << endl;
// prevHead = this->head;
// cout << "prevHead: " << prevHead->x << endl;
// nextHead = this->head->next;
// cout << "nextHead: " << nextHead->x << endl;
// update links
lowestNode->next = this->head->next;
currentNode = this->head;
currentNode->next = nextNode;
this->head = lowestNode;
} else {
currentNode = currentNode->next;
}
}
// decrement the size
this->_size--;
// return the minVal
return returnVal;
}
while(currentNode != NULL && currentNode->next!=NULL) //here you need check for currectNode->next also for NULL condition
{
if (currentNode->next->x < this->head->x) {
nextNode = currentNode->next->next;
cout << "nextNode: " << nextNode->x << endl;
lowestNode = currentNode->next;
cout << "lowestNode: " << lowestNode->x << endl;
//here storing previous node is of no use except for debugging
prevNode = currentNode;
cout << "prevNode: " << prevNode->x << endl;
// update links- here you need to first remove the lowest node from its postion
currentNode->next=nextNode;
//and then add lowest node a the front
lowestNode->next = this->head->next;
this->head = lowestNode;
} else {
currentNode = currentNode->next;
}
}
我意識到的一個問題是,取決於包含最低int x
的節點的位置。 如果head
直接鏈接到一個較低的節點,則重新鏈接需要與在列表中間找到一個較低的節點稍有不同。
#ifndef SLLIST_H
#define SLLIST_H
using namespace std;
class SLList
{
struct Node {
int x;
Node *next;
};
private:
int _size;
public:
Node* head; // used to store minimum value of a Node's x
Node* tail;
SLList() :
_size(0),
head(NULL),
tail(NULL) {
}
int size() const {
return this->_size;
}
const int add (const int x) {
// create new node
Node* newNode = new Node();
newNode->x = x;
if (this->_size == 0) {
this->head = newNode;
this->tail = newNode;
} else {
if (newNode->x < this->head->x) {
// update head to new lowest and relink nodes
newNode->next = this->head;
this->head = newNode;
} else {
this->tail->next = newNode;
this->tail = newNode;
}
}
// update list size
this->_size++;
return x;
}
const int deleteMin() {
if (this->_size == 0) return -1;
int returnVal = this->head->x;
cout << "removing min val: " << returnVal << endl;
// we need to find the next lowest in the list and update our head, then relink the list.
// first update our head to the next of the current head
this->head = this->head->next;
// iterate through our nodes searching for a smaller value
Node* currentNode = this->head;
Node* lowestNode = NULL;
for(int i = 0; i < this->_size - 1; i++) {
if (currentNode->next->x < this->head->x) {
lowestNode = currentNode->next;
if (currentNode == this->head) {
cout << "current->next is next to head" << endl;
// only need to update 2 nodes
this->head->next = currentNode->next->next;
lowestNode->next = this->head;
this->head = lowestNode;
currentNode = currentNode->next;
} else {
// update three nodes
cout << "current->next has neighbours" << endl;
// Example scenario
// 3 // nextTo5
// 5 // nextTo4
// 4 // nextTo2
// 2 // nextToNull
// == turns into ==
// 2 // nextTo5
// 5 // nextTo4
// 4 // nextTo3
// 3 // nextToNull
lowestNode->next = this->head->next;
currentNode->next = this->head;
this->head = lowestNode;
currentNode = currentNode->next;
}
} else {
currentNode = currentNode->next;
}
}
// decrement the size
this->_size--;
// return the minVal
return returnVal;
}
const void printList() const {
Node* tmp = this->head;
cout << "printing list... " << endl;
for(int i = 0; i < this->_size; i++) {
cout << tmp->x << endl;
tmp = tmp->next;
}
}
};
#endif // SLLIST_H
也許我實際上應該以某種方式使用tail
,但是目前,我並沒有真正使用它太多。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.