Esempio n. 1
0
void k_merge_sort(char *ofname, char **fnames) {
    init_buff(ofname, fnames);
    int i=0; 
    for(; i<K; ++i) {
        input(i, BUFF_SIZE);
        // first step
        losertree.nodes[i] = pop_head(i);
    }
    build_loser_tree();
    // sort
    datatype winner_data;
    index_type winner = losertree.losers[0];
    datatype nextdata;
    while(1) {
        winner = losertree.losers[0];
        winner_data = losertree.nodes[winner];
        if(strcmp(winner_data, MAX_DATA) == 0) break;
        // save it to out_buff
        append_output_buff(winner_data);
        // get next data
        nextdata = pop_head(winner);
        losertree.nodes[winner] = nextdata;
        adjust_loser_tree(winner);
    }
    flush_output_buff();
}
Esempio n. 2
0
int main(){
	struct queue Q;
	Q.head = Q.tail = NULL;
	// printf("%p\n", Q.head);
	add(&Q, 1);
	add(&Q, 2);
	print(&Q);
	int a = pop_head(&Q);
	int b = pop_head(&Q);
	pop_head(&Q);
	printf("Popped val: %d\n", a);
	print(&Q);
}
 std::shared_ptr<T> pop() {
     node *old_head = pop_head();
     if (!old_head) { return std::shared_ptr<T>(); }
     std::shared_ptr<T> const res(old_head->data);
     delete old_head;
     return res;
 }
 std::unique_ptr<node> try_pop_head()
 {
     std::lock_guard<std::mutex> head_lock(head_mutex);
     if (head.get() == get_tail()) {
         return std::unique_ptr<node>();
     }
     return pop_head();
 }
 std::unique_ptr<node> try_pop_head(T& value)
 {
     std::lock_guard<std::mutex> head_lock(head_mutex);
     if (head.get() == get_tail()) {
         return std::unique_ptr<node>();
     }
     value = std::move(*head->data);
     return pop_head();
 }
Esempio n. 6
0
//删除指定位置的节点
int del(List *plist, int pos)
{
    //1.判断坐标是否合法
    if(pos < 0 || pos >= size(plist))
    {
        printf("坐标不合法, 删除节点失败\n");
        return -1;

        //pos = 0;
        //pos = size(plist) - 1;
    }

    //2.当pos = 0时, 调用pop_head函数
    if(0 == pos)
    {
        return pop_head(plist);
    }

    //3.当pos = size(plist)-1, 调用pop_tail函数
    if(size(plist) - 1 == pos)
    {
        return pop_tail(plist);
    }

    //4.当pos为其他数值时的情况
    Node *pt = plist->head;
    int i = 0;
    for(i = 1; i < pos; i++)
    {
        //把对出来的next跑光
        pt = pt->next;
    }

    //把pos=1时的代码处理如下
    Node *pn = pt->next;
    pt->next = pn->next;
    int temp = pn->data;
    free(pn);
    pn = NULL;
    --plist->cnt;
    return temp;
}
Esempio n. 7
0
/*
	Returns whether there is pending input or not.
*/
bool inputPending( )
{
	int old_ssp = Super( 1 );
	int old_ipl;
	
	if( USER_MODE == old_ssp )
		old_ssp = Super( 0 );
		
	old_ipl = set_ipl( KEYBOARD_INTERRUPT_MASK );
	
	while( cBffrHead != cBffrTail && !cKYBDBuffer[ cBffrHead ].bValid )
		pop_head( );
		
	set_ipl( old_ipl );
	
	if( SUPER_MODE != old_ssp )
		Super( old_ssp );
	
	return EMPTY != cFill;
}
static void test_pop_head(void)
{
  using string = mtr::string;
  using vector = mtr::vector<string>;

  const char *strs[] = { "string 1",
                         "string 2",
                         "string 3",
                         "string 4" };

  auto v = vector().push_tail(string(strs[0]))
                   .push_tail(string(strs[1]))
                   .push_tail(string(strs[2]))
                   .push_tail(string(strs[3]));

  SHOULD_BE_EQ(v.head(), string(strs[0]), "tail should equal first string");
  SHOULD_BE_EQ((int) v.length(), 4, "vector should be 4 items long");
  auto x = v.pop_head();
  SHOULD_BE_EQ(x.head(), string(strs[1]), "tail should equal second string");
  SHOULD_BE_EQ((int) x.length(), 3, "vector should be 3 items long");
}
 std::shared_ptr<T> try_pop() {
     std::unique_ptr<node> old_head = pop_head();
     return old_head ? old_head->data : std::shared_ptr<T>();
 }
