hozer

DDS - Lab2 (1-way linked list)

Apr 22nd, 2014
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.10 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4.  
  5. typedef struct _node {
  6.     char data[64];
  7.     struct _node *next;
  8. } node;
  9.  
  10. addNode(node **head, char s[64])
  11. {
  12.     node * el = *head;
  13.     node * n = (node*) malloc(sizeof(node));
  14.     n->next = NULL;
  15.     strcpy(n->data, s);
  16.     if (!*head)
  17.     {
  18.         *head = n; return;
  19.     }
  20.     while (el->next)
  21.         el = el->next;
  22.     el->next = n;
  23. }
  24.  
  25. node *finder(node *head1, node* head2)
  26. {
  27.     node *l1, *l2, *rhead = NULL;
  28.     l1 = head1;
  29.     while (l1)
  30.     {
  31.         int c=0;
  32.         l2 = head2;
  33.         while (l2)
  34.         {
  35.             if (!strcmp(l1->data, l2->data)) c++;
  36.             l2 = l2->next;
  37.         }
  38.         if (!c) addNode(&rhead, l1->data);
  39.         l1 = l1->next;
  40.     }
  41.     return rhead;
  42. }
  43.  
  44. reprint(node *head)
  45. {
  46.     if (!head) return;
  47.     reprint(head->next);
  48.     printf("%s ", head->data);
  49. }
  50.  
  51. int main()
  52. {
  53.     int n, i, fi;
  54.     char s[64];
  55.     node *head1 = NULL, *head2 = NULL, *rhead = NULL,  *f = NULL;
  56.  
  57.     printf("input L1: ");
  58.     while (strcmp(gets(s), ""))
  59.         addNode(&head1, s);
  60.  
  61.     printf("input L2: ");
  62.     while (strcmp(gets(s), ""))
  63.         addNode(&head2, s);
  64.  
  65.     rhead = finder(head1, head2);
  66.  
  67.     reprint(rhead);
  68.     _getch();
  69.     return 0;
  70. }
Advertisement
Add Comment
Please, Sign In to add comment