Link List



I recently got this assignment from someone and one of the question he/she asked was regarding link list of integers in c.


I know that link list was something to do about pointers and their structures (such in database). So how actually do I call it. Since I lasted played c was during my class in which I made a simple game project. Huhu. The best thing about it, which was lectured by Hj Asri, is that in history of his teaching in UiTM nobody has ever done a game before. My game was basic movements of array in which a pointer called to write on a text file for saving. Many bugs but not to those who is familiar with it. The only problem with that game it is supported for windows only. In which I called a header for callings on the system files.

Enough of the history, lets talk about this assignment I received. Question no 5, Implement a Link List of integers using C. I called a friend, a hustler in programming, asked about such question. He notify me back that he knows of it but haven't applied in c. Plus he qouted:
"Hahaha. Linked list ni selalu utk database."
So I was right. I remembered Link List as I was trying to implement my c project. Such programming actually is beyond the fundamentals teached in class, and thought that my lecturer might think I'm over doing it because it is not in the syllabus. So I did only in arrays for my c.

So what is Link List(LL)?
Link list as I told earlier is something related to pointers that could be callback. Such as in database. Cases as this actually points to a memory address and called out in that particular memory address. Plus, LL sets each next address as their pointer.

Here is a brief image of my words. Where is context of A, B and C has a pointer to call out. It's just like arrays of numbers. Let say you have array[]={343,366,288,509}. Pointer for context A is 343, B = 366, C = 288 and D = 509.


Correct if I'm wrong guys. So here is what I've written in the assignment.

#include<stdio.h>
//Linked List of integers
//If given array as below
//array[]=(343,366,288,509);

typedef struct db{
  int data;
  struct db *next;
}linklist;

linklist *list(linklist **result, int id){
    linklist *temp = malloc(sizeof(linklist));
    temp->data = id;
    temp->next = *result;
    *result = temp;
}

int main()
{
    linklist *pointer = NULL;
    list(&pointer, 509);
    list(&pointer, 288);
    list(&pointer, 366);
    list(&pointer, 343);

   do{
        printf("%i\n", pointer->data);
        pointer = pointer->next;
    }while(pointer!=NULL);

 return 0;
}
Now the explaination part.

#include<stdio.h>
Calls the standard input output header. Think I won't be touching on that.
typedef struct db{
  int data;
  struct db *next;
}linklist;

Here I wrote a type to define. The function of the syntax to simplify the declaration for the compound type struct and the pointer type.Instead of writing down
struct db{
...};
db linklist;
It can be shortened such as
typedef struct db{
 ...
}linklist;
I defined db as main structure which has an integer data and structured db pointer of next as its member. Which linklist as declaration of the main structure.

linklist *list(linklist **result, int id){
    linklist *temp = malloc(sizeof(linklist));
    temp->data = id;
    temp->next = *result;
    *result = temp;
}
 
Here, I function of type that points to linklist. Where I inserted a pointer to pointer of linklist Followed by id in integer as the data. Where I defined a temporary  linklist which locates the memory of information of linklist. Then define each temporary  linklist as members of db.

The reason of using pointer in pointer is because the first pointer calls out the secound pointer which has the structure addressing. So every time I moved around, the first pointer reads the second pointer address that has the structure's address. In which, the temporary pointer is placed in the pointer list. Here is what it means in graphical method.


This then I set the parameter of my pointer result from the temporary variable.

Next, my main function.
int main()
{
    linklist *pointer = NULL;
    list(&pointer, 509);
    list(&pointer, 288);
    list(&pointer, 366);
    list(&pointer, 343);

   do{
        printf("%i\n", pointer->data);
        pointer = pointer->next;
    }while(pointer!=NULL);

 return 0;
}
Let see the first coding inside the main function.
linklist *pointer = NULL;
I setup a pointer in the linklist as NULL. So that, so that my root of the db is wiped out.
list(&pointer, 343);
list(&pointer, 366);
list(&pointer, 288);
list(&pointer, 509);
So now what I've done above are to insert a value in the list by each pointer. Where actually, 509 is being the 1st pointer. Here on I inserted the next coding.
do{
        printf("%i\n", pointer->data);
        pointer = pointer->next;
    }while(pointer!=NULL);
Which it repeat a loop until the pointer goes back to NULL. Being called out reversely. So, when I stated "pointer=pointer->next", what it does is that it goes back one memory location at the previous pointer. Here is the picture of my structure.
Hmm..The output goes something like this.
509
288
366
343
Hope this assignment goes well.

Comments

Popular Posts