I am trying to convert an array to an linked list.
so basically, im gonna have a structure called "head" which will be the first element
and node, which will be the other elements.
any ideas so i can it started?
I don't see any solution simpler than just iterating through the array and appending the elements to the list.
The standard way to implement linked lists in C is with a single node structure containing a data member and a next pointer. Every time you want a new node, malloc space for it and set the next pointer of the last node in the list to point to it. The last node's next pointer should point to NULL.
You only have to hold onto a regular pointer to the first element. That's your head pointer.
Without using malloc you won't be able to easily add a new node to store data in so its best to just use the array to avoid the mess (but why can't you use malloc now?)
Related
I am reading Mastering Algorithms with C by Kyle Loudon, and currently I am struggling with Loudon's implementation of Singly-Linked List in Chapter 5.
Here are the links to the source code. I apologize for not posting them here as they are a bit long.
list.h
list.c
My question is related to the destroy in list.c as it is mentioned in line 11 under
void list_init(List* list, void (*destroy)(void* data))
as list->destroy = destroy
and then again in line 24 as
list->destroy(data).
All I know is that this destroy is different from the function list_destroy but I have no idea what it is. Is it a function or is it just a pointer? What purpose does it serve in the list_init() function for initializing a linked list?
I really appreciate your time and help! The source code is linked above.
It is a function pointer. When you create an instance of this list, you also have to hand the init_list function a function which it will use to destroy the info.
The purpose of a linked list is to store information, and the linked list structure is there to give some structure to
this data. Hence, each element of the list contains a pointer to some data, and a pointer to the next element in the list. However, you want the list to able to handle multiple kinds of data.
Suppose you want to remove an element of the list, then there are basically two things that have to happen:
The data needs to be destroyed
The linked list structure must be restored. Meaning that the predecessor of the element you removed must point to
the next element in the list.
Since you do not know beforehand what kind of data the data pointer in the list will contain, for step 1 a function pointer is provided to handle the destroying of that data.
A linked list is a data structure made up of nodes, each of which contains or links to one piece of data.
The destroy function destroys just one node out of the list.
The list_destroy function destroys an entire list.
In the given implementation, the node actually contains a pointer to its destroy function, and accesses it by dereferencing that pointer. As it happens (so far), all nodes point to the same destroy function. But with more complex data structures this pattern allows multiple types of nodes to be in the data structure. And the equivalent of the list_destroy function will correctly destroy each node type correctly since the node knows how it should be destroyed.
If we have one pointer pointing at the last element of link list and we don't have any other pointer pointing to any other node of the link list then is it possible to delete that last node safely?
As per my opinion its not possible.
This is not possible if you do not have reference to the second last element of the list. You need to make the pointer part of the second last element null. So as per my knowledge, if you only have a reference to the last element, then deleting it is not possible.
An exceptional case will be when there is only one element in the list which is the first as well as the last element. In such a case, freeing that element will not cause any harm.
When you have a linked-list, you need to have a reference to the first element using which you traverse the entire list. But when you say you do not have any reference to the list except the last element, this is a very unusual case.
I tried to initialize a doubly linked list wich contains dummy nodes inside another doubly linked list (also with dummy nodes). For example, a node in the list of students has many friends stored in a linked list inside that node. Here's my code:
As I tried to compile it, it gave me this:
warning: assignment from incompatible pointer type.
it appeared on the lines that i put a comment on. Please help. ^^
Edit: Thanks The Platypus!
You are assigning a pointer of type "friendt" to "friendh".
newNode -> friendh -> next // is a pointer of type friendh. You are assigning it a pointer of type friendt.
You're doing the opposite in the next line. Assigning friendh to friendt.
Assuming both your structs do the same thing, keep one struct or change the type of pointer in your struct to reflect your code. I.e. Change frndh* next to frndt* next and the same with the other struct
I'm trying to make a link list and I'm having trouble with the concept with linking the middle part, I'm just doing a little pseudo-code right now, haven't actually coded anything.
(struct pointers) *current, *ahead, *behind, *begin;
(behind)-->(current)-->(ahead) //This is what I want to do
behind->next = current;
current->next = ahead;
Is this the proper way to break and connect the list? Without losing anything..
What you have looks correct but rather incomplete. One of the unwritten rules of programming is that you cannot write a linked list implementation correctly the first time. There are four cases you need to deal with:
Insert into an empty list
Insert into a non-empty list
Removing the first element from the list
Removing any other element from the list
There are also doubly-linked lists, where each element has a pointer to both the previous element and the next element. That makes it easier to handle things like removal of a random element without traversing the list, but can be trickier to get right.
I have tried a lot to learn linked list.But all my efforts were wasted.Please can some one help me understand linked list by providing his/her own code?Thanks in advance.
A linked list is simply a list of elements (usually called nodes) where each node has a reference (or pointers, in C) to the next node:
http://img837.imageshack.us/img837/5613/ll1s.png
You keep track of the list by having a pointer to the first node (the "head"), and by having the last node point to null
Linked lists where each element points to both the next and previous nodes are called doubly-linked lists.
By following these references, you can traverse the list and get any node.
A common advantage of linked lists over arrays is that you can insert and remove the elements in O(1) (constant) time. The disadvantage is that you have O(N) random-access.
See Wikipedia for more.
Have you play some of these rally games? The organizer left this hints all around the city and you must get one hint and then solve the riddle for getting the position of the next hint. Now, imagine every hint comes with a little prize.
Well, linked list are like that: every element has "content" on it AND the memory address (the hint) for getting the next item. The next item, of course, has another prize and another hint.
A linked list is a series of object each pointing to the next one in the list. The last element in the list has NULL as it's next pointer.
You keep track of the head of the list in your program so you can traverse the list from the start.
You might want to keep track of the current position in the list, but that will depend on your application.
A doubly linked list has a previous element pointer too enabling you to traverse the list in both directions.
This:
typedef struct tagProp
{
rtPropertyKey key;
rtProperty property;
struct tagProp *next;
} TProperty;
defines a property key/value lookup list.
A linked list is implemented as a list of "nodes". The nodes are linked together using pointers. The following code describes one node. The pointer to the next node is called next. In my example, each node contains an integer value as its data.
struct node {
int val;
struct node * next;
};
The fun is how to actually create a list. You have to use malloc to create new nodes. When you malloc the new node, you have to tie into the list using the next pointer.
We can help you more if you specifically tell us what your issues are...