Advertisement
tresonance

LINKED LIST FROM SCRATCH

Apr 15th, 2020
281
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.83 KB | None | 0 0
  1. //MATHSPHYSIC CODE
  2. //LET CREATE SIMPLE LINKED LIST FROM SCRATCH
  3. //STEP 0: INCLUDE LIBRARY
  4. #include <stdio.h> //printf to use
  5. #include <string.h> //strcpy function from here
  6. #include <stdlib.h> //malloc & free
  7. #define MAX_SIZE 25
  8.  
  9. //STEP 1: CRETE STRUCT NODE (SILPMY CREATE LIST'S NODE AND LIST )
  10. //this is linked list node
  11. typedef struct s_node {
  12.     char name[MAX_SIZE]; //our data here
  13.     struct s_node *next; //to move to the next node (pointer to the next node)
  14. }t_node;
  15.  
  16. //this is our linked list: linked list is simply pointer of our node
  17. typedef struct s_list {
  18.     t_node *node_pointer;
  19. }t_list;
  20.  
  21.  
  22. //STEP 2: INITIALIZE LINKED LIST NODE
  23. t_node *new_node(const char name[]){ //name is the string to add to linked list
  24.     t_node *node = (t_node*)malloc(sizeof(t_node));
  25.     if(node == NULL) //always test if malloc failed
  26.         return NULL;
  27.     strcpy(node->name,  name); // strcy = string copy function
  28.     node->next = NULL; //null because we don't add this node to linked list yet
  29.     return node;
  30. }
  31.  
  32.  
  33. //STEP 3: CREATE FUNCTION TO ADDA CONTENT TO THEM
  34. void addContent(t_list **head, const char name[]){
  35.     t_node *node = new_node(name);
  36.     if(node != NULL){
  37.         if(!head || !*head || !(*head)->node_pointer){ //if list is empty
  38.             (*head)->node_pointer = node;
  39.             return ;
  40.         }
  41.         //if list is not empty, go to the end and add your content
  42.         t_node *tmp = (*head)->node_pointer;
  43.         while(tmp && tmp->next)
  44.             tmp = tmp->next; //this means move to the next node
  45.         tmp->next = node; //here at the end of list we add our data
  46.     }
  47. }
  48.  
  49. //STEP 4: DISPLAY RESULT AND THEN ...... DEALLOCATE MEMORY BY DESTROYING LIST
  50. void display(t_list *list){
  51.     if(list == NULL)
  52.         return;
  53.     t_node *tmp = list->node_pointer;
  54.     while(tmp){
  55.         printf("[ %s ] ", tmp->name);
  56.         tmp = tmp->next; //it is like i++ or i=i+1 in array loop
  57.     }
  58. }
  59.  
  60. void destroyList(t_list **list){
  61.     if(list && *list){
  62.         t_node *tmp;
  63.         while ((*list)->node_pointer){
  64.             tmp = (*list)->node_pointer;
  65.             (*list)->node_pointer = tmp->next;
  66.             free(tmp);
  67.         }
  68.         free(*list);
  69.         list = NULL; //to avoid dangling pointer
  70.     }
  71. }
  72.  
  73.  
  74. //STEP 5: THE MAIN FUNCTION TO TEST
  75. int main(){
  76.     t_list *list = (t_list*)malloc(sizeof(t_list));
  77.     if(list == NULL)
  78.         return (-1);
  79.  
  80.     list->node_pointer = NULL; //we must set this pointer to null because no node yet
  81.     //here are data to add
  82.     const char *names[]= {"John", "Isabella", "Adriana", "kwassi", "Ruth", NULL};
  83.     int i = -1;
  84.  
  85.     //let add all these datas
  86.     while(names[++i])
  87.         addContent(&list, names[i]);
  88.  
  89.     //display content now
  90.     display(list);
  91.  
  92.     //destroy list to deallocate memory
  93.     destroyList(&list);
  94.  
  95.     return (0);
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement