Advertisement
Guest User

Untitled

a guest
Jun 27th, 2017
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.42 KB | None | 0 0
  1. /*-----------------------------
  2. Program 9 by Rebecca VanderClute
  3. -----------------------------*/
  4.  
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include <stdio.h>
  8. #include "leak.h"
  9.  
  10. struct node
  11. {
  12.     int data;
  13.     struct node *next;
  14. };
  15.  
  16. struct node *searchList(int value, node *head);
  17.  
  18. int main()
  19. {
  20.     struct node *first, *current, *temp;
  21.     first = calloc(1, sizeof(struct node));
  22.     current = first;
  23.     int i = 0;
  24.     for (i = 0; i < 9; i++)
  25.     {
  26.         temp = calloc(1, sizeof(struct node));
  27.         temp->data = i;
  28.         current->next = temp;
  29.         current = current->next;
  30.     }
  31.     current = first->next;
  32.     printf("Printing node values...\n");
  33.     while (current)
  34.     {
  35.         printf("%d\n", current->data);
  36.         current = current->next;
  37.     }  
  38.     int search = 0;
  39.     printf("-----------------\n");
  40.     printf("Please enter a value to search for: ");
  41.     scanf("%d", &search);
  42.     current = first->next;
  43.     printf("[%d %d]\n", search, searchList(search, &first)->data);
  44.     printf("(if the number returned happens to be different than the number searched for and/or really huge, it means that it couldn't find it. Curse you lack of error checking caused by instructions! *shakes fist*)\n");
  45.     current = first->next;
  46.     while (current)
  47.     {
  48.         free(current);
  49.         current = current->next;
  50.     }
  51.     displayLeaks();
  52.     return 0;
  53. }
  54.  
  55. struct node *searchList(int value, node *head)
  56. {
  57.     while(head)
  58.     {
  59.         if(value == head->value)
  60.         {
  61.             return head;
  62.         }
  63.         head = head->next;
  64.     }
  65.     return NULL;
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement