- #1
Jamin2112
- 986
- 12
the other interpreter I'm using?
I'm practicing my coding chops for an upcoming interview (I don't really have to use any hardcore programming chops I work as a web dev) and have been writing small C++ programs on online compilers/interpreters because I hate having to make entire new projects on my desktop's IDE. These online compilers/interpreters often give different results, making it hard to debug.
Example code piece:
On http://cpp.sh/ is printed
and on
http://codepad.org/ is printed
Can someone help me figure out what the problem is?
I'm practicing my coding chops for an upcoming interview (I don't really have to use any hardcore programming chops I work as a web dev) and have been writing small C++ programs on online compilers/interpreters because I hate having to make entire new projects on my desktop's IDE. These online compilers/interpreters often give different results, making it hard to debug.
Example code piece:
Code:
#include <iostream>
template <typename T> class SinglyLinkedList
{
struct node
{
T val;
node * next;
};
public:
SinglyLinkedList ();
SinglyLinkedList (T *, size_t);
~SinglyLinkedList ();
void push_back (T);
void print ();
void remove_val (T);
private:
node * _root;
};int main ()
{
int myArray [] = { 1, 69, -23942, 11111 };
SinglyLinkedList<int> myList(myArray, sizeof(myArray)/sizeof(int));
myList.print();
return 0;
}
template <typename T> SinglyLinkedList<T>::SinglyLinkedList ( )
{
_root = NULL;
}
template <typename T> SinglyLinkedList<T>::SinglyLinkedList (T * arr, size_t n)
{
/* Initialize a singly-linked list of objects of type T from an array of objects of type T */
if (n > 0)
{
node * lastNode = new node;
lastNode->val = *arr;
lastNode->next = NULL;
_root = lastNode;
for (T * pa(arr+1), * pb(arr+n); pa != pb; ++pa)
{
node * thisNode = new node;
thisNode->val = *pa;
thisNode->next = NULL;
lastNode->next = thisNode;
lastNode = thisNode;
}
delete lastNode;
}
else
{
_root = NULL;
}
}
template <typename T> SinglyLinkedList<T>::~SinglyLinkedList ( )
{
node * thisNode = _root;
while (thisNode != NULL)
{
node * temp = thisNode;
thisNode = thisNode->next;
delete temp;
}
}
template <typename T> void SinglyLinkedList<T>::push_back ( T val )
{
node * newNode = new node;
if (_root == NULL)
{
// ...
}
node * endNode;
}
template <typename T> void SinglyLinkedList<T>::print ( )
{
if (_root == NULL) return;
for (node * thisNode = _root; thisNode != NULL; thisNode = thisNode->next)
{
std::cout << thisNode->val << "->";
}
std::cout << "NULL";
}
template <typename T> void SinglyLinkedList<T>::remove_val ( T val )
{
// ...
}
On http://cpp.sh/ is printed
Code:
1->69->-23942->0->NULL
and on
http://codepad.org/ is printed
Code:
Segmentation fault
Can someone help me figure out what the problem is?