Example #1
0
Stack* stack_pop(Stack* st)
{
    if (llist_size(st->list)!=0)
    {
        st->list=llist_remove_first(st->list);
        return (st);
    }
    else
        return(st);

}
LList* llist_remove_last( LList* lst )
{
    Node* ptr;
    ptr = lst->head;
    if(llist_size(lst) == 1) return llist_remove_first(lst);
    else
    {
		while((ptr->next)->next!=NULL)
		{
			ptr = ptr->next;
		}
		ptr->next = NULL;
	}
    return lst;
}
Example #3
0
void evdev_post_event(dev_t device, struct input_event event)
{
	evdev_event_t *ev;
	evdev_device_t *dev = evdev_get(device);
	assert(dev != NULL);
	ev = heapmm_alloc(sizeof(evdev_event_t));
	ev->event = event;
	ev->event.time.tv_sec = (time_t) system_time;
	ev->event.time.tv_usec = (time_t) system_time_micros;
	llist_add_end(&(dev->queue), (llist_t *) ev);
	dev->queue_count++;
	if (dev->queue_count == CONFIG_EVDEV_QUEUE_SIZE) {
		ev = (evdev_event_t *)llist_remove_first(&(dev->queue));
		heapmm_free(ev, sizeof(evdev_event_t));
		dev->queue_count--;
	}
	semaphore_up(&(dev->event_wait));
}
LList* llist_remove( LList* lst, int idx )
{
    Node* ptr;
    Node* ptr_idx_minus_one;
    int i;
    ptr = lst->head;
    if(idx==0) llist_remove_first(lst);
    else if(idx==1) 
    {
		(lst->head)->next = (ptr->next)->next;
	}
    else
    {
		for(i=0;i<idx-1;i++)
		{
			ptr = ptr->next;
		}
		ptr_idx_minus_one = ptr;
		ptr = ptr->next;
		ptr = ptr->next;
		ptr_idx_minus_one->next = ptr;
		return lst;
	}
}
Example #5
0
int play_TOH(LList* lst_1, LList* lst_2 ,LList* lst_3){
    int num_steps=0;
    int lst_1_size,lst_2_size;
    int k=llist_size(lst_1);
    int p=llist_size(lst_3);
    printf("\nTransfer\tpeg1\tpeg2\tpeg3\n");
    if(k%2!=0){
    while(p!=k)
    {
       switch(num_steps%3)
       {
       
      case 0:
             if(lst_3->head==NULL || lst_1->head->data < lst_3->head->data)
             {
                 lst_3=llist_prepend(lst_3,lst_1->head->data);
                 lst_1=llist_remove_first(lst_1);
                 printf("1 -> 3");
             }
             else 
              {
                 lst_1=llist_prepend(lst_1,lst_3->head->data);
                 lst_3=llist_remove_first(lst_3);
                 printf("3 -> 1");
             }
             num_steps++;
             break;
             
     case 1:
           if(lst_2->head==NULL || lst_1->head->data < lst_2->head->data)
             {
                 lst_2=llist_prepend(lst_2,lst_1->head->data);
                 lst_1=llist_remove_first(lst_1);
                 printf("1 -> 2");
             }else{
             if(lst_1->head->data==1000)
             lst_1=llist_remove_first(lst_1);
                lst_1=llist_prepend(lst_1,lst_2->head->data);
                 lst_2=llist_remove_first(lst_2);
                 printf("2 -> 1");
             }
             num_steps++;
             break;
     
     case 2:
           if(lst_3->head==NULL || lst_2->head->data < lst_3->head->data)
             {
                 lst_3=llist_prepend(lst_3,lst_2->head->data);
                 lst_2=llist_remove_first(lst_2);
                 printf("2 -> 3");
             }
             else 
              {
                 lst_2=llist_prepend(lst_2,lst_3->head->data);
                 lst_3=llist_remove_first(lst_3);
                 printf("3 -> 2");
             }
             num_steps++;
             break;
          default :
              break;
       }
       if(lst_1->head==NULL)
       lst_1=llist_prepend(lst_1,1000);
       p=llist_size(lst_3);
       lst_1_size=llist_size(lst_1);
       lst_2_size=llist_size(lst_2);
      if(lst_1->head->data==1000)
       lst_1_size-=1;
       printf("\t\t %d\t%d\t%d\n",lst_1_size,lst_2_size,p);
    }
}else{
      while(p!=k)
      {
       switch(num_steps%3)
       {
       case 0:
             if(lst_2->head==NULL || lst_1->head->data < lst_2->head->data)
             {
                 lst_2=llist_prepend(lst_2,lst_1->head->data);
                 lst_1=llist_remove_first(lst_1);
                 printf("1 -> 2");
             }
             else
              {
               if(lst_1->head->data==1000)
               lst_1=llist_remove_first(lst_1);
                 lst_1=llist_prepend(lst_1,lst_2->head->data);
                 lst_2=llist_remove_first(lst_2);
                 printf("2 -> 1");
             }
             num_steps++;
             break;
             
     case 1:
           if(lst_3->head==NULL || lst_1->head->data < lst_3->head->data)
             {
                 lst_3=llist_prepend(lst_3,lst_1->head->data);
                 lst_1=llist_remove_first(lst_1);
                 printf("1 -> 3");
             }
             else
              {
                 lst_1=llist_prepend(lst_1,lst_3->head->data);
                 lst_3=llist_remove_first(lst_3);
                 printf("3 -> 1");
             }
             num_steps++;
             break;
     
     case 2:
           if(lst_3->head==NULL || lst_2->head->data < lst_3->head->data)
             {
                 lst_3=llist_prepend(lst_3,lst_2->head->data);
                 lst_2=llist_remove_first(lst_2);
                 printf("2 -> 3");
             }
             else
              {
                 lst_2=llist_prepend(lst_2,lst_3->head->data);
                 lst_3=llist_remove_first(lst_3);
                 printf("3 -> 2");
             }
             num_steps++;
             break;
        default:
             break;  
       }
        if(lst_1->head==NULL)
       lst_1=llist_prepend(lst_1,1000);
       p=llist_size(lst_3);
       lst_1_size=llist_size(lst_1);
       lst_2_size=llist_size(lst_2);
       if(lst_1->head->data==1000)
       lst_1_size=-1;
       printf("\t\t %d\t%d\t%d\n",lst_1_size,lst_2_size,p);
       
     }
  }
  return num_steps;
}