Advertisement
Guest User

Povezane Liste

a guest
Feb 21st, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.79 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. struct person {
  5.   int startPosition;
  6.   struct person *next;
  7. };
  8. int isAlone(struct person *person) {
  9.     return person == person->next;
  10. }
  11. int main() {
  12.     struct person *first=NULL,*curr, *end=NULL;
  13.    
  14.     for(int i=100;i>0;i--) {
  15.         curr = (struct person *) malloc(sizeof(struct person));
  16.         curr->startPosition = i;
  17.         curr->next = first;
  18.         first = curr;
  19.         if(!end) end = curr;
  20.     }
  21.    
  22.     end->next = first; //Create a circular list
  23.  
  24.     while(!isAlone(first)) {
  25.         curr = first->next;
  26.         first->next = curr->next;
  27.         first = first->next;
  28.  
  29.         free(curr);
  30.     } // can be shortened to first = first->next = first->next->next
  31.  
  32.     printf("%d", first->startPosition);
  33.     return 0;
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement