BFS(int st, int n)
{
	 int p,q,r,i,j;
	 intialise(n);

	 enque(st-1);
	 time[st-1]=0;
	 par[st-1]=-1;
	 chk[st-1]=1;
	 
	 while(front<=rear)
	 {
	 	printf("\nworking");
	 	p=deque();
	 	printf(" %d ",p+1);
	 	//tim++;
	 	for(i=0;i<n;i++)
	 	{
	 		if(a[p][i]==1 && chk[i]==0)
	 		{
	 			enque(i);
	 			par[i]=p+1;
	 			time[i]=time[p]+1;
	 			chk[i]=1;
	 		}
	 	}	
	 }
}
Example #2
0
int bfs(int s,int a, struct vertex *adj) {
    int i, u, j, l, v, w, k;

    struct queue *queue = malloc(sizeof(struct queue));
    struct queue *queueHyphen = malloc(sizeof(struct queue));
    queueInit(queue);
    queueInit(queueHyphen);
#pragma omp parallel for shared(adj)
    for (i = 0; i < adj[s].indeg; i++) { //par begin //compare ok
        u = adj[s].ins[i].u;
        j = adj[s].ins[i].iuout;
        eliminate(adj, u, j);                       //compare ok
    } //par end                                     //compare ok
    l = 0;                                          //compare ok
    adj[s].traversal = a;                           //compare ok
    adj[s].distance = l;                            //compare ok //before was adj[s].distance = l //compare with php
    struct queueNode *queueNode1 = malloc(sizeof(struct queueNode));
    enque(s, queue, queueNode1);                               //todo
//    printf("l74:queue -- should've queued %i\n", s);
    queueReset(queueHyphen);                        //todo
//    printf("l76:queueHypen -- should've reset");
    while (true) {  //repeat 1 begin
        l = l + 1;                                  //compare ok
        while (true) {  //repeat 2 begin
            u = deque(queue);                      //todo
//            printf("l81:queue -- should've dequeued %i\n", u);
            while (adj[u].first < adj[u].outdeg) {  //compare ok
                i = adj[u].first;                   //compare ok
                v = adj[u].out[i];                  //compare ok
#pragma omp parallel for shared(adj)
                for (j = 0; j < adj[v].indeg; j++) {  //par begin //compare ok
                    w = adj[v].ins[j].u;
                    k = adj[v].ins[j].iuout;
                    eliminate(adj, w, k);           //compare ok
                } //par end                         //compare ok
                a = a + 1;                          //compare ok
                adj[v].traversal = a;               //compare ok
                adj[v].distance = l;                //compare ok
                adj[v].parent = u;                  //compare ok
//                printf(":%i parent of %i\n", u, v);  //comment me
                struct queueNode *queueNode2 = malloc(sizeof(struct queueNode));
                enque(v, queueHyphen, queueNode2);              //todo
//                printf("l103:queueHyphen -- should've queued %i\n", v);
            } //end while                           //compare ok
            if (isQueueEmpty(queue)) {  //until
                break;
            }
        } //repeat 2 end                            //compare ok
        *queue = *queueHyphen;
        queueInit(queueHyphen);
//        printf("l121:queue = queueHyphen\n");
        if (isQueueEmpty(queue)) {
            break;
        }
    } //end repeat 1                                //compare ok
    return EXIT_SUCCESS;
}
Example #3
0
File: test.c Project: shixv/test
int main(void)
{
	Queue* q=initQueue();
	enque(q,1);
	enque(q,2);
	printf("%d,",head(q));
	deque(q);
	printf("%d,",head(q));
	freeQueue(q);
	return 0;
}
Example #4
0
 void bfs(vector<vector<char>>& g)
 {
     while (!que.empty())
     {
         int tmp = que.front();
         que.pop();
         int x = tmp / m;
         int y = tmp % m;
         enque(x-1,y,g);
         enque(x+1,y,g);
         enque(x,y-1,g);
         enque(x,y+1,g);
     }
 }
