#include<stdlib.h>
#include<stdio.h>

struct process { int Num; struct process *next;} *Act=NULL;

int Empty() { if(Act!=NULL)return 0; else { printf("E!\n") ; return 1; } }

int Active() { if(Empty())return -1; return Act->Num; }

int Next() { 
if(Empty())return -1; Act=Act->next; Stampa("N"); return Active(); }

void Insert(int Num) {
  struct process *Tmp, *Pos=Act;
  if(!Empty()) // Get Position before Act
    while(Pos->next!=Act) Pos=Pos->next; 
  Tmp=(struct process *)malloc(sizeof(struct process));
  if(Empty()) { Act=Tmp; Pos=Tmp; }
  Tmp->Num=Num; Tmp->next=Act; Pos->next=Tmp;
  Stampa("I"); // Debug.
}

int Done() {
  struct process *Pos=Act; int Num;
  if(Empty())return -1; else Num=Act->Num;
  // If only one element.
  if(Act==Act->next) { free(Act); Act=NULL; } 
  else { 
    while(Pos->next!=Act) Pos=Pos->next; // Else get Position before Act
    Pos->next=Act->next; free(Act); Act = Pos->next; } // Delete Act.
  Stampa("D"); // Debug.
  return Num;
}

int Stampa(char *s) {
  struct process *Pos=Act;
  if(Empty()) return -1; 
  printf("%s:",s);
  do { printf("%d ",Pos->Num); Pos=Pos->next; } while(Pos!=Act);
  printf("\n");
}

main() {
  Insert(15); Insert(7); Insert(3); Next(); Insert(9); 
  Next(); Done(); Next(); Insert(12); Done();
}


