+ 2
Link list
What exactly is a link list? How is it built? How does it work/behave? Thanks
3 Respostas
+ 1
Linked list is a list of elements of two variables, one is the value, the other one a pointer pointing to the next value, by grouping all this element we obtain the linked list.
We build it by defining a structure that define the element, of the list:
typedef struct element {
TheTypeOfTheValue value; //example : int number;
element * nextValue; /*the pointer pointing to the next element*/
}element;
/*And defining a list by having a pointer that points to the first element:*/
typedef element * linkedList;
/*We can declare a list by the following instructions:*/
linkedList list1=NULL;
element * list2=NULL;
struct element * list3=NULL;
/*Or adding elements to it*/
likedList addElement (linkedList list, TheTypeOfTheValue val){
/*creating the new element*/
element * newElement=malloc (sizeof (element));
/*Giving the value to the new element*/
newElement -> val = value;
/*Assigning the address of the new element nextValue to the new element*/
newElement ->nextValue = likedList;
/*We return the new list by returning the pointer of the first element*/
return newElement;
}
+ 1
In fact, the code I showed you above is the C implementation of a Linked List, for the Java one, all is predefined in the LinkedList Class and it behaves like I said to you, faciliting adds and removes functions way more than ArrayList, you can see the linked list as a chain and an a ArrayList as an array.
0
I understood the concept but not how it's done in code....