o I am practicing doing data structures. Figured I would start with a linked list and building them in a few different ways.
Right now when I create a double pointer and try to set the values my program crashes at run time. Can somebody help me figure this out. I don't have a lot of experience with double pointers but I was told (*dataValue) is how you use it.
#include<iostream>
using namespace std;
class node {
int value;
node* ptr;
public:
void setValue(int theValue) { value = theValue; }
void setPointer(node* thePtr) { ptr = thePtr; }
int getValue() { return value; }
node* getPtr() { return ptr; }
};
typedef node* nodePtr;
class linkedList {
public:
nodePtr link;
void insertLinkHead(nodePtr* next, int theValue);
void outPut(nodePtr head);
};
int main() {
linkedList LL;
nodePtr* head = new node*;
//THIS PART CRASHES BELOW
(*head)->setPointer(NULL);
(*head)->setValue(0);
//THIS PART CRASHES ABOVE
//setting up linked list
for (int i = 1; i <= 5; i++) {
LL.insertLinkHead(head, i);
}
LL.outPut(*head);
cout << "Testing" << endl;
return 0;
}//end of main function
void linkedList::insertLinkHead(nodePtr* head, int theValue) {
nodePtr newLink = new node;
newLink->setValue(theValue);
newLink->setPointer(*head);
*head = newLink;
}
void linkedList:
utPut(nodePtr head) {
nodePtr temp;
temp = head;
while (temp != NULL) {
cout << "value is " << temp->getValue() << endl;
temp = temp->getPtr();
}
}
Right now when I create a double pointer and try to set the values my program crashes at run time. Can somebody help me figure this out. I don't have a lot of experience with double pointers but I was told (*dataValue) is how you use it.
#include<iostream>
using namespace std;
class node {
int value;
node* ptr;
public:
void setValue(int theValue) { value = theValue; }
void setPointer(node* thePtr) { ptr = thePtr; }
int getValue() { return value; }
node* getPtr() { return ptr; }
};
typedef node* nodePtr;
class linkedList {
public:
nodePtr link;
void insertLinkHead(nodePtr* next, int theValue);
void outPut(nodePtr head);
};
int main() {
linkedList LL;
nodePtr* head = new node*;
//THIS PART CRASHES BELOW
(*head)->setPointer(NULL);
(*head)->setValue(0);
//THIS PART CRASHES ABOVE
//setting up linked list
for (int i = 1; i <= 5; i++) {
LL.insertLinkHead(head, i);
}
LL.outPut(*head);
cout << "Testing" << endl;
return 0;
}//end of main function
void linkedList::insertLinkHead(nodePtr* head, int theValue) {
nodePtr newLink = new node;
newLink->setValue(theValue);
newLink->setPointer(*head);
*head = newLink;
}
void linkedList:
nodePtr temp;
temp = head;
while (temp != NULL) {
cout << "value is " << temp->getValue() << endl;
temp = temp->getPtr();
}
}