Need help with unsorted linked list

delete
Last edited on
closed account (10oTURfi)
At first glance... look at constructor:
head->next = NULL;
This can't happen, because you never new'd the head.
delete
Last edited on
closed account (10oTURfi)
Woah.

1
2
3
4
5
6
7
void List::Insertitem(ItemType x)
{
	NodeType *newNode = new NodeType;
	newNode->item = x;
	newNode->next = NULL;
	newNode = head;
}


What is this supposed to do?

Constructor should only set head to NULL
head = NULL;

And the insert function should look something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
void Insertitem(int data)
    {
        if(!head)//If head wasnt new'd yet
        {
            head = new NodeType;
            head->item = data; // set the head
            head->next = 0;
        }
        else 
        {
            NodeType* iter = head;
            while(iter->next) //else find end of list
            {
                iter = iter->next;
            }
            NodeType* _new = new NodeType; //and add element
            _new->item = item;
            iter->next = _new;
            _new->next = 0;
        }
    }


(I hope I didnt make mistake)
Last edited on
delete
Last edited on
delete
Last edited on
Please do not blank your posts - if others have the same problem as you, they can no longer find the information they need.
Topic archived. No new replies allowed.