Rakibul_Ahasan

Linked List 01

Jun 21st, 2022 (edited)
577
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. struct LinkedList{
  5.     int data;
  6.     struct LinkedList *next;
  7.  };
  8.  
  9. typedef struct LinkedList *node;
  10.  
  11. void printList(node n){
  12.     while (n != NULL) {
  13.         printf("%d ",n->data);
  14.         n = n->next;
  15.     }
  16. }
  17.  
  18. node createNode(){
  19.     node temp;
  20.     temp = (node)malloc(sizeof(struct LinkedList));
  21.     temp->next = NULL;
  22.     return temp;
  23. }
  24.  
  25. node addNode(node head, int value){
  26.     node temp,p;
  27.     temp = createNode();
  28.     temp->data = value;
  29.     if(head == NULL){
  30.         head = temp;
  31.     }
  32.     else{
  33.         p  = head;
  34.         while(p->next != NULL){
  35.             p = p->next;
  36.         }
  37.         p->next = temp;//Point the previous last node to the new node created.
  38.     }
  39.     return head;
  40. }
  41.  
  42. int main(){
  43.     node head = NULL;
  44.     head = addNode(head, 10);
  45.     addNode(head, 20);
  46.     printList(head);
  47. }
Add Comment
Please, Sign In to add comment