int main()
{
	int n1;
	scanf("%d", &n1);
	int sq[n1];
	
	int n2, n, front, rear, i, c;
	front = rear = -1;
	scanf("%d", &n2);
	char s[100];
	for(i = 0; i < n2; i++) {
		scanf("%s", s);
		if (strcmp(s,"enque") == 0) {
			getchar();
			getchar();
			scanf("%d",&n);
			enque(sq,n,n1,&front,&rear);
		} else if (strcmp(s,"deque") == 0) {
			getchar();
			getchar();
			deque(sq,n1,&front,&rear);
		} else {
			c = size(n1,&front,&rear);
			printf("%d\n", c);
		}
	}
	return 0;
}
Example #6
0
void relay_data(struct params *p, struct ieee80211_frame *wh, int len)
{
	char seq[2];
	char fc[2];
	unsigned short *ps;

	/* copy crap */
	memcpy(fc, wh->i_fc, 2);
	memcpy(seq, wh->i_seq, 2);

	/* relay frame */
	wh->i_fc[1] &= ~(IEEE80211_FC1_DIR_TODS | IEEE80211_FC1_RETRY);
	wh->i_fc[1] |= IEEE80211_FC1_DIR_FROMDS;
	memcpy(wh->i_addr1, wh->i_addr3, sizeof(wh->i_addr1));
	memcpy(wh->i_addr3, wh->i_addr2, sizeof(wh->i_addr3));
	memcpy(wh->i_addr2, p->mac, sizeof(wh->i_addr2));
        ps = (unsigned short*)wh->i_seq;
        *ps = seqfn(p->seq, 0);
	
	send_frame(p, wh, len);
	enque(p, wh, len);

	/* restore */
	memcpy(wh->i_fc, fc, sizeof(fc));
	memcpy(wh->i_addr2, wh->i_addr3, sizeof(wh->i_addr2));
	memcpy(wh->i_addr3, wh->i_addr1, sizeof(wh->i_addr2));
	memcpy(wh->i_addr1, p->mac, sizeof(wh->i_addr1));
	memcpy(wh->i_seq, seq, sizeof(seq));
}
Example #7
0
push()
{
	int ch;
	printf("\nEnter value ");
	scanf("%d",&ch);
	enque(ch,head);		
}
void reference_page(Queue *q, Hash *h, unsigned int page_no)
{
	if(q == NULL || h == NULL || page_no > h->capacity)
		return;
		
	//node corresponding to the requested page 
	QNode *req_page = h->array[page_no];
	//requested page is not in cache, so bring it in
	if(req_page ==  NULL)
      enque(q, h, page_no);
    //if requested page is not the recent or front node
    else if(req_page != q->front)
	{
		//detach the page
		req_page->prev->next = req_page->next;
		if(req_page->next)
			req_page->next->prev = req_page->prev;
		
		//if the requested page is in the end, 
		//change the rear pointer of the queue
		if(req_page == q->rear)
		{
			q->rear = req_page->prev;
			q->rear->next = NULL;
		}
		//bring the page to the front
		req_page->next = q->front;
		req_page->prev = NULL;
		//change prev of current front
		q->front->prev = req_page;
		//change the front
		q->front = req_page;
	}
	//else nothing to do as node is in front
}
int main()
{
	int n1;
	scanf("%d", &n1);
	int sq[n1];
	
	int n2, n, front, rear, i;
	front = rear = -1;
	scanf("%d", &n2);
	char s[100];
	for(i = 0; i < n2; i++) {
		scanf("%s", s);
		if (strcmp(s,"enque") == 0) {
			getchar();
			getchar();
			scanf("%d",&n);
			enque(sq,n,n1,&front,&rear);
		} else if (strcmp(s,"deque") == 0) {
			getchar();
			getchar();
			deque(sq,&front,&rear);
		} else if (s[0] == 's' || s[0] == 'S') {
			size(&front,&rear);
		} else if (s[0] == 'f' || s[0] == 'F') {
			front_element(sq,&front,&rear);
		} else {
			rear_element(sq,&front,&rear);
		}
	}
	return 0;
}
Example #10
0
static void* reconn_fun(void* arg){
		conn_t* conn;
		queue_t* q = &g_ctx.reconn_q;
		rval_t rval;
		int  times = 1;
		while(1){
				conn = list_entry(deque(q), conn_t,  reconn_link);
				assert(conn->conn_state == CONNECTING);
				assert(conn->conn_type == ACTIVE);
				debug_info("start reconn conn[%d], fd[%d],%s \n",
						   conn->id, conn->sockfd, dump_addr(conn->addr));
				rval =  re_connect(conn);
				if(success(rval)){
					debug_info("reconn conn[%d], fd[%d] success! \n", conn->id, conn->sockfd);
					set_conn_state(conn, CONNECTED);
					add_event(conn);

				}else{
						debug_warn("reconn conn[%d], fd[%d] failed! errno[%d]\n",
								conn->id, conn->sockfd, rval.err);
						enque(q, &conn->reconn_link); //continue reconn
				}
				sleep(times);
		}
}
Example #11
0
void JvDecoder::decode(const JpegFrame* p)
{
	/*XXX need to handle p->type changes too!*/
	int w = p->width_;
	int h = p->height_;
	if (p->q_ != inq_ || w != inw_ || h != inh_)
		reset(p->q_, w, h);

	/*
	 * Copy packet data into a dma buffer.  If we had a more
	 * intelligent buffering scheme, we could make sure
	 * reassembly occured in-place.  But copying a compressed
	 * frame an extra time is noise compared to copying
	 * the uncompressed data which happens at least once
	 * (to the frame buffer).
	 */
	memcpy((char*)pi_->db.bp, (char*)p->bp_, p->len_);
	pi_->len = p->len_;
	/*XXX this is a gross test condition */
	if (pf_ != 0) {
		enque(pi_);
		pi_ = pf_;
		pf_ = pf_->next;
	}
}
Example #12
0
int
binary_semaphore_down(int s_id)
{
	acquire(&list_lock);
	if(!is_legal_id(s_id) || !is_semaphore_init(s_id))
		return -1;
	
	struct binary_semaphore *s = &bsemaphores[s_id];
	
	acquire(&s->lock);
	
	if(s->value <= 0){
		// Already locked
		if(enque(proc, &s->waiting)<0){
			// enque fails
			release(&s->lock);
			release(&list_lock);
			//cprintf("[err %d %d]", proc->pid, s_id);
			return -1;
		}

		//release(&s->lock);
		//cprintf("[sleep %d %d]", proc->pid, s_id);
		release(&list_lock);
		sleep(proc, &s->lock);
		release(&s->lock);
		return 0;
	}
	//cprintf("[lock %d %d]", proc->pid, s_id);
	s->value=0;
	release(&s->lock);
	release(&list_lock);
	return 0;
}
Example #13
0
int main(int argc, char const *argv[]) {

    create();
    enque(5);


	return 0;
}
Example #14
0
int main(int argc, char* argv[])
{
	Node tail1 = LSNodeCreate(1);
	tail1 = enque(tail1, 2);
	tail1 = enque(tail1, 3);
	tail1 = enque(tail1, 4);
	tail1 = enque(tail1, 5);
	tail1 = enque(tail1, 6);
	tail1 = enque(tail1, 7);
	tail1 = enque(tail1, 8);

	Node tail2 = NULL; 

	// struct dequeueOutput dOutput = dequeue(tail1, tail2);
	// printf("%d\n", dOutput.popedValue);
	// dOutput = dequeue(dOutput.tail1, dOutput.tail2);
	// printf("%d\n", dOutput.popedValue);

	struct dequeueOutput temp;
	temp = dequeue(tail1, tail2);
	while(temp.tail2 != NULL)
	{
		temp = dequeue(temp.tail1, temp.tail2);
		printf("%d\n", temp.popedValue);
	}




	return 0;
} 
Example #15
0
void main()
{
  int ch=0,k,l;
	while(ch!=6)
	  {
	    printf("choose your option..!\n");
	    while((q!=1)&&(q!=2))
		{
		  printf("1.Input Restricted\t2.Output Restricted\n");
		  scanf("%d",&q);
		  if((q!=1)&&(q!=2))
		    printf("Wrong option\n");
		}
	    printf("1.Enqueue\t2.Dequeue\t3.Display\t4.Search\t5.Change the type\t6.Exit\n");
	    scanf("%d",&ch);
		switch(ch)
		  {
		  case 1:
		    printf("Enter the element to be queued\n");
		    scanf("%d",&k);
		    enque(k);
		    break;
		  case 2:
		    k=deque();
		    if(flag==0)
		      printf("Element %d is removed from the queue...!\n",k);
		    break;
		  case 3:
		    if((front==-1)||(front>rear))
		      printf("Nothing to display!!");
		    else
		      {
			printf("Elements in the queue are\n");
			display();
		      }
		    printf("\n");
		    break;
		  case 4:
		    printf("Enter the element to searched\n");
		    scanf("%d",&k);
		    l=search(k);
		    if(l==0)
		      printf("%d is not present in the queue\n",k);
		    else
		      printf("%d is present at the position %d\n",k,l);
		    break;
		  case 5:
		    printf("1.Input Restricted\t2.Output Restricted\n");
		    scanf("%d",&q);
		    break;
		  case 6:
		    break;
		  default:
		    printf("You've entered wrong option, please try again.........!\n");
		}
	  }
}
Example #16
0
void cond_variable_wait(cond_variable * c, mutex * m)
{
	/* add thread to cond_variable queue */
	enque(c->q, getPid());
	/* sleep thread and release lock atomically */

	release_lock_and_sleep(m);
	/* re-aquire lock */
	mutex_lock(m);
}
Example #17
0
/*
 *  round robin schedule
*/
int schedule(tpool_t* tp,task_t* task){
	
	int thread_id;
	conn_t* conn =(conn_t*)task->arg;

	thread_id = conn->id % tp->num;
	
	debug_info("schedule task[%lu], on thread[%d]\n", task->id, tp->threads[thread_id]->myid);
	enque(&tp->threads[thread_id]->q, &task->link); 

}
Example #18
0
/* run evaluator */
void
run(void)
{
    Task	*t;

    /* empty task queue */
    if (taskq == NULL)
	return;

    inrun = 1;

    /* initialize task scheduling */
    t = taskq;
    while (t) {
	t->eval = t->epoch = start;
	t->retry = 0;
	t->tick = 0;
	t = t->next;
    }

    /* evaluate and reschedule */
    t = taskq;
    for (;;) {
	if (t->retry && t->retry < t->eval) {
	    now = t->retry;
	    if (now > stop)
		break;
	    sleepTight(t, SLEEP_RETRY);
	    enable(t);
	    t->retry = waiting(t) ? now + RETRY : 0;
	}
	else {
	    now = t->eval;
	    if (now > stop)
		break;
	    reflectTime(t->delta);
	    sleepTight(t, SLEEP_EVAL);
	    eval(t);
	    t->tick++;
	    t->eval = t->epoch + t->tick * t->delta;
	    if ((! t->retry) && waiting(t))
		t->retry = now + RETRY;
	}
	taskq = t->next;
	if (taskq) taskq->prev = NULL;
	enque(t);
	t = taskq;
    }

    if (!quiet)
	__pmNotifyErr(LOG_INFO, "evaluator exiting\n");
}
Example #19
0
int main() {
  DEBUG_PRINT("creating QUEUE");
  UIntQueue Q;
  DEBUG_PRINT("QUEUE created!");

  for (uint32_t rounds = 0; rounds < ROUNDS; ++rounds) {
    enque(Q);
    random_access(Q);
    deque(Q);
  }

  return 0;
}
Example #20
0
void addAdjacentNonVisitedVertexToQue(int v)
{
   struct node *cur = graph[v];

   while(cur!=0)
   {
      if(visited[cur->v]==0)
      {
         enque(cur->v);
      }
      cur=cur->next;
   }
}
Example #21
0
// byte recieved
static void byteReceived(void)
{
    if(length(&recvBuffer) < (SERIALBUFFER - 1))
    {
        // put byte in recv buffer
        enque(&recvBuffer, LINDAT);
    }
    else
    {
        // buffer full, discard
        LINSIR |= (1 << LRXOK);
    }
}
Example #22
0
 void solve(vector<vector<char>>& g) 
 {
     n = g.size();
     if (n == 0) return ;
     m = g[0].size();
     if (m == 0) return ;
     while (!que.empty()) que.pop();
     for (int i=0;i<m;++i) enque(0,i,g);
     for (int i=0;i<m;++i) enque(n-1,i,g);
     for (int i=0;i<n;++i) enque(i,0,g);
     for (int i=0;i<n;++i) enque(i,m-1,g);
     
     bfs(g);
     
     for (int i=0;i<n;++i)
         for (int j=0;j<m;++j)
             if (g[i][j] == 'O') g[i][j] = 'X';
     
     for (int i=0;i<n;++i)
         for (int j=0;j<m;++j)
             if (g[i][j] == '#') g[i][j] = 'O';
 }
