Initial commit of code for dopelib, and ch 1, 2, and 12
This commit is contained in:
32
libs/chapter2/reverse.cpp
Normal file
32
libs/chapter2/reverse.cpp
Normal file
@@ -0,0 +1,32 @@
|
||||
#include "chapter2.hpp"
|
||||
#include <stack>
|
||||
|
||||
/*
|
||||
Reverse Linked List
|
||||
*/
|
||||
|
||||
void reverse_linkedlist(DopeLinkedList* ll){
|
||||
DopeNode* node = ll->GetHead();
|
||||
DopeNode* prev = nullptr;
|
||||
std::stack<DopeNode*> s;
|
||||
int iter_cnt = 0;
|
||||
s.push(node);
|
||||
while(node->GetNext() != NULL){
|
||||
s.push(node);
|
||||
node = node->GetNext();
|
||||
iter_cnt++;
|
||||
}
|
||||
s.push(node);
|
||||
|
||||
prev = s.top();
|
||||
s.pop();
|
||||
ll->SetHead(prev);
|
||||
while(!s.empty()){
|
||||
node = s.top();
|
||||
s.pop();
|
||||
prev->SetNext(node);
|
||||
prev = node;
|
||||
}
|
||||
node->SetNext(nullptr);
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user