Hi so I'm trying to create a linked list. Have been trying to watch youtube clips, wikipedia etc but I'm just getting more and more confused. I do however grasp the idea behind the list that the first "box" will link to the next which will contain some sort of data and so on. Don't just know how to create some c++ code of it.
First of all i start whit:
1 2 3 4 5
struct Node
{
int data;
node *next; // this will be the thing that points to my next box, right?
};
Then I'm really confused what to type in the main.
Structs members are public by default, so you can just do
1 2
Node a;
a.data = 0;
You can also create a constructor to set 'data' when a Node is created
1 2 3 4 5 6 7 8 9
struct Node
{
Node(int n) {data = n;}
int data;
node *next;
};
Node a(0);
Node* b = new Node(0);
The segfault happens because when b, c and d are defined they point to some memory location in which you don't have permission to read/write, so you can't set 'next'. 'new' returns the location of a memory block in which you have those permissions.
Node* b declares that b will hold the address of a Node object.
new is a C++ operator that is used to allocate memory.
The line Node* b = new Node(0);
allocates memory for a Node object, calls its constructor with 0 as parameter, and returns the address of the constructed object into b.
Node* b declares a new refernece object called b, right?
Not exactly. It's a pointer. in C++ a "reference" is another type of data, but conceptually you're there: it's a variable that is not a Node, but refers to (points to) a Node existing somewhere in memory.
But what does "new node(0)" do? Is new a function or just some name that you did pick?
'new' is an operator of C++. It is used to obtain valid memory in which to store data (read previous post). The reason for this is that when you run a program it uses an area of memory, called 'stack', which isn't exactly large (IIRC it's usually 1MB, while computers now are around 4GB of total memory). Now, if you tried to create ten millions of ints on the stack (int n[10000000];) you wouldn't have enough memory for that, the program tries to write beyond the stack limit and that causes a crash (stack overflow). So you need more memory, and you obtain it using 'new'
Thanks alot for the help! Have been reading and thanks to evryone that have helped me i think i start to understand little of the linked list.
Working right now on a program that will add the highes value as the first value, second highest value as nummber 2 in the list etc. Although it does the opposite right now. Anyone that has any ideas? :) It does work if i remove my if-statements in the bool insert functions, so i guess its something wrong there? Thanks!
Suppose you call insert() for the first time with 'value' = 4. The list is created and 'data' become 4. Then you call insert(list, 3). Since 'data' < 'value' (4 < 3) is false, the value is not inserted.
Suppose you call insert(list, 5) the second time. (4 < 5) is true, so the second element in the list becomes 5, giving the reverse order you see.
Note that if any 'value' you input is smaller than any 'data' already in the list, it will not inserted at all.
Notes:
- I'd change line 55 to be temp->next = nullptr. You want it to be null, not to point to the list (which is null at that point, but it makes the code more readable)
- You don't deal with 'value' == 'data'. Is it supposed to ignore the input if it is already present in the list?
- Just changing the code to (list->data > value) won't solve the issue. You need to implement a mechanism to make the values in the list "slide" when a new value has to be inserted in the middle of it. And a way to assign 'data' to 'value' even when 'list' is not nullptr
- insert() is declared as returning a bool, but it does not return anything at all. If you don't care to verify if an item is successfully inserted change it to void