Advertisement
andruhovski

Prog-0201 (linked list)

Oct 2nd, 2014
312
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.00 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include <stdlib.h>
  3.  
  4. typedef int DATA;
  5. struct Node {
  6.     DATA value;   // дані
  7.     Node *next;   // поле зв'зку
  8. };
  9.  
  10. typedef struct Node NODE;
  11. typedef struct Node* PNODE;
  12.  
  13. struct LIST {
  14.     PNODE Head, Tail;
  15. } firstList = { NULL, NULL };
  16.  
  17. PNODE makeNode(DATA val);
  18. void initList(LIST *lst , DATA val);
  19. bool addNodeToEnd(PNODE *tail, DATA val);
  20. bool addNodeToHead(PNODE *head, DATA val);
  21. bool addNodeByIndex(PNODE *head, DATA val, unsigned pos);
  22. PNODE FindNode(PNODE head, DATA key);
  23. bool DeleteNode(PNODE *head, DATA key);
  24. bool DeleteNodeByIndex(PNODE *head, unsigned pos);
  25. void PrintList(const LIST *list);
  26. // void SortList();
  27. // void MergeList();
  28.  
  29. struct STACK {
  30.     PNODE top;
  31. };
  32. void s_push(STACK *st, DATA value);
  33. bool s_pop(STACK *st, DATA *value);
  34.  
  35. struct QUEUE {
  36.     PNODE Head, Tail;
  37. };
  38.  
  39. void q_push(QUEUE *q, DATA value);
  40. bool q_pop(QUEUE *q, DATA *value);
  41.  
  42. int _tmain(int argc, _TCHAR* argv[])
  43. {
  44.     initList(&firstList, 12); //12
  45.     addNodeToHead(&firstList.Head, 99); // 99 12
  46.     PrintList(&firstList);
  47.     addNodeToEnd(&firstList.Tail, 111); // 99 12 111
  48.     PrintList(&firstList);
  49.     for (int i = 1; i<15; i++)
  50.         addNodeToEnd(&firstList.Tail, i);
  51.     PrintList(&firstList);
  52.     DeleteNode(&firstList.Head, 20);
  53.     if (FindNode(firstList.Head, 20) != NULL)
  54.         printf_s("\nYes!\n");
  55.     else
  56.         printf_s("\nNo!\n");
  57.     PrintList(&firstList);
  58.     if (DeleteNodeByIndex(&firstList.Head, 14))
  59.         printf_s("\nYes!\n");
  60.     else
  61.         printf_s("\nNo!\n");
  62.     PrintList(&firstList);
  63.     addNodeByIndex(&firstList.Head, 1111, 5);
  64.     PrintList(&firstList);
  65.  
  66.     STACK a={ NULL };
  67.     int x;
  68.     push(&a, 1);
  69.     push(&a, 2);
  70.     push(&a, 3);
  71.     while (pop(&a, &x))
  72.     {
  73.         printf("%d", x);
  74.     };
  75.     QUEUE q1 = { NULL, NULL };
  76.     q_push(&q1, 1);
  77.     q_push(&q1, 2);
  78.     q_push(&q1, 3);
  79.     while (q_pop(&q1, &x))
  80.     {
  81.         printf("%d", x);
  82.     };
  83.     return 0;
  84. }
  85.  
  86. PNODE makeNode(DATA val)
  87. {
  88.     PNODE tmp;
  89.     tmp = (PNODE)calloc(1, sizeof(NODE));
  90.     tmp->value = val;
  91.     tmp->next = NULL;
  92.     return tmp;
  93. }
  94.  
  95. void initList(LIST *lst, DATA val)
  96. {
  97.     lst->Head = lst->Tail = makeNode(val);
  98. }
  99.  
  100. bool addNodeToEnd(PNODE *tail, DATA val)
  101. {
  102.     PNODE temp = makeNode(val);
  103.     (*tail)->next = temp;
  104.     *tail = temp;
  105.     return true;
  106. }
  107.  
  108. bool addNodeToHead(PNODE *head, DATA val)
  109. {
  110.     PNODE temp = makeNode(val);
  111.     temp->next = *head;
  112.     *head = temp;
  113.     return true;
  114. }
  115.  
  116. PNODE FindNode(PNODE head, DATA key)
  117. {
  118.     PNODE temp;
  119.     for (temp = head; temp != NULL; temp = temp->next)
  120.         if (temp->value == key) break;
  121.     return temp;
  122. }
  123.  
  124. bool DeleteNode(PNODE *head, DATA key)
  125. {
  126.     PNODE temp = *head, temp1 = *head;
  127.     if ((*head)->value == key)
  128.     {
  129.         *head = temp->next;
  130.         free(temp);
  131.         return true;
  132.     }
  133.     else
  134.     {
  135.         for (temp = (*head)->next; temp != NULL; temp1 = temp, temp = temp->next)
  136.             if (temp->value == key) {
  137.             temp1->next = temp->next;
  138.             free(temp);
  139.             return true;
  140.             }
  141.     }
  142.     return false;
  143. }
  144.  
  145. bool DeleteNodeByIndex(PNODE *head, unsigned pos)
  146. {
  147.     PNODE temp = *head, temp1 = *head;
  148.     if (!pos) {
  149.         *head = temp->next;
  150.         free(temp);
  151.         return true;
  152.     }
  153.     else {
  154.         for (temp = (*head)->next; temp != NULL; temp1 = temp, temp = temp->next)
  155.             if (pos-- == 1) {
  156.             temp1->next = temp->next;
  157.             free(temp);
  158.             return true;
  159.             }
  160.     }
  161.     return false;
  162. }
  163.  
  164. void PrintList(const LIST *list)
  165. {
  166.     PNODE temp;
  167.     for (temp = list->Head; temp != NULL; temp = temp->next)
  168.     {
  169.         printf_s("%d ", temp->value);
  170.     }
  171.     printf("\n--------------------\n");
  172. }
  173.  
  174. bool addNodeByIndex(PNODE *head, DATA val, unsigned pos)
  175. {
  176.     PNODE temp, temp1 = *head;
  177.     if (!pos--)
  178.         return addNodeToHead(head, val);
  179.     for (temp = (*head)->next; temp != NULL; temp1 = temp, temp = temp->next)
  180.     {
  181.         if (!pos--) {
  182.             PNODE new_element = makeNode(val);
  183.             new_element->next = temp;
  184.             temp1->next = new_element;
  185.             return true;
  186.         }
  187.     }
  188.     if (!pos) {
  189.         PNODE new_element = makeNode(val);
  190.         new_element->next = temp;
  191.         temp1->next = new_element;
  192.         return true;
  193.     }
  194.     return false;
  195. }
  196.  
  197. PNODE addNode(DATA number, PNODE next)
  198. {
  199.     PNODE tnode = makeNode(number);
  200.     tnode->next = next;
  201.     return tnode;
  202. }
  203.  
  204. int Length(PNODE head)
  205. {
  206.     int count = 0;
  207.     for (PNODE current = head; current != NULL; current = current->next) count++;
  208.     return count;
  209. }
  210.  
  211. void FrontBackSplit(PNODE source, PNODE* frontRef, PNODE* backRef)
  212. {
  213.  
  214.     int len = Length(source);
  215.     int i;
  216.     PNODE current = source;
  217.     if (len < 2)
  218.     {
  219.         *frontRef = source;
  220.         *backRef = NULL;
  221.     }
  222.     else
  223.     {
  224.         int hopCount = (len - 1) / 2;
  225.         for (i = 0; i<hopCount; i++)
  226.         {
  227.             current = current->next;
  228.         }
  229.         // початковий список розбивается на два подсписка
  230.         *frontRef = source;
  231.         *backRef = current->next;
  232.         current->next = NULL;
  233.     }
  234. }
  235.  
  236. PNODE SortedMerge(PNODE a, PNODE b)
  237. {
  238.     PNODE result = NULL;
  239.     if (a == NULL)
  240.         return b;
  241.     else
  242.         if (b == NULL)
  243.             return a;
  244.     if (a->value <= b->value)
  245.     {
  246.         result = a;
  247.         result->next = SortedMerge(a->next, b);
  248.     }
  249.     else
  250.     {
  251.         result = b;
  252.         result->next = SortedMerge(a, b->next);
  253.     }
  254.     return(result);
  255. }
  256.  
  257.  
  258. void MergeSort(PNODE* headRef)
  259. {
  260.     PNODE head = *headRef;
  261.     PNODE a;
  262.     PNODE b;
  263.     // вирождений випадок– довжина списка рівна 0 чи 1
  264.     if ((head == NULL) || (head->next == NULL))
  265.     {
  266.         return;
  267.     }
  268.     FrontBackSplit(head, &a, &b);
  269.     MergeSort(&a); // рекурсивне сортування підсписків
  270.     MergeSort(&b);
  271.     *headRef = SortedMerge(a, b);
  272. }
  273.  
  274. void s_push(STACK *st, DATA value)
  275. {
  276.     PNODE temp;
  277.     temp = makeNode(value);
  278.     if (st->top != NULL)
  279.         temp->next = st->top;
  280.     st->top = temp;
  281. }
  282. bool s_pop(STACK *st, DATA *value){
  283.     PNODE var;
  284.     bool res = st->top != NULL;
  285.     if (res)
  286.     {
  287.         var = st->top;
  288.         st->top = st->top->next;
  289.         *value = var->value;
  290.         free(var);
  291.     }
  292.     return res;
  293. }
  294.  
  295. void q_push(QUEUE *q, DATA value)
  296. {
  297.     if (q->Head == NULL)
  298.         initList((LIST *)q, value);
  299.     else
  300.         addNodeToEnd(&q->Tail, value); 
  301. }
  302.  
  303. bool q_pop(QUEUE *q, DATA *value)
  304. {
  305.     bool res = (q->Head != NULL);
  306.     if (res)
  307.     {
  308.         *value = q->Head->value;
  309.         DeleteNodeByIndex(&q->Head, 0);
  310.     }
  311.     return res;
  312. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement