I've been working at this, ALL day... and stumped
I have a class called LinkedList, which I am writing, Everything compiles, but at run time I get a segmentation default error.
It occurs when I initialize a LinkedList object.
I.e. LinkedList ll; // causes a seg def error at runtime

Also, As i can't even get the object to initialize, please dont be too critical over the accuracy of the algorithms used. Thanks alot!

here is LinkedList.h

#ifndef LinkedList_h
#define LinkedList_h

#include "common.h"
#include "Node.h"
class Node; // forward reference

class LinkedList {
public:
    LinkedList();
    void insert(int,int);
    void append(int);
    void prepend(int);
    Node* getNode(int);
    int getValue(int);
    int remove(int);
    int getLast();
    int getFirst();
    void enqueue(int);
    int dequeue();
    int pop();
    void push(int);
    int getSize();
    void clear();
    void destroy();

private:
    Node root;
   // Node* root_ptr;
    int size;
};
#endif

LinkedList.cpp

#include "LinkedList.h"
using namespace std;


LinkedList::LinkedList() {
    root = Node();    cout << "TEST" << endl;
    size = 0;
}


void LinkedList::insert(int i, int val) {
    if(i >= 0 && i < size) {
        Node* walkthru  = root.getNext();
        for(int c = 0; c < i; c++) {
            walkthru = walkthru->getNext();
        }
        Node tmp = Node(val); // make a new node
        tmp.setPrev(walkthru);
        if(i < size - 1) { // if its not last
            tmp.setNext(walkthru->getNext());
            walkthru->getNext()->setPrev(&tmp);
        }
        walkthru->setNext(&tmp);
        size++;
    } else {
        cout << " could not insert to list" << endl;
    }
}

void LinkedList::append(int val) {
    cout << "TEST" << endl;
    Node* walkthru  = root.getNext();
    for(int c = 0; c < size; c++) {
        walkthru = walkthru->getNext();
    }
    Node tmp = Node(val); // make a new node
    tmp.setPrev(walkthru);
    walkthru->setNext(&tmp);
    size++;
}

void LinkedList::prepend(int val) {
    insert(0,val);
}

Node* LinkedList::getNode(int i) {
    if(i >= 0 && i < size) {
        Node* walkthru  = root.getNext();
        for(int c = 0; c < i; c++) {
            walkthru = walkthru->getNext();
        }
        return walkthru;
    }
    return NULL;
}

int LinkedList::getValue(int i) {
    if(i >= 0 && i < size) {
        Node* walkthru  = root.getNext();
        for(int c = 0; c < i; c++) {
            walkthru = walkthru->getNext();
        }
        return walkthru->get();
    }
    return NULL;
}

int LinkedList::remove(int i) {
    if(i >= 0 && i < size) {
        Node* walkthru  = root.getNext();
        for(int c = 0; c < i; c++) {
            walkthru = walkthru->getNext();
        }
        Node* tmp = walkthru;
        walkthru->getPrev()->setNext(walkthru->getNext());
        int val = tmp->get();
        delete tmp;
        size--;
        return val;
    }
    return NULL;
}

int LinkedList::getFirst() { // head
    return getValue(0);
}

int LinkedList::getLast() { // tail
    return getValue(size - 1);
}

void LinkedList::enqueue(int val) { // enqueue to tail
     insert(size,val);
     remove(size);
}

int LinkedList::dequeue() { // dequeue from head
    return remove(0);
}

int LinkedList::pop() { // pop from tail
    return remove(size - 1);
}

void LinkedList::push(int val) {
     insert(size,val); // push to tail
}

int LinkedList::getSize() {
    return size;
}
void LinkedList::clear() {
    Node* walkthru  = root.getNext();
    for(int i = 0; i < size; i++) {
        walkthru = walkthru->getNext();
        if(walkthru->getPrev() != &root) {
            delete walkthru->getPrev();
        }
    }
    size = 0;
    root = NULL;
}

void LinkedList::destroy() {
    // should do some sort of clean up. :)
    root = NULL;
}

Node.h

#ifndef Node_h
#define Node_h

#include "common.h"

class Node {
public:
    Node();
    Node(int);
    Node(Node*);
    void set(int);
    int get();
    void setNext(Node*);
    void setPrev(Node*);
    Node* getNext();
    Node* getPrev();

private:
    Node* prev;
    Node* next;
    int data;

};
#endif

Node.cpp

#include "Node.h"
using namespace std;

Node::Node() {
    *prev = NULL;
    *next = NULL;
    data = 0;
}

Node::Node(int val) {
    *prev = NULL;
    *next = NULL;
    data = val;
}

Node::Node(Node* n) {
    *prev = n->getPrev();
    *next = n->getNext();
    data = n->get();
}

void Node::set(int val) {
    data = val;
}

int Node::get() {
    return data;
}

void Node::setNext(Node* n) {
    next = n;
}

void Node::setPrev(Node* p) {
    prev = p;
}

Node* Node::getNext() {
    return next;
}

Node* Node::getPrev() {
    return prev;
}

Main.cpp

#include "Main.h"

using namespace std;

int main ( int argc, char* argv[] ) {
    cout << "Init" << endl;

    LinkedList ll;

    cout << "Init end" << endl;

    ll.append(1);
    ll.append(2);
    ll.append(3);
    cout << "pop " << ll.getLast() << endl;
    cout << "Size " << ll.getSize() << endl;
    return 0;
}

Main.h

#ifndef Main_h
#define Main_h

#include "common.h"
#include "LinkedList.h"

#endif

common.h

#ifndef common_h
#define common_h


#ifdef __cplusplus
    #include <cstdlib>
    #include <iostream>
    #include <fstream>
#else
    #include <stdlib.h>
    #include <iostream>
    #include <fstream>
#endif

using namespace std;
#endif

wow.. so stupid :P
Thanks alot! Can't believe I didn't see that.
now i can continue debugging. I got another error, but I know whats wrong.
peace

This article has been dead for over six months. Start a new discussion instead.