Initial commit of code for dopelib, and ch 1, 2, and 12

This commit is contained in:
2024-10-29 16:04:54 -04:00
parent 11601352d3
commit afbf4c2f7e
25 changed files with 745 additions and 0 deletions

32
libs/chapter2/reverse.cpp Normal file
View 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);
}