machkovskitomche

od bosko mn raboti

Aug 9th, 2018
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.94 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. typedef struct _node{
  4.     struct _node *next;
  5.     int element;
  6. }Node;
  7. /*
  8.     Prints the data in the nodes.
  9. */
  10. void printData(Node* head){
  11.     Node* temp;
  12.     temp = head;
  13.     while(temp!=NULL){
  14.         printf("%d",temp->element);
  15.         printf("->");
  16.         temp=temp->next;
  17.     }
  18.     printf("NULL\n");
  19.     free(temp);
  20. }
  21. /*
  22.     Adds a new node to the front.
  23. */
  24. Node* push(Node* head,int data){
  25.     Node* n = (Node*)malloc(sizeof(Node));
  26.     n->element = data
  27. ;   n->next = head;
  28.     return n;
  29. }
  30. /*
  31.     Adds a new node to the end.
  32. */
  33. Node* append(Node* head,int data){
  34.     Node* beg = head;
  35.     Node* n = (Node*)malloc(sizeof(Node));
  36.     n->element = data;
  37.     n->next = NULL;
  38.     if(beg==NULL) return n;
  39.     while(beg->next!=NULL){
  40.         beg=beg->next;
  41.     }
  42.     beg->next = n;
  43.     return head;
  44. }
  45. /*
  46.     Remove first node.
  47. */
  48. Node* pop(Node* head){
  49.     Node* t = head->next;
  50.     free(head);
  51.     return t;
  52. }
  53. /*
  54.     Remove node at position @param(position);
  55. */
  56. Node* deletePosition(Node* head,int position){
  57.     //If empty, do nothing
  58.     if(head == NULL) return head;
  59.     //If first position, pop
  60.     if(position == 0) return pop(head);
  61.     Node* t = head;
  62.     int tempPost = 0;
  63.     while(t!= NULL && position-1>tempPost){
  64.         t=t->next;
  65.         tempPost++;
  66.     }
  67.     if(t == NULL || t->next == NULL) return head;
  68.     //Next node of the next node of temp
  69.     Node* n = t->next->next;
  70.     //Free the temp next
  71.     free(t->next);
  72.     //Connect the temp with the next of the next
  73.     t->next = n;
  74.     return head;
  75. }
  76. //------------------------------------------------------------------------------------------------------------
  77. /*
  78.     Sum all elements from the LinkedList;
  79. */
  80. int sumNode(Node* head){
  81.     Node* t = head;
  82.     int rez = 0;
  83.     while(t!=NULL){
  84.         rez+=t->element;
  85.         t=t->next;
  86.     }
  87.     free(t);
  88.     return rez;
  89. }
  90. /*
  91.     Sum all even numbers;
  92. */
  93. int sumEven(Node* head,int idx){
  94.     if(head == NULL) return 0;
  95.     if(idx%2 == 0) return head->element + sumEven(head->next,idx+1);
  96.     return 0+sumEven(head->next,idx+1);
  97. }
  98. /*
  99.     Sum all elements recursievly from the LinkedList;
  100. */
  101. int sumRec(Node* head){
  102.     if(head == NULL)
  103.         return 0;
  104.     return head->element + sumRec(head->next);
  105. }
  106. /*
  107.     Count all elements from the LinkedList;
  108. */
  109. int count(Node* head){
  110.     Node* t = head;
  111.     int len = 0;
  112.     while(t!=NULL){
  113.         len++;
  114.         t=t->next;
  115.     }
  116.     free(t);
  117.     return len;
  118. }
  119. /*
  120.     Count all recursievly elements from the LinkedList;
  121. */
  122. int countRec(Node* head){
  123.     if(head == NULL)
  124.         return 0;
  125.     return 1+countRec(head->next);
  126. }
  127. /*
  128.     Concatenate two lists together;
  129. */
  130. Node* concatenate(Node* first,Node* second){
  131.     if(first == NULL)   return second;
  132.     if(second == NULL) return first;
  133.     Node* beg = first;
  134.     while(beg->next!=NULL){
  135.         beg=beg->next;
  136.     }
  137.     beg->next = second;
  138.     return first;
  139. }
  140. /*
  141.     Checks if two lists are equal;
  142. */
  143. int areEqual(Node* first,Node* second){
  144.     int flag = 1;
  145.     if(first == NULL && second != NULL) return 0;
  146.     if(first != NULL && second == NULL) return 0;
  147.     Node *p=first,*q=second;
  148.     while(p!=NULL && q!=NULL){
  149.         if(p->element != q->element){
  150.             flag = 0;
  151.             break;
  152.         }
  153.         p=p->next;
  154.         q=q->next;
  155.     }
  156.     return flag;
  157. }
  158. /*
  159.     Makes Cilular linked-list;
  160. */
  161. Node* cilular(Node* head){
  162.     if(head==NULL) return head;
  163.     Node *temp = head;
  164.     while(temp->next!=NULL)
  165.         temp=temp->next;
  166.     temp->next = head;
  167.     return head;
  168. }
  169. /*
  170.     Reverses linked-list;
  171. */
  172. Node* reverse(Node* head){
  173.     Node* prev = NULL;
  174.     Node* curr = head;
  175.     Node* next = head;
  176.     while(curr!=NULL){
  177.         next = curr->next;
  178.         curr->next = prev;
  179.         prev = curr;
  180.         curr = next;
  181.     }
  182.     head = prev;
  183.     return head;
  184. }
  185. /*
  186.     Remove duplicates;
  187. */
  188. void removeDuplicates(Node* head){
  189.     if(head == NULL || head->next == NULL) return;
  190.     Node* temp = head;
  191.     int lastValue = temp->element;
  192.     while(temp->next!=NULL){
  193.         if(lastValue == temp->next->element){
  194.             //Next node of the next node of temp
  195.             Node* n = temp->next->next;        
  196.             //Free the temp next
  197.             free(temp->next);
  198.             //Connect the temp with the next of the next
  199.             temp->next = n;
  200.         }
  201.         else{
  202.             lastValue = temp->element;
  203.             temp=temp->next;       
  204.         }
  205.     }
  206. }
  207. /*
  208.     Returns middle node;
  209. */
  210. Node* middle(Node* head){
  211.     Node *fast = head,
  212.          *slow = head;
  213.     while(fast!=NULL&&slow!=NULL){
  214.         fast=fast->next->next;
  215.         slow=slow->next;
  216.     }
  217.     return slow;
  218. }
  219. /*
  220.     Split at middle;
  221. */
  222. Node* split(Node* head){
  223.     Node* mid = middle(head);
  224.     Node* helper = mid;
  225.     helper->next = NULL;
  226. }
  227. /*
  228.     Copy list;
  229. */
  230. Node* copy(Node* head){
  231.     Node* temp = head;
  232.     Node* new = NULL;
  233.     while(temp!=NULL){
  234.         new = append(new,temp->element);
  235.         temp = temp->next;
  236.     }
  237.     free(temp);
  238.     return new;
  239. }
  240. /*
  241.     Delete all nodes;
  242. */
  243. void deleteAllRec(Node* head){
  244.     if(head==NULL) return; 
  245.     deleteAllRec(head->next);
  246.     free(head);
  247. }
  248. /*
  249.     Detects loop;
  250. */
  251. int detectLoop(Node* head){
  252.     Node *sp = head, *fp = head;
  253.     while(sp!=NULL && fp!=NULL && fp->next!=NULL){
  254.         sp = sp->next;
  255.         fp=fp->next->next;
  256.         if(sp == fp){
  257.             printf("Found loop @ %d\n",sp->element);
  258.             return 1;
  259.             }      
  260.         }
  261.     return 0;
  262. }
  263. /*
  264.     Special delete case;
  265. */
  266. Node* filter(Node* head, int max){
  267.     if(head==NULL)
  268.         return NULL;
  269.     if(head->element == max){
  270.         free(head);
  271.         return filter(head->next,max);
  272.     }
  273.     else{
  274.         head->next = filter(head->next,max);
  275.         return head;
  276.     }
  277. }
  278. /*
  279.     Loopify;
  280. */
  281. Node* loopify(Node* head){
  282.     if(head == NULL) return NULL;
  283.     if(head->next == NULL) return head;
  284.     Node* last = head;
  285.     while(last->next!=NULL){
  286.         last=last->next;
  287.     }
  288.     while(head->element!=last->element){
  289.         head = head->next;
  290.     }
  291.     last->next = head;
  292.     return head;
  293. }
  294. Node* addSpecial(Node* head, int data){
  295.     Node*  new = malloc(sizeof(Node));
  296.     new->element = data;
  297.     new->next = NULL;
  298.     if(head==NULL){
  299.         return new;
  300.     }
  301.     if(head->element == data) return NULL;
  302.     if(head->element>data){
  303.         new->next = head;
  304.         head = new;
  305.         return head;
  306.     }
  307.     Node* temp = head;
  308.     while(temp->next!=NULL){
  309.         if(temp->next->element == data) return NULL;
  310.         if(temp->next->element > data){
  311.             new->next = temp->next;
  312.             temp->next = new;
  313.             return head;
  314.         }
  315.         temp = temp->next;
  316.     }
  317.     temp->next = new;
  318.     return head;
  319. }
  320. Node* addSpecialRec(Node* head,int data){
  321.     if(head==NULL || head->element >= data){
  322.         Node* new = malloc(sizeof(Node));
  323.         new->next = NULL;
  324.         new->element = data;
  325.         return new;
  326.     }
  327.     else{
  328.         head->next = addSpecialRec(head->next,data);
  329.         return head;
  330.     }
  331. }
  332. Node* delRec(Node* head,int data){
  333.     if(head == NULL) return NULL;
  334.     if(head->element == data) {
  335.         free(head);
  336.         return delRec(head->next,data);
  337.     }
  338.     else{
  339.         head->next = delRec(head->next,data);
  340.         return head;
  341.     }
  342. }
  343. Node* addEnd(Node* head,int data){
  344.     if(head==NULL){
  345.         Node* new = malloc(sizeof(Node));
  346.         new->element=data;
  347.         new->next = NULL;
  348.         return new;
  349.     }
  350.     else{
  351.         head->next = addEnd(head->next,data);
  352.         return head;
  353.     }
  354. }
  355. /*
  356.     Test program;
  357. */
  358. int main(){
  359.     Node* first = NULL;
  360.     //int t; scanf("%d",&t);
  361.     //int n = t;
  362.  
  363.     //while(n>0){      
  364.     //  first = push(first,(n%10));
  365.     //  n=n/10;
  366.     //}
  367.  
  368.     printData(first);
  369.     first = addEnd(first,32);
  370.     first = addEnd(first,2);   
  371.  
  372.     printData(first);
  373.     return 0;
  374.  
  375. }
Add Comment
Please, Sign In to add comment