hozer

DDS - LabApakA3

May 27th, 2014
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.43 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.         while (el->next)
  19.             el = el->next;
  20.         el->next = n;
  21.         return head;
  22.     }
  23.     else
  24.     {
  25.         return n;
  26.     }
  27. }
  28.  
  29. node *addBeforeHead(node *head, int data)
  30. {
  31.     node *nNode;
  32.     nNode = (node*) malloc(sizeof(node));
  33.     nNode->data = data;
  34.     nNode->next = NULL;
  35.     if (head)
  36.         nNode->next = head;
  37.     head = nNode;
  38.     return head;
  39. }
  40.  
  41. void removeNode(node **head, node *del)
  42. {
  43.     node *el = *head;
  44.  
  45.     if (*head == del)
  46.     {
  47.         *head = del->next;
  48.     }
  49.     else
  50.     {
  51.         while (el->next && el->next != del)
  52.             el = el->next;
  53.  
  54.         if (!el->next) return;
  55.         el->next = del->next;
  56.     }
  57.     free(del);
  58. }
  59.  
  60.  
  61. void printList(node *head)
  62. {
  63.     node *el = head;
  64.     while (el)
  65.     {
  66.         printf("%d, ", el->data);
  67.         el = el->next;
  68.     }
  69. }
  70.  
  71.  
  72. int main()
  73. {
  74.     int n, i, fi, t;
  75.     node *head = NULL, *f = NULL, *h2 = NULL;
  76.  
  77.     while (1)
  78.     {
  79.         scanf("%d", &t);
  80.         if (t == 0) break;
  81.         head = addNode(head, t);
  82.     }
  83.  
  84.     f = head;
  85.     while (f)
  86.     {
  87.         int tmp;
  88.         if (f->data < 0)
  89.         {
  90.             tmp = f->data;
  91.             removeNode(&head, f);
  92.             h2 = addBeforeHead(h2, tmp);
  93.             f = head;
  94.         }
  95.         f = f->next;
  96.     }
  97.  
  98.     f = head;
  99.     while (f->next)
  100.         f = f->next;
  101.     f->next = h2;
  102.  
  103.     printList(head);
  104.  
  105.     _getch();
  106.     return 0;
  107. }
Advertisement
Add Comment
Please, Sign In to add comment