Advertisement
vinocastro

ME01

Oct 5th, 2020 (edited)
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.95 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. typedef struct node Node;
  4. struct node{
  5.  int data;
  6.  Node* next;
  7. };
  8.  
  9. void build_linked_list(Node **head_ptr)
  10. {
  11.   (*head_ptr)->next = NULL;
  12.     Node* new_node = (Node*)malloc(sizeof(Node));
  13.   int num;
  14.     while(1)
  15.     {
  16.         scanf("%d",&num);
  17.         if(num == -1)
  18.         {
  19.             break;
  20.         }
  21.         else
  22.         {
  23.             new_node->data = num;
  24.                         new_node->next = *head_ptr;
  25.                         *head_ptr = new_node;
  26.         }
  27.     }
  28. }
  29.  
  30. int count(Node *head, int x)
  31. {
  32.   int count = 0;
  33.   Node* current = head;
  34.   while(current != NULL)
  35.   {
  36.     if(current->data == x) count++;
  37.     current = current->next;
  38.   }
  39.   return count;
  40. }
  41.  
  42. int get_nth(Node *head, int n)
  43. {
  44.   Node* current = head;
  45.   int index = 1,result;
  46.   while(current!= NULL)
  47.   {
  48.     if(index == n)
  49.     {
  50.       result = current->data;
  51.       break;
  52.     }
  53.     current =  current->next;
  54.     index++;
  55.   }
  56.   return result;
  57. }
  58.  
  59.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement