Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- struct person {
- int startPosition;
- struct person *next;
- };
- int isAlone(struct person *person) {
- return person == person->next;
- }
- int main() {
- struct person *first=NULL,*curr, *end=NULL;
- for(int i=100;i>0;i--) {
- curr = (struct person *) malloc(sizeof(struct person));
- curr->startPosition = i;
- curr->next = first;
- first = curr;
- if(!end) end = curr;
- }
- end->next = first; //Create a circular list
- while(!isAlone(first)) {
- curr = first->next;
- first->next = curr->next;
- first = first->next;
- free(curr);
- } // can be shortened to first = first->next = first->next->next
- printf("%d", first->startPosition);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement