hozer

DDS - AL7Lab

Jun 2nd, 2014
271
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.61 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4.  
  5. typedef struct _node {
  6.     int data;
  7.     struct _node *next;
  8. } node;
  9.  
  10. node *addNode(node *head, int num)
  11. {
  12.     node * el = head;
  13.     node * n = (node*) malloc(sizeof(node));
  14.     n->next = NULL;
  15.     n->data = num;
  16.     if (!head)
  17.     {
  18.         return n;
  19.     }
  20.     while (el->next)
  21.         el = el->next;
  22.     el->next = n;
  23.     return head;
  24. }
  25.  
  26. node *replace(node *headSource, node *headToRep, node *headRep)
  27. {
  28.     node *elStart = headSource, *elEnd = headSource, *elStartPrev = headSource;
  29.    
  30.     while (elStart)
  31.     {
  32.         node *elf = headToRep;
  33.         if (elStart->data == elf->data)
  34.         {
  35.             elEnd = elStart;
  36.             while (elf && elEnd && elEnd->data == elf->data)
  37.             {
  38.                 elEnd = elEnd->next;
  39.                 elf = elf->next;
  40.             }
  41.             if (!elf)
  42.             {
  43.                 node *el = headRep;
  44.                 elStartPrev->next = headRep;
  45.                 while (el->next)
  46.                     el = el->next;
  47.                 el->next = elEnd;
  48.             }
  49.         }
  50.         elStartPrev = elStart;
  51.         elStart = elStart->next;
  52.     }
  53.  
  54.     return headSource;
  55. }
  56.  
  57. void printList(node *head)
  58. {
  59.     node *el = head;
  60.     while (el)
  61.     {
  62.         printf("%d ", el->data);
  63.         el = el->next;
  64.     }
  65. }
  66.  
  67. int main()
  68. {
  69.     int i, t;
  70.     node *headS = NULL, *headTR = NULL, *headR = NULL, *in = NULL;
  71.  
  72.     printf("input L1: ");
  73.     while (1)
  74.     {
  75.         scanf("%d", &t);
  76.         if (t == 0) break;
  77.         headS = addNode(headS, t);
  78.     }
  79.  
  80.     printf("input L2: ");
  81.     while (1)
  82.     {
  83.         scanf("%d", &t);
  84.         if (t == 0) break;
  85.         headTR = addNode(headTR, t);
  86.     }
  87.    
  88.     printf("input L3: ");
  89.     while (1)
  90.     {
  91.         scanf("%d", &t);
  92.         if (t == 0) break;
  93.         headR = addNode(headR, t);
  94.     }
  95.  
  96.     replace(headS, headTR, headR);
  97.     printList(headS);
  98.  
  99.     _getch();
  100.     return 0;
  101. }
Advertisement
Add Comment
Please, Sign In to add comment