Linked List Beginner

Day 1 learning about linked lists. I am following an example in the textbook, and the following code compiles fine. When I go to run, however it forces the .exe file to close. Is it a code issue or am I missing a file? Using Dev c++ 5.4 right now.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include<iostream>
#include<string>
#include<iomanip>
#include<stdio.h>
#include<conio.h>
#include<list>
using namespace std;

int main()
{
struct nodeType
{
	int info;
	nodeType *link;
};
nodeType *head, *p, *q, *newNode;
newNode = new nodeType;
newNode->info = 50;
newNode->link = p->link;
p->link = newNode;
} 
Your program produces no output and receives no input. So it should just run to completion rather quickly and show you nothing as a result. Unless, by saying
forces the .exe file to close
you mean that there is an error of some sort that comes up letting you know the program had an abnormal termination.
Yes, the exact error reads "File_Name".exe has stopped working:
A problem caused the program to stop working correctly. Windows will close the program and notify you if a solution is available.

However, since it compiles with no errors or warning I thought maybe I was missing a dev c++ file. The error only occurs when I run.
Last edited on
newNode->link = p->link;

Tell me, where does p points to? And what will we get if we will try to get some value from it?
Actually your program accesses unknown memory on line 19.
p does not have a memory address assigned to it, but you are trying to access UNKOWN->link so it crashes.
This is even simpler. I expected to be able to start storing numbers in head->info after the pointer *head was defined. However the code below gives the same error. It's likely I just don't understand how the linked lists work yet. Again, this compiles but doesn't run. Is there something else I need to initialize?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include<iostream>
#include<string>
#include<iomanip>
#include<stdio.h>
#include<conio.h>
#include<list>
using namespace std;

int main()
{
struct nodeType
{
	int info;
	nodeType *link;
};
nodeType *head;
head->info = 50;
cout << head->info;
}
Last edited on
It's likely I just don't understand how the linked lists work yet.
You don't understand how pointers work yet. I suggest starting with learning pointers purpose and usage:

http://www.cplusplus.com/doc/tutorial/pointers/
http://www.learncpp.com/cpp-tutorial/67-introduction-to-pointers/
I will do that. Thanks.
Topic archived. No new replies allowed.