Example #23
0
// send byte
static void sendRaw(uint8_t byte)
{
    if(length(&sendBuffer) < (SERIALBUFFER - 1))
    {
        // put byte in send buffer, enable sending
        enque(&sendBuffer, byte);
        ATOMIC_BLOCK(ATOMIC_FORCEON)
        {
            if(sending == 0)
            {
                readyToSend();
            }
        }
    }
Example #24
0
pop()
{
	int tem,temp=0;
	temp=deque(head);
	while((tem=deque(head))!=-1)
	{
		swapQ();
		enque(temp,head);
		swapQ();
		temp=tem;		
	}
	swapQ();
	return temp;
}
Example #25
0
int main()
{
    s->count=0;//initilize number of elem in the list
    pf=fopen("input.dat","r");
      if (pf==NULL)
    {
        perror("No students!");
        return -100;
    }
    ps=fopen("output.dat","w");
    //read from file
    int period[100];//array to store the periods
    char line[200];
    fseek(pf,0,SEEK_SET);
    char c;
    int number,n=0;
    fscanf(pf,"%d%c",&number,&c);
    while (c==' ')
    {
        n++;
        period[n]=number;
        fscanf(pf,"%d%c",&number,&c);
    }
    n=n+1;
    period[n]=number;//array for periods of length n
    while (fgets(line,sizeof(line),pf)!=NULL)
    {
        //read the command from the line
        char name[15];
        int money,seconds;
        sscanf(line,"%s %d %d",&name,&money,&seconds);
        enque(name,money,seconds);
    }
    int i;
    for (i=1;i<=n;i++)
    {
        int sum=0;int currentTime=0;
        QueT *p=s->head;
        while (currentTime<period[i])
        {
            sum=sum+p->ruble;
            currentTime=currentTime+p->time;
            p=p->next;
        }
        fprintf(ps,"After %d seconds: %d",period[i],sum);
    }
    fclose(ps);
    return 0;
}
Example #26
0
void doBFS(int v)
{
   enque(v);

   while(isQueEmpty() == 0)
   {
      int k = deque();

      if(visited[k] == 0)
      {
         printf("BFS visited %d \n",k);
         visited[k] = 1;
         addAdjacentNonVisitedVertexToQue(k);
      }
   }
}
Example #27
0
void read_wifi(struct params *p)
{
	static char *buf = 0;
	static int buflen = 4096;
	struct ieee80211_frame *wh;
	int rc;

	if (!buf) {
		buf = (char*) malloc(buflen);
		if (!buf)
			err(1, "malloc()");
	}
	
	rc = sniff(p->rx, buf, buflen);
	if (rc == -1)
		err(1, "sniff()");

	wh = get_wifi(buf, &rc);
	if (!wh)
		return;

	/* relayed macast */
	if (frame_type(wh, IEEE80211_FC0_TYPE_DATA,
		       IEEE80211_FC0_SUBTYPE_DATA) &&
	    (wh->i_fc[1] & IEEE80211_FC1_DIR_FROMDS) &&
	    (memcmp(wh->i_addr2, p->ap, 6) == 0) &&
	    (memcmp(wh->i_addr1, p->mcast, 5) == 0) &&
	    (memcmp(p->mac, wh->i_addr3, 6) == 0)) {
		got_mcast(p, wh, rc);
		return;
	}

	/* data */
	if (frame_type(wh, IEEE80211_FC0_TYPE_DATA,
		       IEEE80211_FC0_SUBTYPE_DATA)) {
		if (!wanted(p, wh, rc))
			return;
		
		enque(p, &buf, wh, rc);
		if (p->state == S_START)
			send_queue(p);
		return;
	}
}
int main(void)
{
	board_init();												//Initialize the circuit board configurations
  	changeState(START);											//Initialize the state
  	enque(DIAGNOSTIC);											//first do diagnostic
  	//motorsInit();
  	//motorDir(CLOCKWISE);
  	
  	#ifdef SHOW_MESSAGES
	    hal_sendString_UART1("Machine started");
	    hal_sendChar_UART1('\n');
    #endif
  	
  	//INTEnableInterrupts();
  	//INTEnableSystemMultiVectoredInt();
  	
  	
  	SKPIC32_Init();
	SKPIC32_GLCDInit();
	SKPIC32_GLCDWriteText(0, 0, "Hello World!");
	SKPIC32_GLCDWriteText(0, 1, "This is SKPIC32!");
	SKPIC32_GLCDWriteText(0, 2, "1234567890");
	SKPIC32_GLCDWrite(0, 3, 0x31);
	DelayMs(2000);
	

  	while(1){
    	uint8 nextEvent = deque();								//get next event
    	if(nextEvent)
	    {
     		stateMachine(nextEvent);							//enter the event functions
    	}
  
    	if(keyuse ==(uint8)PRODUCT)								//check key press for product selection
     	{	    
     			keypad_pole();
     	}
     	else if(keyuse ==(uint8)AMOUNT)							//check key press for amount selection
     	{
     			keypad_pole();	
     	}	
  	}	
  	
}
Example #29
0
int add_reconn(conn_t* conn){
	
	int sockfd = conn->sockfd;
	int need = 0;
	int original_state ;
	int rc;
	
	pthread_mutex_lock(&conn->lock);
	original_state = conn->conn_state;
	if(conn->conn_state == CONNECTED){
		debug_info("closed sockfd[%d]\n", sockfd);
		rc = close(sockfd);
		check(rc);
		/*
		if(conn->conn_type == ACCEPT){
			rc = close(sockfd);
			check(rc);
		}else{
			rc =shutdown(sockfd,SHUT_RDWR);
			check(rc);
		}*/
		need = 1;
	}else if(conn->conn_state == CLOSED){  //when active connect failed
		need = 1;	
	}//other COONECING

	conn->conn_state = CONNECTING;
	pthread_mutex_unlock(&conn->lock);

	assert(conn->conn_state == CONNECTING);
	//
	del_event(conn);

	
	if(need && conn->conn_type == ACTIVE){
		debug_info("add to reconn queue, conn->conn_type[%d], original state[%d]\n",
					conn->conn_type, original_state);
		enque(&g_ctx.reconn_q, &conn->reconn_link);
	}else{		//accept connect, do nothing ,but wait for be connected
		debug_info("accept connect do nothing, conn->conn_type[%d], original state[%d]\n",
					conn->conn_type, original_state);
	}
}
double main( void )
{
	QueueNodePtr header = NULL, tail = NULL; // declare two pointer of Queue type

	double input;

	printf( "Enter a set of number, stop at -999\n" );

	while( scanf( "%lf", &input ) && input != -999 ) // scan an integer input while input not equal -999
	{
		enque( &header, &tail, input );
	}

	printf( "Output of the queue:\n" );

	while( header != NULL )
		printf( "%lf\n", deque( &header, &tail ) ); // output queue


	return 0;
}