Skip to content

Instantly share code, notes, and snippets.

@fernandozamoraj
Created February 26, 2022 16:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fernandozamoraj/e608f53ebb96fa6ae4838cd085aa3445 to your computer and use it in GitHub Desktop.
Save fernandozamoraj/e608f53ebb96fa6ae4838cd085aa3445 to your computer and use it in GitHub Desktop.
ThreeWaysToPrintLinkedListInReverseCPP
//demolinkedlist.cpp
#include <iostream>
template <class N>
class Node{
public:
N data;
Node<N> *next;
};
template <class T>
class DemoLinkedList{
private:
Node<T>* _head;
Node<T>* _tail;
int _size;
public:
DemoLinkedList(){
_head = nullptr;
_tail = nullptr;
_size = 0;
}
void add(T value){
if(_head == nullptr){
_head = new Node<T>();
_head->data = value;
_head->next = nullptr;
_tail = _head;
std::cout <<"Added new head" << std::endl;
}
else{
_tail->next = new Node<T>();
_tail->next->data = value;
_tail->next->next = nullptr;
_tail = _tail->next;
std::cout << "Added new tail " << std::endl;
}
_size++;
}
Node<T> *getHeadNode(){
return _head;
}
Node<T> *getTailNode(){
return _tail;
}
int size(){
return _size;
}
};
//main.cpp file
#include <iostream>
#include <stack>
#include "demolinkedlist.h"
template <typename T>
void test(T expected, T actual, std::string testName){
if(expected == actual){
std::cout << testName << " passed" << std::endl;
}
else{
std::cout << testName << " FAILED!!" << std::endl;
std::cout << " expected " << expected << " but was " << actual << std::endl;
}
}
template<class T>
void printNormal(Node<T> *current){
while(current != nullptr){
std::cout << current->data << " -> ";
current = current->next;
}
std::cout << std::endl;
}
template<class T>
void printMethod1(Node<T> *current){
if(current == nullptr){
return;
}
printMethod1(current->next);
std::cout << current->data << " -> ";
}
template<class T>
void printMethod2(Node<T> *head){
Node<T> *current = head;
std::stack<T> currentStack;
while(current != nullptr){
currentStack.push(current->data);
current = current->next;
}
std::cout << std::endl;
std::cout << "Method 2" << std::endl;
while(!currentStack.empty()){
T val = currentStack.top();
currentStack.pop();
std::cout << val << " -> ";
}
std::cout << std::endl;
}
template<class T>
void printMethod3(Node<T> *head){
std::cout << std::endl;
std::cout << "Method 3" << std::endl;
int size = 0;
Node<T> *current = head;
while(current != nullptr){
current=current->next;
size++;
}
std::cout << "Size " << size << std::endl;
for(int i=size-1; i >=0; i--){
current = head;
int newIndex = 0;
while(current != nullptr){
if(newIndex == i){
std::cout << current->data << " -> ";
break;
}
current=current->next;
newIndex++;
}
}
std::cout << std::endl;
}
int main() {
test(1, 2, "should failed test");
test(1, 1, "should pass test");
DemoLinkedList<int> demoList;
int actualSize = demoList.size();
test(0, actualSize, "Size 0");
demoList.add(3);
test(1, demoList.size(), "size 1");
test(3, demoList.getHeadNode()->data, "Has 3");
demoList.add(4);
test(2, demoList.size(), "size 2");
test(4, demoList.getHeadNode()->next->data, "Has 4 at next");
test(4, demoList.getTailNode()->data, "Has 4 at tail");
demoList.add(5);
test(3, demoList.size(), "size 3");
test(5, demoList.getHeadNode()->next->next->data, "Has 5 at next");
test(5, demoList.getTailNode()->data, "Has 5 at tail");
printNormal(demoList.getHeadNode());
printMethod1(demoList.getHeadNode());
printMethod2(demoList.getHeadNode());
printMethod3(demoList.getHeadNode());
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment