Advertisement
Guest User

hashtable

a guest
Jul 6th, 2014
240
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.25 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. typedef struct node
  5. {
  6.     int val;
  7.     struct node *next;
  8. }node;
  9.  
  10. void insert(int input);
  11. void print(void);
  12.  
  13. node *first[10];
  14.  
  15. int main(void)
  16. {
  17.     for (int i = 0; i < 10; i++)
  18.     {
  19.         insert(i);
  20.     }
  21.     print();
  22. }
  23.  
  24. void print(void)
  25. {
  26.     node *curr = first[1];
  27.     while (curr != NULL)
  28.     {
  29.         printf("%d\t", curr -> val);
  30.         curr = curr -> next;
  31.     }
  32.     printf("\n");
  33. }
  34. // takes the number to be inputted. aslo calculates the right position to insert into
  35. void insert(int input)
  36. {
  37.         // calculate which position to insert into
  38.         int index = 1;
  39.  
  40.         node* new = malloc(sizeof(node));
  41.         new->val = input;
  42.  
  43.         // insertion in case of empty list
  44.         if (first[index] == NULL)
  45.         {
  46.                 first[index] = new;
  47.                 new->next = NULL;
  48.                 return;
  49.         }
  50.  
  51.         // if list not empty, insert at front
  52.         else
  53.         {
  54.                 // in case number already exists at head of list
  55.  
  56.                 if (first[index]->val == input)
  57.                 {
  58.                         return;
  59.                 }
  60.  
  61.                 new->next = first[index];
  62.                 first[index] = new;
  63.                 return;
  64.         }
  65.  
  66.         return;
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement