예제 #1
0
/** Create a huffman codebook from the input stream.
 * @param[in] fp Pointer to input stream
 * @param[out] codebook Huffman codebook
 */
void get_huffman_code(FILE *fp, struct huffcode codebook[ALPHLEN])
{
    int i;
    /* Get table of frequencies (> 0) of symbols in text */
    struct treenode ftable[ALPHLEN];
    int ftsize = create_freq_table(fp, ftable);
    /* Sort frequency table by frequency */
    qsort(ftable, ftsize, sizeof(struct treenode), node_compar);
    /* Initialize queues */
    struct queue *q1 = initqueue(), *q2 = initqueue();
    /* Enqueue leaf nodes in q1 */
    for (i=0; i<ftsize; i++) {
        enqueue(q1, &ftable[i]);
    }
    /* Form tree using both queues */
    while (q1->size > 1 || q2->size > 1) {
        struct treenode *tn[2];
        get_mintwo(q1, q2, tn);
        struct treenode *internal = malloc(sizeof(struct treenode)); 
        internal->symbol = 0;
        internal->freq = tn[0]->freq + tn[1]->freq;
        internal->l = tn[0];
        internal->is_internal = 1;
        tn[0]->p = internal;
        internal->r = tn[1];
        tn[1]->p = internal;
        enqueue(q2, internal);
    }
    /* Set root of tree */
    struct treenode *root = front(q1) ? front(q1) : front(q2);
    char code[MAX_CODELEN] = "";
    update_codes(root, codebook, code);
    freequeue(q1);
    freequeue(q2);
}
예제 #2
0
void initairport(struct airport *ap)
{
    initqueue( &(ap -> landing) );
    initqueue( &(ap -> takeoff) );
    ap -> pl = &(ap -> landing);
    ap -> pt = &(ap -> takeoff);
    ap -> nplanes = ap -> nland = ap -> ntakeoff = ap -> nrefuse = 0;
    ap -> landwait = ap -> takeoffwait = ap -> idletime = 0;
}
예제 #3
0
void main( )
{
	struct queue a ;
	int i ;

	clrscr( ) ;

	initqueue ( &a ) ;

	addq ( &a, 11 ) ;
	addq ( &a, -8 ) ;
	addq ( &a, 23 ) ;
	addq ( &a, 19 ) ;
	addq ( &a, 15 ) ;
	addq ( &a, 16 ) ;
	addq ( &a, 28 ) ;

	i = delq ( &a ) ;
	printf ( "\nItem extracted: %d", i ) ;

	i = delq ( &a ) ;
	printf ( "\nItem extracted: %d", i ) ;

	i = delq ( &a ) ;
	printf ( "\nItem extracted: %d", i ) ;

	delqueue ( &a ) ;

	getch( ) ;
}
예제 #4
0
파일: msgqueue.c 프로젝트: ansrivas/codes
void initmsgqueue(MessageQue * pMsgQue)
{
	if (NULL == pMsgQue)
	{	
		initqueue(pMsgQue->pQueue);
		pthread_mutex_init(&(pMsgQue->queLock), NULL);
		pthread_cond_init(&(pMsgQue->queCond), NULL);
	}
}
예제 #5
0
파일: mythread_rr.c 프로젝트: ketki95/3-1
void mythread_init(){
	maint = (struct mythread_t*)malloc(sizeof(struct mythread_t));
	if (getcontext(&(maint->uc)) == -1){
		printf("Error getting context\n");
		exit(1);
	}
	current = maint;
	readyq = (struct queue*) malloc(sizeof(struct queue)); 
	initqueue(readyq);
}
예제 #6
0
void floortraverse(TREE T){
	TREE p;
	LinkQueue Q;
	initqueue(&Q);p=T;
	inqueue(&Q,p);
	while(queueempty(Q)){
		outqueue(&Q,&p);
		print(p->data);
		if(p->left) inqueue(&Q,p->left);
		if(p->right) inqueue(&Q,p->right);
	}
}
예제 #7
0
/*
************************************************************************************************************
*
*                                             function
*
*    name          :
*
*    parmeters     :
*
*    return        :
*
*    note          :
*
*
************************************************************************************************************
*/
int sunxi_queue_init(void)
{
	Q = (queue *)malloc(sizeof(queue));
	if(!Q)
	{
		printf("malloc memory for sunxi queue failed\n");

		return -1;
	}
	initqueue(Q, SUNXI_BUFFER_QUEUE_COUNT, SUNXI_BUFFER_QUEUE_SIZE);

	return 0;
}
예제 #8
0
void breadtl(bintree tree) {
	cirqueue treequeue = initqueue(MAXTREE_TMP);
	btnode tr =  tree->root;
	if (tr)
		enqueue(treequeue, tr);

	while (qlength(treequeue) > 0) {
		tr = dequeue(treequeue);
		visit(tr->data);
		if (tr->lsubtr)
			enqueue(treequeue, tr->lsubtr);
		if (tr->rsubtr)
			enqueue(treequeue, tr->rsubtr);
	}
}
예제 #9
0
bool YYLibEvent::Init(ILibEvent	* callback)
{
	_callback = callback;

	base = event_base_new();
	if (!base)
	{
		LOG::Error("YYLibEvent init event base failed");
		return false;
	}

	AddQueueEvent(WRITE_EVENT_FLAG, this);

	if (!initqueue())
		return false;

	return true;
}
예제 #10
0
int main()
{
  queue q;
  initqueue(&q);
  showqueue(&q);

  printf("对尾插入元素...\n");
  InQueue(&q,'a');
  InQueue(&q,'b');
  InQueue(&q,'c');
  InQueue(&q,'d');
  showqueue(&q);
  printf("队列中的元素个数为%d\n",Length(&q));

  printf("队首删除元素..\n");
  OutQueue(&q);
  showqueue(&q);
  return 0;
}
예제 #11
0
void outdegree2(TREE T){
	TREE p;
	LinkQueue Q;
	int flag1=0,flag2=0,n0=0,n1=0,n2=0;
	initqueue(&Q);p=T;
	inqueue(&Q,p);
	while(queueempty(Q)){
		outqueue(&Q,&p);
		if(p->left) {inqueue(&Q,p->left);flag1=1;}
		if(p->right){inqueue(&Q,p->right);flag2=1;}
		if(flag1&&flag2) n2++;
		else if(!flag1&&!flag2) n0++;
		     else n1++;
	    flag1=flag2=0;
	}
	printf("出度为0的节点数目:%d\n",n0);
	printf("出度为1的节点数目:%d\n",n1);
	printf("出度为2的节点数目:%d\n",n2);
}
예제 #12
0
static void allinit () { 
    long i; 
    initqueue(); 
    for (i=0; i<MAXPROCESSES; i++) processes[i]=NULL; 
    for (i=0; i<procs; i++) { 
	// zero out pages from processes
	if (!empty()) {
	    processes[i]=dequeue(); 

	    sim_log(LOG_LOAD,"process %2d; pc %04d: loaded\n",i, processes[i]->pc); 
	    if (output) fprintf(output, "%ld,%ld,%ld,%ld,%ld,load\n", 
		sysclock, i, processes[i]->pid, 
		processes[i]->kind, processes[i]->pc);
	    if (pages) { 
		long j;
		for (j=0; j<MAXPROCPAGES; j++) 
		    fprintf(pages,"%ld,%ld,%ld,%ld,%ld,out\n",
			sysclock,i,j,processes[i]->pid,processes[i]->kind); 
	    } 
	} 
    } 
} 
예제 #13
0
/* called only at the start of a session, set up initial state */
void common_session_init(int sock_in, int sock_out) {
	time_t now;

#ifdef DEBUG_TRACE
	debug_start_net();
#endif

	TRACE(("enter session_init"))

	ses.sock_in = sock_in;
	ses.sock_out = sock_out;
	ses.maxfd = MAX(sock_in, sock_out);

	if (sock_in >= 0) {
		setnonblocking(sock_in);
	}
	if (sock_out >= 0) {
		setnonblocking(sock_out);
	}

	ses.socket_prio = DROPBEAR_PRIO_DEFAULT;
	/* Sets it to lowdelay */
	update_channel_prio();

	now = monotonic_now();
	ses.connect_time = now;
	ses.last_packet_time_keepalive_recv = now;
	ses.last_packet_time_idle = now;
	ses.last_packet_time_any_sent = 0;
	ses.last_packet_time_keepalive_sent = 0;
	
	if (pipe(ses.signal_pipe) < 0) {
		dropbear_exit("Signal pipe failed");
	}
	setnonblocking(ses.signal_pipe[0]);
	setnonblocking(ses.signal_pipe[1]);

	ses.maxfd = MAX(ses.maxfd, ses.signal_pipe[0]);
	ses.maxfd = MAX(ses.maxfd, ses.signal_pipe[1]);
	
	ses.writepayload = buf_new(TRANS_MAX_PAYLOAD_LEN);
	ses.transseq = 0;

	ses.readbuf = NULL;
	ses.payload = NULL;
	ses.recvseq = 0;

	initqueue(&ses.writequeue);

	ses.requirenext = SSH_MSG_KEXINIT;
	ses.dataallowed = 1; /* we can send data until we actually 
							send the SSH_MSG_KEXINIT */
	ses.ignorenext = 0;
	ses.lastpacket = 0;
	ses.reply_queue_head = NULL;
	ses.reply_queue_tail = NULL;

	/* set all the algos to none */
	ses.keys = (struct key_context*)m_malloc(sizeof(struct key_context));
	ses.newkeys = NULL;
	ses.keys->recv.algo_crypt = &dropbear_nocipher;
	ses.keys->trans.algo_crypt = &dropbear_nocipher;
	ses.keys->recv.crypt_mode = &dropbear_mode_none;
	ses.keys->trans.crypt_mode = &dropbear_mode_none;
	
	ses.keys->recv.algo_mac = &dropbear_nohash;
	ses.keys->trans.algo_mac = &dropbear_nohash;

	ses.keys->algo_kex = NULL;
	ses.keys->algo_hostkey = -1;
	ses.keys->recv.algo_comp = DROPBEAR_COMP_NONE;
	ses.keys->trans.algo_comp = DROPBEAR_COMP_NONE;

#ifndef DISABLE_ZLIB
	ses.keys->recv.zstream = NULL;
	ses.keys->trans.zstream = NULL;
#endif

	/* key exchange buffers */
	ses.session_id = NULL;
	ses.kexhashbuf = NULL;
	ses.transkexinit = NULL;
	ses.dh_K = NULL;
	ses.remoteident = NULL;

	ses.chantypes = NULL;

	ses.allowprivport = 0;

	TRACE(("leave session_init"))
}
예제 #14
0
int
procfile(char *fn)
{
	str_t ln;
	file_t *f;
	int c, t, z, nottext;

	if (fn == NULL) {
		fn = "(standard input)";
		f = grep_fdopen(STDIN_FILENO, "r");
	} else {
		f = grep_open(fn, "r");
	}
	if (f == NULL) {
		file_err = 1;
		if (!sflag)
			warn("%s", fn);
		return 0;
	}

	nottext = grep_bin_file(f);
	if (nottext && binbehave == BIN_FILE_SKIP) {
		grep_close(f);
		return 0;
	}

	ln.file = fn;
	ln.line_no = 0;
	ln.len = 0;
	linesqueued = 0;
	tail = 0;
	ln.off = -1;

	if (Bflag > 0)
		initqueue();
	for (c = 0;  c == 0 || !(lflag || qflag); ) {
		ln.off += ln.len + 1;
		if ((ln.dat = grep_fgetln(f, &ln.len)) == NULL)
			break;
		if (ln.len > 0 && ln.dat[ln.len - 1] == '\n')
			--ln.len;
		ln.line_no++;

		z = tail;

		if ((t = procline(&ln, nottext)) == 0 && Bflag > 0 && z == 0) {
			enqueue(&ln);
			linesqueued++;
		}
		c += t;
	}
	if (Bflag > 0)
		clearqueue();
	grep_close(f);

	if (cflag) {
		if (!hflag)
			printf("%s:", ln.file);
		printf("%u\n", c);
	}
	if (lflag && c != 0)
		printf("%s\n", fn);
	if (Lflag && c == 0)
		printf("%s\n", fn);
	if (c && !cflag && !lflag && !Lflag &&
	    binbehave == BIN_FILE_BIN && nottext && !qflag)
		printf("Binary file %s matches\n", fn);

	return c;
}
예제 #15
0
파일: 04C2.c 프로젝트: alstamber/cpa
int main(void){
#define BUFSIZE (N*2)
    char buf[BUFSIZE];
    char maze[N][N];
    int i, j, sx, sy, gx, gy;
    struct cost pos;
    struct queue hoge;
    struct cost tsugi;

    for (i=0; i<N; i++){
        fgets(buf, BUFSIZE, stdin);
        for (j=0; j<N && (buf[j] != '\n' && buf[j] != '\0'); j++){
            if(buf[j] == 'S'){
                sx = j;
                sy = i;
                buf[j] = ' ';
            }else if(buf[j] == 'G'){
                gx = j;
                gy = i;
                buf[j] = ' ';
            }
            maze[i][j] = buf[j];
        }
        while(j<N) maze[i][j++] = ' ';
    }

    initqueue(&hoge);

    pos.x = sx;
    pos.y = sy;
    pos.c = 0;

    putq(&hoge, pos);

    while(!queueempty(&hoge)){
        pos = getq(&hoge);
        if (pos.c == N * N){
            pos.c = -1;
            break;
        }
        if (pos.x == gx && pos.y == gy){
            break;
        }
        maze[pos.y][pos.x] = '*';

        if (maze[pos.y][pos.x - 1] == ' '){
            tsugi.x = pos.x - 1;
            tsugi.y = pos.y;
            tsugi.c = pos.c + 1;
            putq(&hoge, tsugi);
        }
        if (maze[pos.y][pos.x + 1] == ' '){
            tsugi.x = pos.x + 1;
            tsugi.y = pos.y;
            tsugi.c = pos.c + 1;
            putq(&hoge, tsugi);
        }
        if (maze[pos.y + 1][pos.x] == ' '){
            tsugi.x = pos.x;
            tsugi.y = pos.y + 1;
            tsugi.c = pos.c + 1;
            putq(&hoge, tsugi);

        }
        if(maze[pos.y - 1][pos.x] == ' '){
            tsugi.x = pos.x;
            tsugi.y = pos.y - 1;
            tsugi.c = pos.c + 1;
            putq(&hoge, tsugi);
        }
    }

    if (pos.x != gx || pos.y != gy){
        pos.c = -1;
    }



    printf("%d\n", pos.c);

    return 0;


}
/* called only at the start of a session, set up initial state */
void common_session_init(int sock, char* remotehost) {

	TRACE(("enter session_init"))

	ses.remotehost = remotehost;

	ses.sock = sock;
	ses.maxfd = sock;

	ses.connecttimeout = 0;
	
	if (pipe(ses.signal_pipe) < 0) {
		dropbear_exit("signal pipe failed");
	}
	setnonblocking(ses.signal_pipe[0]);
	setnonblocking(ses.signal_pipe[1]);
	
	kexfirstinitialise(); /* initialise the kex state */

	ses.writepayload = buf_new(MAX_TRANS_PAYLOAD_LEN);
	ses.transseq = 0;

	ses.readbuf = NULL;
	ses.decryptreadbuf = NULL;
	ses.payload = NULL;
	ses.recvseq = 0;

	initqueue(&ses.writequeue);

	ses.requirenext = SSH_MSG_KEXINIT;
	ses.dataallowed = 0; /* don't send data yet, we'll wait until after kex */
	ses.ignorenext = 0;
	ses.lastpacket = 0;

	/* set all the algos to none */
	ses.keys = (struct key_context*)m_malloc(sizeof(struct key_context));
	ses.newkeys = NULL;
	ses.keys->recv_algo_crypt = &dropbear_nocipher;
	ses.keys->trans_algo_crypt = &dropbear_nocipher;
	
	ses.keys->recv_algo_mac = &dropbear_nohash;
	ses.keys->trans_algo_mac = &dropbear_nohash;

	ses.keys->algo_kex = -1;
	ses.keys->algo_hostkey = -1;
	ses.keys->recv_algo_comp = DROPBEAR_COMP_NONE;
	ses.keys->trans_algo_comp = DROPBEAR_COMP_NONE;

#ifndef DISABLE_ZLIB
	ses.keys->recv_zstream = NULL;
	ses.keys->trans_zstream = NULL;
#endif

	/* key exchange buffers */
	ses.session_id = NULL;
	ses.kexhashbuf = NULL;
	ses.transkexinit = NULL;
	ses.dh_K = NULL;
	ses.remoteident = NULL;

	ses.chantypes = NULL;

	ses.allowprivport = 0;

	TRACE(("leave session_init"))
}
예제 #17
0
파일: 04D.c 프로젝트: neotaso/pp
int main(void) {
  char buf[BUFSIZE];
  char canvas[N][N];
  int i,j;
  elemtype point,pos;
  struct queue que;
  initqueue(&que);
  
  for (i = 0; i < N; i++) {
    fgets(buf, BUFSIZE, stdin);
    for (j = 0; j < N && (buf[j] != '\n' && buf[j] != '\0'); j++)
      canvas[i][j] = buf[j];
    while (j < N) canvas[i][j++] = ' ';
  }

  point.x=point.y=N/2;
  canvas[point.y][point.x]=C;
  enqueue(&que,point);

  while(!queueempty(&que)){
    point=dequeue(&que);
    if(point.y!=0){
      if(canvas[point.y-1][point.x]==' '){
	pos.x=point.x;
	pos.y=point.y-1;
	enqueue(&que,pos);
	canvas[pos.y][pos.x]=C;
      }
    }
    if(point.x!=0){
      if(canvas[point.y][point.x-1]==' '){
	pos.x=point.x-1;
	pos.y=point.y;
	enqueue(&que,pos);
	canvas[pos.y][pos.x]=C;
      }
    }
    if(point.y!=N-1){
      if(canvas[point.y+1][point.x]==' '){
	pos.x=point.x;
	pos.y=point.y+1;
	enqueue(&que,pos);
	canvas[pos.y][pos.x]=C;
      }
    }
    if(point.x!=N-1){
      if(canvas[point.y][point.x+1]==' '){
	pos.x=point.x+1;
	pos.y=point.y;
	enqueue(&que,pos);
	canvas[pos.y][pos.x]=C;
      }
    }
  }

  for(i=0;i<N;i++){
    for(j=0;j<N;j++)printf("%c",canvas[i][j]);
    printf("\n");
  }
  return 0;
}
/* called only at the start of a session, set up initial state */
void common_session_init(int sock_in, int sock_out) {

	TRACE(("enter session_init"))

	ses.sock_in = sock_in;
	ses.sock_out = sock_out;
	ses.maxfd = MAX(sock_in, sock_out);

	ses.connect_time = 0;
	ses.last_trx_packet_time = 0;
	ses.last_packet_time = 0;
	
	if (pipe(ses.signal_pipe) < 0) {
		dropbear_exit("Signal pipe failed");
	}
	setnonblocking(ses.signal_pipe[0]);
	setnonblocking(ses.signal_pipe[1]);

	ses.maxfd = MAX(ses.maxfd, ses.signal_pipe[0]);
	ses.maxfd = MAX(ses.maxfd, ses.signal_pipe[1]);
	
	kexfirstinitialise(); /* initialise the kex state */

	ses.writepayload = buf_new(TRANS_MAX_PAYLOAD_LEN);
	ses.transseq = 0;

	ses.readbuf = NULL;
	ses.payload = NULL;
	ses.recvseq = 0;

	initqueue(&ses.writequeue);

	ses.requirenext = SSH_MSG_KEXINIT;
	ses.dataallowed = 1; /* we can send data until we actually 
							send the SSH_MSG_KEXINIT */
	ses.ignorenext = 0;
	ses.lastpacket = 0;
	ses.reply_queue_head = NULL;
	ses.reply_queue_tail = NULL;

	/* set all the algos to none */
	ses.keys = (struct key_context*)m_malloc(sizeof(struct key_context));
	ses.newkeys = NULL;
	ses.keys->recv.algo_crypt = &dropbear_nocipher;
	ses.keys->trans.algo_crypt = &dropbear_nocipher;
	ses.keys->recv.crypt_mode = &dropbear_mode_none;
	ses.keys->trans.crypt_mode = &dropbear_mode_none;
	
	ses.keys->recv.algo_mac = &dropbear_nohash;
	ses.keys->trans.algo_mac = &dropbear_nohash;

	ses.keys->algo_kex = -1;
	ses.keys->algo_hostkey = -1;
	ses.keys->recv.algo_comp = DROPBEAR_COMP_NONE;
	ses.keys->trans.algo_comp = DROPBEAR_COMP_NONE;

#ifndef DISABLE_ZLIB
	ses.keys->recv.zstream = NULL;
	ses.keys->trans.zstream = NULL;
#endif

	/* key exchange buffers */
	ses.session_id = NULL;
	ses.kexhashbuf = NULL;
	ses.transkexinit = NULL;
	ses.dh_K = NULL;
	ses.remoteident = NULL;

	ses.chantypes = NULL;

	ses.allowprivport = 0;

	TRACE(("leave session_init"))
}