Esempio n. 10
0
/*
	Returns the next Character in the buffer.
	Returns 0x80 if an error occurs and there is nothing in the buffer to return.
*/
UINT8 getInput( )
{
	return pop_head( );
}
Esempio n. 11
0
// function defining the working of the worked thread
void* worker_thread(void* arg){
	
	while(1){
		// if queue is empty, then wait for it to contain something
		pthread_mutex_lock(&mutex);
		while(waiting.curr_len == 0){
			printf("Queue empty\n");
			pthread_cond_wait(&has_data, &mutex);
		}

		// extract the first element from the queue
		int newsockfd = pop_head(&waiting);
		
		// signal that queue has space since the first element has been popped
		pthread_cond_signal(&has_space);
		pthread_mutex_unlock(&mutex);

		char buffer[256];
		bzero(buffer, 256);
		int n = read(newsockfd,buffer, 255);
		if (n < 0){
			printf("ERROR reading from socket");
			continue;
		}

		// server code from the previous labs
		if((buffer[0] == 'g' || buffer[0] == 'G') && (buffer[1] == 'e' || buffer[1] == 'E') && (buffer[2] == 't' || buffer[2] == 'T')
					&& (buffer[3] == ' ') ){
			// get file from the server and send to client			
			char filepath[100]; //string copy
			int i;
		
			for(i=4;buffer[i]!='\0';i++){
				filepath[i-4] = buffer[i];
				// printf("%c\n", buffer[i]);
			}
			filepath[i-4] = '\0';

			char sendbuffer[PACKETSIZE];

			FILE* fp;
			fp = fopen(filepath, "rb");
			if(fp == NULL){
				printf("Error in opening file\n");
				continue;
			}

			int bytesSent;
			int read = 0;
			
	        //send file 512 bytes at a time
			while((read = fread(sendbuffer, sizeof(sendbuffer), 1, fp)) > 0){
				bytesSent = write(newsockfd, sendbuffer, PACKETSIZE);

				if(bytesSent < 0){
					printf("Error while writing to socket\n");
					break;	
				}
			}
			fclose(fp);
		}

		else{
			printf("Invalid Command received\n");
			
			/* send reply to client */
			n = write(newsockfd,"Invalid Command", 15);
			if (n < 0) {
				printf("ERROR writing to socket\n");
				continue;
			}
		}
		close(newsockfd);
	}

	return 0;
}
Esempio n. 12
0
int main()
{
    //创建单链表, 并且初始化
    List list;
    list.head = NULL;
    list.tail = NULL;
    list.cnt = 0;


    printf("%s\n", empty(&list) ? "空" : "没空");
    printf("%s\n", full(&list) ? "满" : "没满");
    printf("链表中有%d个元素\n", size(&list));
    printf("----------------\n");
    push_head(&list, 11);
    travel(&list);
    push_head(&list, 22);
    travel(&list);
    push_head(&list, 33);
    travel(&list);
    printf("链表中有%d个元素\n", size(&list));
    printf("----------------\n");

    push_tail(&list, 44);
    travel(&list);
    push_tail(&list, 55);
    travel(&list);
    push_tail(&list, 66);
    travel(&list);

    printf("链表中有%d个元素\n", size(&list));
    printf("----------------\n");
    insert(&list, 77, -2);
    printf("链表中有%d个元素\n", size(&list));
    travel(&list);
    insert(&list, 88, 0);
    printf("链表中有%d个元素\n", size(&list));
    travel(&list);
    insert(&list, 99, 5);
    printf("链表中有%d个元素\n", size(&list));
    travel(&list);
    insert(&list, 100, 2);
    printf("链表中有%d个元素\n", size(&list));
    travel(&list);

    printf("--------------------\n");
    printf("头节点元素值: %d\n", get_head(&list));
    printf("尾节点元素值: %d\n", get_tail(&list));

    printf("--------------------\n");
    pop_head(&list);
    printf("链表中有%d个元素\n", size(&list));
    printf("头节点元素值: %d\n", get_head(&list));
    printf("尾节点元素值: %d\n", get_tail(&list));
    printf("--------------------\n");
    pop_tail(&list);
    printf("链表中有%d个元素\n", size(&list));
    printf("头节点元素值: %d\n", get_head(&list));
    printf("尾节点元素值: %d\n", get_tail(&list));

    printf("--------------------\n");
    del(&list, -2);
    travel(&list);

    del(&list, 0);
    travel(&list);

    del(&list, 4);
    travel(&list);

    del(&list, 1);
    travel(&list);

    del(&list, 2);
    travel(&list);

    del(&list, 7);
    travel(&list);
    printf("--------------------\n");
    reverse_list(&list);
    printf("链表中有%d个元素\n", size(&list));
    printf("头节点元素值: %d\n", get_head(&list));
    printf("尾节点元素值: %d\n", get_tail(&list));
    travel(&list);
    printf("--------------------\n");
    reverse_travel_list(&list);
    printf("=====清空========\n");
    clear(&list);
    return 0;
}
Esempio n. 13
0
//清空链表中的所有节点
void clear(List *plist)
{
    while(-1 != pop_head(plist));
}
Esempio n. 14
0
 std::unique_ptr<node> wait_pop_head(T& value)
 {
     std::unique_lock<std::mutex> head_lock(wait_for_data());
     value = std::move(*head->data);
     return pop_head();
 }
Esempio n. 15
0
 std::unique_ptr<node> wait_pop_head()
 {
     std::unique_lock<std::mutex> head_lock(wait_for_data());
     return pop_head();
 }