C++ LinkedList Using Double pointers

JC0133

Senior member
Nov 2, 2010
201
1
76
So 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::eek:utPut(nodePtr head) {
nodePtr temp;

temp = head;

while (temp != NULL) {
cout << "value is " << temp->getValue() << endl;
temp = temp->getPtr();
}
}
 

Ken g6

Programming Moderator, Elite Member
Moderator
Dec 11, 1999
16,584
4,495
75
I see the problem. Let me rewrite this for you, without changing it:

Code:
node** head = new node*;

//THIS PART CRASHES BELOW
(**head).setPointer(NULL);

You created a pointer in the heap, and assigned a pointer to it. You then try to access the memory pointed to by the inner pointer. But you never pointed the inner pointer to any location.
 

sao123

Lifer
May 27, 2002
12,653
205
106
Not saying that what you have is wrong, but is very odd... you have some of your functionality in your main that seems it should be in your LL class.
Your Main should never see the details of the head/tail, etc.

Code:
//This is Pseudo code

class LinkedList
{

public:
LinkedList() //Default Constructor
void InsertAtHead(int Data)
int RemoveHead()
private:
Node* Head
Node* Tail
}

LinkedList()
{
Head = null
Tail = null
}

void insert(int data)
{
  node* nodetobeinserted
  if (head = null)
  {
  nodetobeinserted = new node(data, null)
  head = &nodetobeinserted
  tail = head
  }
  else
  {
  nodetobeinserted = new node(data, head)
  head = &nodetobeinserted()
  }
}

int RemoveHead()
{
int Data = Head->Data
Node* TempPtr = Head
Head = Head->Link
Delete TempPtr
Return Data
}

}
[
 

Cogman

Lifer
Sep 19, 2000
10,284
138
106
Agreed with sao.

A good datastructure doesn't expose its implementation to the outside world. Ideally, a user of your linked list class would never touch a pointer or know anything about the "Node" class.

This is important down the line when you decide you want to do things differently. Perhaps you want to preallocate nodes. Maybe you want to remove or add the double linking. Whatever you want. But you can't do any of that if the user of your datastructure is exposed to or is expected to work with the internal mechanisms.