コード例 #1
0
int checkStackPairwiseOrder(struct sNode *top) {
    struct Queue *q = createQueue();
    struct sNode *s = top;
    
    int pairwiseOrdered = 1;

    while (!isEmpty(s))
        enQueue(q, pop(&s));

    while (!queueIsEmpty(q))
        push(&s, deQueue(q)->key);

    while (!isEmpty(s)) {
        int n = pop(&s);
        enQueue(q, n);
        if (!isEmpty(s)) {
            int m  = pop(&s);
            enQueue(q, m);
            if(abs(n - m) != 1) {
                pairwiseOrdered = 0;
            }    
        }
    }    

    while (!queueIsEmpty(q))
        push(&s, deQueue(q)->key);
    return pairwiseOrdered;
}        
コード例 #2
0
int main(void){
    
    Queue_t *q;
    Stack_t *stack1 = (Stack_t*)malloc(sizeof(Stack_t));
    Stack_t *stack2 = (Stack_t*)malloc(sizeof(Stack_t));
    
    q->basic = stack1;
    q->forDequeue = stack2;
    
    puts("//enqueue");
    enQueue(q, 1);
    enQueue(q, 2);
    enQueue(q, 3);
    
    printQueue(q);
    
    puts("//deQueue");
    deQueue(q);
    deQueue(q);
    printQueue(q);
    
    deQueue(q);
    printQueue(q);
    
    deQueue(q);
    
    
    
    
    return 0;
    
}
コード例 #3
0
ファイル: queue.c プロジェクト: king63499/queue
void main(){
	sQueue q;
	init(&q);
	
	enQueue(&q,5);
	deQueue(&q);
	enQueue(&q,71);
	deQueue(&q);
	enQueue(&q,72);
	deQueue(&q);
	enQueue(&q,73);
	enQueue(&q,74);
	deQueue(&q);
	enQueue(&q,75);
	enQueue(&q,76);
	enQueue(&q,77);
	deQueue(&q);
	enQueue(&q,78);
	deQueue(&q);
	deQueue(&q);
	deQueue(&q);
	deQueue(&q);
	deQueue(&q);
	
}
コード例 #4
0
ファイル: Queue.c プロジェクト: AbdulmalikGadeer/C-Language
int main(void){

	printf("FIFO: First In First Out\n");

	printf("Queue is %s\n", (isEmpty()? "Empty": "Not Empty"));
	printf("Queue Size = %d\n", queueSize());

	enQueue(1);
	enQueue(2);
	enQueue(13);
	enQueue(6);

	printQueue();
	printf("Queue Size = %d\n", queueSize());


	printf("1st DeQueue = 1 || deQueue Result = %d\n", deQueue());
	printf("2nd DeQueue = 2 || deQueue Result = %d\n", deQueue());
	printf("Queue Size = %d\n", queueSize());

	printQueue();

	printf("3rd DeQueue = 13 || deQueue Result = %d\n", deQueue());
	printf("4th DeQueue = 6  || deQueue Result = %d\n", deQueue());

	printf("Add 6 to the queue\n");
	enQueue(6);
	printQueue();

	printf("Queue Size = %d\n", queueSize());
	printf("Queue is %s\n", (isEmpty()? "Empty": "Not Empty"));


	return 0;
}
コード例 #5
0
ファイル: pQueueTest.c プロジェクト: shwetado/DSA
void test_inserts_the_element_at_the_starting_of_queue(){
	void* pQueue = createPQueue();
	int data = 40;
	ASSERT(1 == enQueue(pQueue, &data, 5));
	ASSERT(1 == deQueue(pQueue));
	ASSERT(0 == deQueue(pQueue));
};
コード例 #6
0
ファイル: pQueueTest.c プロジェクト: shwetado/DSA
void test_inserts_the_element_first_having_higher_priority_float(){
	void* pQueue = createPQueue();
	float data1 = 10.0f , data2 = 20.0f;
	ASSERT(1 == enQueue(pQueue, &data2, 2));
	ASSERT(1 == enQueue(pQueue, &data1, 1));
	ASSERT(1 == deQueue(pQueue));
	ASSERT(1 == deQueue(pQueue));
	ASSERT(0 == deQueue(pQueue));
};
コード例 #7
0
ファイル: pQueueTest.c プロジェクト: shwetado/DSA
void test_inserts_the_element_first_having_lower_priority_double(){
	void* pQueue = createPQueue();
	double data1 = 10.0 , data2 = 20.0;
	ASSERT(1 == enQueue(pQueue, &data1, 1));
	ASSERT(1 == enQueue(pQueue, &data2, 2));
	ASSERT(1 == deQueue(pQueue));
	ASSERT(1 == deQueue(pQueue));
	ASSERT(0 == deQueue(pQueue));
};
コード例 #8
0
ファイル: pQueueTest.c プロジェクト: shwetado/DSA
void test_inserts_the_element_first_having_lower_priority_char(){
	void* pQueue = createPQueue();
	char data1 = 'a' , data2 = 'b';
	ASSERT(1 == enQueue(pQueue, &data1, 1));
	ASSERT(1 == enQueue(pQueue, &data2, 2));
	ASSERT(1 == deQueue(pQueue));
	ASSERT(1 == deQueue(pQueue));
	ASSERT(0 == deQueue(pQueue));
};
コード例 #9
0
ファイル: pQueueTest.c プロジェクト: shwetado/DSA
void test_inserts_the_element_first_having_higher_priority_String(){
	void* pQueue = createPQueue();
	String data1 = "a" , data2 = "b";
	ASSERT(1 == enQueue(pQueue, &data2, 2));
	ASSERT(1 == enQueue(pQueue, &data1, 1));
	ASSERT(1 == deQueue(pQueue));
	ASSERT(1 == deQueue(pQueue));
	ASSERT(0 == deQueue(pQueue));
};
コード例 #10
0
void test_to_dequeue_an_int_element(){
	int element = 4;
	queue = create(sizeof(int), 3);
	ASSERT(enQueue(queue, &element));
	ASSERT(enQueue(queue, &element));
	deQueue(queue);
	deQueue(queue);
	ASSERT(1==queue->queueInfo.front);
}
コード例 #11
0
ファイル: circularQueueTest.c プロジェクト: pallavig/dsa
void test_to_check_behaviour_when_deleting_all_elements(){
	char exp_arr[] = {'a','b'};
	CircularQueue expected = {exp_arr,-1,-1,2,sizeof(char)};
	CircularQueue* actual = create(sizeof(char),2);
	char element1 = 'a';
	char element2 = 'b';
	char* firstElement;
	int res;
	res = enQueue(actual,&element1);
	res = enQueue(actual,&element2);
	firstElement = deQueue(actual);
	firstElement = deQueue(actual);
	ASSERT(compareQueue(actual,&expected));
};
コード例 #12
0
int main()
{
    struct Queue *q = createQueue();
    enQueue(q, 10);
    enQueue(q, 20);
    deQueue(q);
    deQueue(q);
    enQueue(q, 30);
    enQueue(q, 40);
    enQueue(q, 50);
    struct QNode *n = deQueue(q);
    if (n != NULL)
        printf("Dequeued item is %d", n->key);
    return 0;
}
コード例 #13
0
ファイル: queueTest.c プロジェクト: clearlylin/Algorithm
int main(int argc, char const *argv[])
{
	Queue* queue = initQueue();
	bool empty = isEmpty(queue);
	if (empty)
		printf("queue is empty\n");
	else
		printf("queue is not empty, error\n");
	bool success = enQueue(queue, &arr[0]);
	if (success)
		printf("enQueue success\n");
	else
		printf("enQueue failed, error\n");
	int* ele;
	success = deQueue(queue, &ele);
	if (success)
		printf("%d, deQueue success", *ele);
	else
		printf("error\n");
	printf("isEmpty: %d\n", isEmpty(queue));
	printf("queueSize: %d\n", queueSize(queue));
	enQueue(queue, &arr[1]);
	enQueue(queue, &arr[2]);
	enQueue(queue, &arr[3]);
	enQueue(queue, &arr[4]);
	enQueue(queue, &arr[5]);
	enQueue(queue, &arr[6]);
	enQueue(queue, &arr[7]);
	enQueue(queue, &arr[8]);
	printf("isEmpty: %d\n", isEmpty(queue));
	printf("queueSize: %d\n", queueSize(queue));
	success = deQueue(queue, &ele);
	if (success)
		printf("%d, deQueue success\n", *ele);
	else
		printf("deQueue error\n");

	int count = 105;
	while (count > 0) {
		if (enQueue(queue, &arr[9]))
			--count;
		else
			break;
	}
	printf("count: %d\n", count);
	printf("queueSize: %d\n", queueSize(queue));
	return 0;
}
コード例 #14
0
ファイル: queueLibTest.c プロジェクト: jainsahab/DSA
void test_to_dequeue_an_int_element_when_it_is_already_empty(){
	void *dequeuedElement;
	Queue* queue = create(sizeof(int), 3);
	dequeuedElement = deQueue(queue); 
	free(queue->base);
	free(queue);	
}
コード例 #15
0
ファイル: bp2.c プロジェクト: hermesdragon/CODING
bool isBipartite(int G[][V], int src)
{
    int colorMatrix[V], color, temp, u;

    initColorMatrix(colorMatrix, V);

    struct Queue* queue = createQueue(V);

    color = 1;
    colorMatrix[src] = color;
    enQueue(queue, src);

    while (!isEmpty(queue))
    {
        temp = deQueue(queue);
        // assign alternate color to its neighbor
        color = 1 - colorMatrix[temp];

    	for (u = 0; u < V; ++u)
        {
        	// an edge exists and destination not colored
        	if (G[temp][u] && colorMatrix[u] == -1)
	        {
		        colorMatrix[u] = color;
                enQueue(queue, u);
	        }

            else if (G[temp][u] && colorMatrix[u] == colorMatrix[temp])
                return false;
        }
    }

    return true;
}
コード例 #16
0
int isPathBfs(Graph g, Vertex v, Vertex w)
{
	int isPath = FALSE;
	Queue q = newQueue(); // create a new queue
	Vertex curV = 0;
	int *visited = calloc(g->nV, sizeof(int)); // allocate + init to 0
	int i = 0;

	assert(visited != NULL);
	enQueue(q, v);
	visited[v] = TRUE;

	while ( !isEmptyQueue(q) ){ // still have vertices to traverse
		curV = deQueue(q); // get a vertex from the queue
		printf("Visiting: %d.\n", curV);
		visited[curV] = TRUE; // mark it as visited
		if (curV == w) { isPath = TRUE; } 
		for (i = 0; i < g->nV; i++){ // find vertices to add to queue
			if (g->edges[curV][i] && !visited[i]){ // found a vertex to add
				enQueue(q, i); // add it to queue
				visited[i] = TRUE; // mark it as visited
			}
		}
	}	
	free(visited);
	deleteQueue(q);
	return isPath;
}
コード例 #17
0
ファイル: Queue.cpp プロジェクト: yushiyaogg/backup
/****从队列中取出请求
*主队列,副队列交换了
*
**/
struct req_t* Queue::doRequest()
{
   struct req_t *req =NULL;
   //先从主队列中取
    deQueue(m_pLink,req); //它看到的永远是主队列
    return req;
}
コード例 #18
0
int main()
{
	int num, data, e;

	while(1)
	{
		system("cls"); //system clear
		printf("\n ## 큐구현: 링크드큐## \n\n");
		printf("1) 데이터삽입: enQueue \n");
		printf("2) 데이터삭제: deQueue \n");
		printf("3) 전체출력\n");
		printf("4) 프로그램종료\n\n");
		printf("메뉴선택: ");
		scanf("%d", &num);	
			
		switch(num) {
		case 1 : printf("\n 삽입할데이터입력: ");
		scanf("%d", &data); fflush(stdin); //delete memory garbage 
		enQueue(data);
		break;
		
		case 2 : deQueue(); break;
		
		case 3 : queue_Print(); break;
		
		case 4 : printf("프로그램종료... \n"); return 0;
		
		default : printf("잘못선택하셨습니다. \n"); fflush(stdin);
		} system("pause");
	} //while
} //main
コード例 #19
0
ファイル: os345.c プロジェクト: mukk88/cs345
// **********************************************************************
// **********************************************************************
// scheduler
//
static int scheduler()
{
	int nextTask;
	// ?? Design and implement a scheduler that will select the next highest
	// ?? priority ready task to pass to the system dispatcher.

	// ?? WARNING: You must NEVER call swapTask() from within this function
	// ?? or any function that it calls.  This is because swapping is
	// ?? handled entirely in the swapTask function, which, in turn, may
	// ?? call this function.  (ie. You would create an infinite loop.)

	// ?? Implement a round-robin, preemptive, prioritized scheduler.

	// ?? This code is simply a round-robin scheduler and is just to get
	// ?? you thinking about scheduling.  You must implement code to handle
	// ?? priorities, clean up dead tasks, and handle semaphores appropriately.

	// schedule next task

	nextTask = deQueue(&readyQueue,-1);
	if(nextTask >= 0){
		enQueue(&readyQueue, nextTask, tcb[nextTask].priority);
	}
	if (tcb[nextTask].signal & mySIGSTOP) return -1;

	return nextTask;
} // end scheduler
コード例 #20
0
// dequeue an item from queue
int deQueue(struct queue *q)
{
    int x, res;

    // both stacks are empty then error
    if (q->stack1 == NULL)
    {
        printf("Q is empty");
        exit(0);
    }
    else if (q->stack1->next == NULL)
    {
        return pop(&q->stack1);
    }
    else
    {
        // pop an item from the stack1
        x = pop(&q->stack1);

        // store the last dequeued item
        res = deQueue(q);

        // push everything back to stack1
        push(&q->stack1, x);
        return res;
    }
}
コード例 #21
0
ファイル: graph.c プロジェクト: fun4xnm/CLRS
void bfs(struct Graph *graph, int s)
{

    int visited[graph->v];
    for (int i = 0; i < graph->v; i++)
        visited[i] = G_FALSE;
    visited[s] = G_TRUE;
    
    struct Queue *queue = newQueue(10);
    enQueue(queue, s);
    
    fprintf(stderr, "BFS\n");
    while (!isEmpty(queue)) {
        int temp = deQueue(queue);
        fprintf(stderr, "%d\n", temp);
        struct adjListNode *cur = graph->array[temp].head;
        while (cur){
            if (!visited[cur->dest]) {
                visited[cur->dest] = G_TRUE;
                enQueue(queue,cur->dest);
            }
            cur = cur->next;
        }
    }
    return;
}
コード例 #22
0
ファイル: main.c プロジェクト: namannigam/zealous-codetroops
/* Function to dequeue an item from queue */
int deQueue(struct queue *q)
{
   int x, res;

   /* If both stacks are empty then error */
   if(q->stack1 == NULL)
   {
     printf("Q is empty");
     getchar();
     exit(0);
   }
   else if(q->stack1->next == NULL)
   {
      return pop(&q->stack1);
   }
   else
   {
      /* pop an item from the stack1 */
      x = pop(&q->stack1);

      /* store the last dequeued item */
      res = deQueue(q);

      /* push everything back to stack1 */
      push(&q->stack1, x);

      return res;
   }
}
コード例 #23
0
Node* Queue::creatHuffmanTree(){

	LNode* child1;
	LNode* child2;
	while(getCount() > 1){
		child1 = deQueue();
		child2 = deQueue();
		LNode* root = new LNode;
		root->node = new Node;
		root->node->left = child1->node; // node nho dat ben trai
		root->node->right= child2->node; // node lon dat ben phai
		root->node->weight = child1->node->weight + child2->node->weight;
		enQueue(root);
	}
	return deQueue()->node;
}
コード例 #24
0
ファイル: cache.c プロジェクト: gvanvari/CPP-Codes
// A function to add a page with given 'pageNumber' to both queue
// and hash
void Enqueue( Queue* queue, Hash* hash, unsigned pageNumber )
{
    // If all frames are full, remove the page at the rear
    if ( AreAllFramesFull ( queue ) )
    {
        // remove page from hash
        hash->array[ queue->rear->pageNumber ] = NULL;
        deQueue( queue );
    }

    // Create a new node with given page number,
    // And add the new node to the front of queue
    QNode* temp = newQNode( pageNumber );
    temp->next = queue->front;

    // If queue is empty, change both front and rear pointers
    if ( isQueueEmpty( queue ) )
        queue->rear = queue->front = temp;
    else  // Else change the front
    {
        queue->front->prev = temp;
        queue->front = temp;
    }

    // Add page entry to hash also
    hash->array[ pageNumber ] = temp;

    // increment number of full frames
    queue->count++;
}
コード例 #25
0
void test_to_check_the_behaviour_on_full_capacity_after_dequeing(){
	int element[] = {4,5,6,7,8,9};
	queue = create(sizeof(int), 4);
	ASSERT(enQueue(queue, element));
	ASSERT(enQueue(queue, &element[1]));
	ASSERT(enQueue(queue, &element[2]));
	ASSERT(4==*(int*)(deQueue(queue)));
	ASSERT(0==queue->queueInfo.front);
	ASSERT(enQueue(queue, &element[3]));
	ASSERT(3==queue->queueInfo.rear);
	ASSERT(false==enQueue(queue, &element[4]));
	ASSERT(5==*(int*)(deQueue(queue)));
	ASSERT(enQueue(queue, &element[4]));
	ASSERT(1==queue->queueInfo.front);
	ASSERT(0==queue->queueInfo.rear);
}
コード例 #26
0
ファイル: networkFlow.c プロジェクト: rajareddy1863/C-Codes
int breadthFirst(struct node * resgraph[], int from , int to, int nV, int parent[])
{
	int i;
	int visitList[nV];
	struct Queue * pop;
	struct node * var;
	for(i = 0;i < nV; i++)
	{
		visitList[i] = 0;
	}
	visitList[from] = 1;
	parent[from] = -1;
	enQueue(from);
	
	while(head != NULL)
	{
		pop = deQueue();
		var = resgraph[pop->nodes];
		while(var != NULL)
		{
			if(visitList[var->toNode] == 0 && var->weight > 0)
			{
				enQueue(var->toNode);
				visitList[var->toNode] = 1;
				parent[var->toNode] = pop->nodes;
			}
			var = var->next;
		}
	}
	
	if(visitList[to] == 1)
		return 1;
	return 0;
}
コード例 #27
0
ファイル: main.c プロジェクト: namannigam/zealous-codetroops
/* Driver function to test above functions */
int main()
{
  /* Create a queue with items 1 2 3*/
  struct queue *q = (struct queue*)malloc(sizeof(struct queue));
  q->stack1 = NULL;

  enQueue(q, 1);
  enQueue(q, 2);
  enQueue(q, 3);

  /* Dequeue items */
  printf("%d  ", deQueue(q));
  printf("%d  ", deQueue(q));
  printf("%d  ", deQueue(q));

  getchar();
}
コード例 #28
0
// Driver program
int main()
{
    // Create a queue with items 1, 2, 3
    struct queue *q = (struct queue*)malloc(sizeof(struct queue));
    q->stack1 = NULL;

    enQueue(q, 1);
    enQueue(q, 2);
    enQueue(q, 3);

    // Dequeue items
    printf("%d ", deQueue(q));
    printf("%d ", deQueue(q));
    printf("%d ", deQueue(q));

    return 0;
}
コード例 #29
0
ファイル: brinfsrc.c プロジェクト: Ukusbobra/open-watcom-v2
void BrinfSrcFini               // BrinfSrc COMPLETION
    ( void )
{
    for( ; ins_queue != NULL; ) {
        deQueue();
    }
    brinfSrcDestroy();
}
コード例 #30
0
ファイル: mpu_control.c プロジェクト: qosha1/CS53-Code
void mpu_init(void){
	mpu_setup_s *startup_config;
	boolean is_Started;
	
	// allocate memory and set to zero
	mpu_regs = malloc(sizeof *mpu_regs);
	if(!mpu_regs){
		while(1);
	}
	/* Configure mpu_regs to be a copy  of onboard registers  */
	// mpu actually starts up with two non-zero registers
	mpu_regs->who_am_i = MPU_I2C_ADDRESS;	// initialized with known address
	mpu_regs->pwr_mgmt_1 = BIT_SLEEP; // starts in sleep mode
	/* End configure of mpu_regs */

	NVIC_SetPriority (EXTI9_5_IRQn, 0x07); 			/* set priority to lower than i2c */
	NVIC_DisableIRQ(EXTI9_5_IRQn);							/* we don't want to interrupt during setup */
	
	/* Check to see if MPU is already loaded (from pre-CPU reset) */
	is_Started = mpu_isInitialized();
	
	// restart device 
	enQueue(mpuTxQueue, reg.pwr_mgmt_1);				// enqueue the register number
	enQueue(mpuTxQueue, BIT_RESET);							// enqueue the new value 
	mpu_writeRegister(1, reg.pwr_mgmt_1, false); // send to i2c interface
	
	Delay(1000);// wait for reset
	// wakeup device on restart
	mpu_regs->pwr_mgmt_1 &= MPU_WAKE_UP;
	enQueue(mpuTxQueue, reg.pwr_mgmt_1);				// enqueue the register number
	enQueue(mpuTxQueue, mpu_regs->pwr_mgmt_1);	// enqueue the new value 
	mpu_writeRegister(1, reg.pwr_mgmt_1, false); // send to i2c interface
	
	Delay(500);// wait for wakeup
	mpu_readRegister(1, reg.pwr_mgmt_1);
	while(queue_isEmpty(mpuRxQueue)){ }
	display_Int(deQueue(mpuRxQueue), 0, 0, true);
	
	/* Once device is on and awake, set it to default config */
	startup_config = malloc(sizeof *startup_config);
	startup_config->rate_div = STARTUP_RATE_DIV;
	startup_config->lpf = STARTUP_LPF;
	startup_config->user_ctrl = STARTUP_USERCTRL;
	startup_config->accel_cfg = STARTUP_ACCELCFG;
	startup_config->fifo_en = STARTUP_FIFOEN;
	startup_config->gyro_cfg = STARTUP_GYROCFG;
	startup_config->int_enable = STARTUP_INTNENABLE;
	startup_config->int_pin_cfg = STARTUP_INTPINCFG;
	
	configure_Mpu(startup_config);			/* call the function to write values */
	
	free(startup_config);								/* release memory */
	//NVIC_ClearPendingIRQ(EXTI9_5_IRQn);	/* clear any initial interrupts */
	NVIC_EnableIRQ(EXTI9_5_IRQn);				/* turn interrupts back on */

}