📄 linked_list.c
字号:
#include <thread.h>#include <string>#include "linked_list.h"
Node::Node(){ // Initializing very clean. :-) _prev = 0; _next = 0; _head = false;}Node::Node(class Node *Header_p){ Node *w; // Go to the end w = Header_p->getEnd(); // And add the just created node there w->AddAfter(this); }Node::~Node(){ /* We shall now unlink this node out of the list for its almost dead. :) */ // If there's a previous Node, link the previous with the next one if(_prev!=0) (this->getPrev())->setNext(this->getNext()); // If there's a next Node, link the next with the previous if(_next!=0) (this->getNext())->setPrev(this->getPrev());} class Node * Node::getPrev()
{
return this->_prev;
}
class Node * Node::getNext()
{
return this->_next;
}
bool Node::setPrev(class Node *PrevNode_p)
{
if(this->_prev = PrevNode_p)
return true;
else
return false;
}
bool Node::setNext(class Node *NextNode_p)
{
if(this->_next = NextNode_p)
return true;
else
return false;
}
bool Node::isHeader()
{
if(this->_head == true)
return true;
else
return false;
}
bool Node::setHeader()
{
// If this Node is alone if((this->_next == 0) && (this->_prev == 0)) { // ...it can be a Header Node this->_head = true; return true; } else { // Should we set an exception here? return false; }}
class Node * Node::getHeader()
{
// If we are at the Header, return it if(this->isHeader()) return this; // If we are at the first node, return the previous if((this->getPrev())->isHeader()) return this->getPrev();
Node *w;
w = this;
while(w->isHeader() != true)
{
w = w->getPrev();
}
return w;
}class Node * Node::getStart()
{
// If we are at the Header, return the _next Node if(this->isHeader()) return this->getNext();
// If there's no next node, it'll return 0. BEWARE! // If we are at the first node, return it if((this->getPrev())->isHeader()) return this;
Node *w;
w = this;
while(w->isHeader() != true)
{
w = w->getPrev();
}
w = w->getNext(); return w;
}
class Node * Node::getEnd()
{
if(this->getNext()==0)
return this;
Node *w;
w = this;
while(w->getNext() != 0) {
w = w->getNext();
}
return w;
}
int Node::CountNodes(){
int num_nodes = 1;
// If we are the header and there's nobody after it, return 0 if((this->isHeader() == true) && (this->getNext() == 0)) return 0;
Node *w;
w = this->getStart();
while(w->getNext() != 0) {
w = w->getNext();
num_nodes++;
}
return num_nodes;
}
class Node * Node::AddBefore(class Node *NewNode_p)
{
class Node * old_prev;
// If the current Node is the Header, add after it if(this->isHeader()) return this->AddAfter(NewNode_p); old_prev = this->getPrev(); // Keeping the former previous address
this->setPrev(NewNode_p); NewNode_p->setNext(this); old_prev->setNext(NewNode_p); NewNode_p->setPrev(old_prev); return NewNode_p;
}
class Node * Node::AddAfter(class Node *NewNode_p)
{
class Node * old_next;
// If there's no next, deal with it if(this->getNext() == 0) { this->setNext(NewNode_p); NewNode_p->setPrev(this); return NewNode_p; } old_next = this->getNext(); // Keeping the former next address
this->setNext(NewNode_p); NewNode_p->setPrev(this); old_next->setPrev(NewNode_p); NewNode_p->setNext(old_next); return NewNode_p;
}
class Node * Node::AddAtEnd(class Node *Header_p, class Node *NewNode_p){ class Node *w; if(Header_p == 0) Header_p = this; w = Header_p->getEnd(); w->AddAfter(NewNode_p); return NewNode_p;}class Node * Node::getPos(class Node *ListNode_p, int Position_p){ // To control the position we want to stop int pos = 1; // The working Node Node *w; //If 0 was informed as the position, throw exception or return the header? // How do we do that? Gimme a hand here, Orn. if(ListNode_p == 0) { w = this; } else { // We go to the list's start w = ListNode_p->getStart(); } while(pos < Position_p) { if (w->getNext() == 0) { // Throw exception that the list is smaller than the position askde for // How do we do it? Sorry. Gimme a hand again Orn. :-) // We should return something here too, I guess... } w = w->getNext();
pos++;
}
return w;
}bool Node::DestroyList(class Node *Header_p){ Node *w, *z; // IF we are not at the header, don't destroy a thing if(Header_p->isHeader() == false) return false; w = this; z = this->getNext(); // Somebody's got to do it... do { delete w; w = z; z = z->getNext(); } while (z != 0); //Delete the last one delete w; return true;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -