Creating new linked list

Hello, i am struggling with creating linked lists that are linked to the head list, I know how to create the head list but how can i link the others with it
help please
here is my code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
 void Push(ItemType x)
	{
		if (IsFull())
		{
			cout << "Stack is full";
			return;
		}
		if (topPtr == NULL)
		{
			topPtr = new NodeType<ItemType>;
			topPtr->info = x;
			topPtr->next = NULL;
			tailPtr = topPtr;
			top++;
			stackLength++;
		}
		else
		{
			temp = new NodeType<ItemType>;
			temp->info = x;
			tailPtr->next =temp;
			temp->next = NULL;
			tailPtr = temp;
			top++;
			stackLength++;
		}
	};
Last edited on
What is tailPtr when the else branch is reached? If it's NULL, you're dereferencing a null pointer on line 21 with tailPtr->next. Also, you have both tailPtr->next and tailPtr itself being set to temp. Is that what you want? Also, since you have a tailPtr, wouldn't its "next" always be NULL?

It's hard to tell, but maybe something like this is what you want? Not sure if this will help.
1
2
3
4
5
6
7
8
9
		else
		{
			temp = new NodeType<ItemType>;
			temp->info = x;
                        temp->next = NULL;
                        tailPtr = temp;
			top++;
			stackLength++;
		}

Basically the same as the if branch, but working with the temp isntead of head.
Last edited on
Topic archived. No new replies allowed.