/* Add a new node to the tree */
struct Node *addnode(long value, struct Node* pNode)
{
  if(pNode == NULL)                        /* If there's no node              */
    return createnode(value);              /* ...create one and return it     */

  if(value ==pNode->item)
  {                                        /* Value equals current node       */
    ++pNode->count;                        /* ...so increment count and       */
    return pNode;                          /* ...return the same node         */
  }

  if(value < pNode->item)                  /* If less than current node value */
  {
    if(pNode->pLeft == NULL)               /* and there's no left node        */
    {
      pNode->pLeft = createnode(value);    /* create a new left node and      */
      return pNode->pLeft;                 /* return it.                      */
    }
    else                                   /* If there is a left node...      */
      return addnode(value, pNode->pLeft); /* add value via the left node     */
  }
  else                                     /* value is greater than current   */
  {
    if(pNode->pRight == NULL)              /* so the same process with        */
    {                                      /* the right node.                 */
      pNode-> pRight = createnode(value);
      return pNode-> pRight;
    }
    else
      return addnode(value, pNode-> pRight);
  }
}
Esempio n. 2
0
/* Add a new node to the tree */
Node *addnode(Name *pName, Node* pNode)
{
  if(!pNode)                               /* If there's no node              */
    return createnode(pName);              /* ...create one and return it     */

  if(compare(pName, pNode->pName) == 0)
  {                                        /* Name equals current node        */
    ++pNode->count;                        /* ...so increment count and       */
    return pNode;                          /* ...return the same node         */
  }

  if(compare(pName, pNode->pName) < 0)     /* If less than current node name  */
  {
    if(!pNode->pLeft)                      /* and there's no left node        */
    {
      pNode->pLeft = createnode(pName);    /* create a new left node and      */
      return pNode->pLeft;                 /* return it.                      */
    }
    else                                   /* If there is a left node...      */
      return addnode(pName, pNode->pLeft); /* add value via the left node     */
  }
  else                                     /* value is greater than current   */
  {
    if(!pNode->pRight)                     /* so the same process with        */
    {                                      /* the right node.                 */
      pNode->pRight = createnode(pName);
      return pNode-> pRight;
    }
    else
      return addnode(pName, pNode->pRight);
  }
}
Esempio n. 3
0
void addnode (node* element, node* root)
{ 
  if (element->record->index < root->record->index && root->left == NULL)
  {
    root->left = element; /* mniejszy i wolny lewy syn => element
                             staje się lewym synem */
    element->up = root;
  }
  else
  {
    if (element->record->index < root->record->index && root->left != NULL)
    {
      addnode (element, root->left); /* mniejszy i zajęte miejsce
                                         lewego syna => zejdź niżej */
    }
    else
    {  /* wiadomo że index elementu jest większy */
      if (root->right == NULL)
      {
        root->right = element; /* wolny prawy syn => element staje
                                  się prawym synem */
        element->up = root;
      }
      else
      {
        addnode (element,root->right); /* w przeciwnym przypadku idziemy
                                           głębiej w prawo */
      }
    }
  }
  
}
Esempio n. 4
0
void addnode(Node** node, int newvalue)
{

	if (newvalue > (*node)->value) {
		if ((*node)->right == NULL) {
			Node* newnode = malloc(sizeof(Node));
			newnode->value = newvalue;
			newnode->left = NULL;
			newnode->right = NULL;
			(*node)->right = newnode;
		} else {
			addnode(&((*node)->right), newvalue);
		}
	} else if (newvalue < (*node)->value) {
		if ((*node)->left == NULL) {
			Node* newnode = malloc(sizeof(Node));
			newnode->value = newvalue;
			newnode->left = NULL;
			newnode->right = NULL;
			(*node)->left = newnode;
		} else {
			addnode(&((*node)->left), newvalue);
		}
	} else {
		return;
	}
	
}
Esempio n. 5
0
File: w_init.c Progetto: j13s/devil
/* Draws a button with an image. data is the substructure of
   the button. bm is the pointer to the normal image. abm the pointer to the
   image for the activated button. The color 0 in the images is replaced
   with the correct background color for the button. The button is as large
   as the image plus the edge of the button if the xsize is set to -1.
   If the button is a string button ysize may be set to -1 to get the
   stdsize for stringbuttons. If the buttons is not a string button and
   xsize!=-1 then ysize must be set as well.
   Therefore both images must be of equal size. The position is the position
   of the left upper edge of the button (not the image).
   Returns pointer to button-structure if successful, 0 otherwise. */
struct w_button *w_addimagebutton(struct w_window *w, enum but_types type,
                                  int xpos, int ypos, int xsize, int ysize,
                                  struct ws_bitmap *bm,
                                  struct ws_bitmap *abm, void *data,
                                  int with_bg,
                                  int draw)
{
    struct w_button *b, *nb;
    struct w_description *d1, *d2;


    if (abm == NULL || bm == NULL || abm->xsize != bm->xsize || abm->ysize !=
        bm->ysize) {
        return NULL;
    }

    checkmem( b = MALLOC( sizeof(struct w_button) ) );
    b->xpos = xpos;
    b->ypos = ypos;
    b->w = w;

    if (xsize < 0) {
        b->xsize = bm->xsize + (type == w_b_switch ? 6 : 2);
        b->ysize = bm->ysize + (type == w_b_switch ? 6 : 2);
    }
    else {
        b->xsize = xsize;
        b->ysize = ysize < 0 ? shapes.titlebar_height + 5 : ysize;
    }

    b->type = type;
    b->helptxt = NULL;
    initlist(&b->inscription);
    initlist(&b->a_inscription);
    b->d.d = data;
    checkmem( d1 = MALLOC( sizeof(struct w_description) ) );
    d1->contents.bm = bm;
    d1->type = w_bdt_image;
    d1->image_with_bg = with_bg;
    d1->y = d1->x = type == w_b_switch ? 3 : 1;

    if (type == w_b_string) {
        d1->y = (b->ysize - bm->ysize) / 2;
        b->d.str->d_xsize = bm->xsize + 2;
        b->d.str->offset = 0;
        b->d.str->length = (b->xsize - b->d.str->d_xsize - 10) / ws_pixstrlen(
            "M");
    }

    checkmem( addnode(&b->inscription, -1, d1) );
    checkmem( d2 = MALLOC( sizeof(struct w_description) ) );
    *d2 = *d1;
    d2->contents.bm = abm;
    checkmem( addnode(&b->a_inscription, -1, d2) );
    nb = w_addbutton(b, draw);
    free(b);
    return nb;
}
Esempio n. 6
0
static TLDNode *addnode(TLDList *tld, char *hostname, TLDNode *node) {

    if(node == NULL)
    {
        TLDNode *node = (TLDNode *)malloc(sizeof(TLDNode));
        if(node != NULL)
        {
            node->parent = NULL;
            node->content = hostname;
            node->left = NULL;
            node->right = NULL;
            node->count = 1;
            tld->root = node;
            tld->size++;
        }
        else {
            free(node);
        }
        return node;
    }else{
        int cmp = strcmp(hostname, node->content);
        if(cmp < 0)
        {
            if (node->left != NULL) //turn left
                return addnode(tld, hostname, node->left); //recursive
            else {
                //add node if blank
                TLDNode *n = makeNode(hostname);
                n->parent = node;
                node->left = n;

                balance(node->left); //create the balance
            }
        }
        else if(cmp > 0)
        {
            if (node->right != NULL)
                return addnode(tld, hostname, node->right);
            else {
                TLDNode *n = makeNode(hostname);
                n->parent = node;
                node->right = n;

                balance(node->right);
            }
        }
        else
        {
            free(hostname);
            node->count++;
        }
    }

    return node;
}
int main()
{
node *head;
head=addnode(5);
head->next=addnode(3);
head->next->next=addnode(3);
head->next->next->next=addnode(8);
head->next->next->next->next=addnode(8);
print(head);
end_first(&head);
print(head);
return 0;
}
Esempio n. 8
0
File: flood.cpp Progetto: qbolec/c
void oneproblem()
{
unsigned char a,b,m;
int aa,bb,mx,gp=0;

cin >> m;
mx=(m-'A')+1;
tonextline();


for(int i=0;i<mx;i++)
{
 graph[i].f=graph[i].saco=0;
};
for(int x=0;x<mx;x++)
 for(int y=0;y<mx;y++)
  con[x][y]=0;



cin.get(a);
 
while( (a!= 10) && (!cin.eof()) )
{
  cin.get(b);
  tonextline();

  aa=a-'A';
  bb=b-'A';
  if((con[aa][bb]==0) && (aa!=bb))
  {
  addnode(aa,bb);
  addnode(bb,aa);
  con[aa][bb]=1;
  con[bb][aa]=1;
  };
  cin.get(a);
}

for(int i=0;i<mx;i++)
{
  if(graph[i].f==0)
  {
  gp++;
  flood(i);
  }
}
cout << gp << endl;
}
void dlx_init(int col)
{
 memset(head,-1,sizeof(head));
 memset(res,0,sizeof(res));
 memset(o,0,sizeof(o));
 memset(s,0,sizeof(s));
 int i;
 node=-1;
 addnode(node);
 for(i=1;i<=col;i++)
 {
        addnode(node);
        insert_row(0, node);  
 }
}
Esempio n. 10
0
/*
 * tldlist_add adds the TLD contained in `hostname' to the tldlist if
 * `d' falls in the begin and end dates associated with the list;
 * returns 1 if the entry was counted, 0 if not
 */
int tldlist_add(TLDList *tld, char *hostname, Date *d) {
    // check if it's within the tld dates
    if (date_compare(tld->begin, d) > 0 ||date_compare(tld->end, d) < 0)
        return 0;
    int i , counter=0;
    int len = strlen(hostname);
    for(i=len;i>0;i--){
        if(hostname[i] == '.'){
            counter=i;
            break;
        }
    }
    char *tempTLDnode = hostname+counter+1;
    int hostlen = strlen(tempTLDnode);
    char *tldstr = (char *)malloc((hostlen + 1));

    tldstr[hostlen] = '\0'; // make sure there is a null end

    for(i=0;i<hostlen;i++){
        tldstr[i] = tempTLDnode[i];
    }

    tld->root = addnode(tld, tldstr, tld->root);
    tld->count++;
    return 1;
}
Esempio n. 11
0
int main(){
    TTREE *toku = (TTREE*)malloc(sizeof(TTREE));
    int casos,a,nnode,num,b;
    toku->root = NULL;

    scanf("%d", &casos);
    for(a=1;a<=casos;a++){
        scanf("%d", &nnode);
        for(b=0;b<nnode;b++){
            scanf("%d", &num);
            addnode(toku,&num);
        }
        printf("Case %d:\n", a);
        printf("Pre.:");
        createPre(toku->root);
        printf("\n");
        printf("In..:");
        CreateIn(toku->root);
        printf("\n");
        printf("Post:");
        CreatePost(toku->root);
        printf("\n\n");
        toku->root = NULL;


    }
}
Esempio n. 12
0
File: myndbm.c Progetto: rdebath/cvs
/* Note: only updates the in-memory copy, which is written out at
   mydbm_close time.  Note: Also differs from DBM in that on duplication,
   it gives a warning, rather than either DBM_INSERT or DBM_REPLACE
   behavior.  */
int
mydbm_store (DBM *db, datum key, datum value, int flags)
{
    Node *node;

    node = getnode ();
    node->type = NDBMNODE;

    node->key = xmalloc (key.dsize + 1);
    *node->key = '\0';
    strncat (node->key, key.dptr, key.dsize);

    node->data = xmalloc (value.dsize + 1);
    *(char *)node->data = '\0';
    strncat (node->data, value.dptr, value.dsize);

    db->modified = 1;
    if (addnode (db->dbm_list, node) == -1)
    {
	error (0, 0, "attempt to insert duplicate key `%s'", node->key);
	freenode (node);
	return 0;
    }
    return 0;
}
Esempio n. 13
0
int main()
{
	int mode,data;
	node_type *cur;
	
	first=last=NULL;
	
	while(1)
	{
		printf("mode 1:add 2:pop> ");
		scanf("%d",&mode);
		
		if(mode==1)
		{
			scanf("%d",&data);
			addnode(data);
		}
		else if(mode==2)
		{			
			ptr=pop();
			printf("%d\n",ptr->info);
		}
		
		printf("DATA> ");
		cur=first;
		while(cur!=NULL)
		{
			printf("%d ",cur->info);
			cur=cur->next;
		}
		printf("\n");
	}
	return 0;
}
Esempio n. 14
0
struct tnode* addnode(struct tnode *root, int data){
	struct tnode *t = NULL;
		
	if(root == NULL){
		t = talloc(data);
		return t;
	}
	else if(data <= root->data){
		root->left = addnode(root->left, data);
	}
	else{
		root->right = addnode(root->right, data);
	}

	return root;
}
Esempio n. 15
0
/*============================================
			      主函数
 ============================================*/
int main(void)
{
	long newvalue = 0;
	struct Node *pRoot = NULL;
	char answer = 'n';

	do
	{
		printf("Enter the node value:");
		scanf(" %ld",&newvalue);

		if(pRoot == NULL)
		{
			pRoot = createnode(newvalue);
		}
		else
		{
			addnode(newvalue,pRoot);
		}

		printf("\nDo you want to enter another value (Y or N)?");
		scanf(" %c",&answer);
	}while(tolower(answer) == 'y');

	printf("\nThe value in ascending sequence are:\n");
	listnodes(pRoot);
	freenodes(pRoot);


	return 0;
}
Esempio n. 16
0
void addword(node* root,char* word)
{
    node* t,*prev;
    t=root;prev=root;
    printf("ok");
    int l=strlen(word),i,index;
    for(i=0;i<l;i++)
    {
        printf("ok");
        t->prefixes=(t->prefixes)+1;
        printf("ok");
        index=(int)word[i]-97;
        prev=t;
        t=t->child[index];
        printf("ok");
        if(t==NULL)
        {
            addnode(prev,word[i]);
        }
        if(i==l-1){
            t->word=true;
            t->string=word;
        }

    }
}
Esempio n. 17
0
File: w_init.c Progetto: j13s/devil
struct w_button *w_addbutton(struct w_button *b, int draw) {
    struct wi_button *bi;


    my_assert( b != NULL && b->d.d != NULL && (b->w != NULL || !draw) );
    checkmem( bi = MALLOC( sizeof(struct wi_button) ) );
    bi->b = *b;
    copylisthead(&bi->b.inscription, &b->inscription);
    copylisthead(&bi->b.a_inscription, &b->a_inscription);
    bi->sys_button = 0;
    bi->activated = 1;
    bi->bm = NULL;
    bi->drawn = draw;
    bi->tobechanged = 1;

    if (b->w) {
        if ( draw && !wi_drawbutton(bi, 0) ) {
            return NULL;
        }

        checkmem( bi->n =
                     addnode(&( (struct wi_window *)bi->b.w )->buttonlist, -1,
                             bi) );
        bi->b.no = bi->n->no;
    }
    else {
        bi->b.no = -1;
    }

    return &bi->b;
}
Esempio n. 18
0
int main(void) {
    printf("starting\n");

    struct node* head;
    head=malloc(sizeof(struct node));
    head->data=0;
    head->next=NULL;

    int i;
    for(i=0; i<10; i++) {
        printf("i: %d\n",i);
        addnode(head,i+1);
    }
    printf("starting printlist\n");
    printlist(head);

    int index_fromend=3;
    struct node* rec_ptr=Nthfromend(head,index_fromend);
    if(rec_ptr!=NULL) {
        printf("%dth data from end: %d\n",index_fromend,rec_ptr->data);
    }


    for(i=0; i<10; i++) {
        random_addnode(head);
    }

    printlist(head);



    return 0;

}
Esempio n. 19
0
File: main.c Progetto: aahud/harvey
static Filter*
fillin(Filter *f, Proto *last)
{
	int i;
	Filter *nf;

	/* hack to make sure top level node is the root */
	if(last == nil){
		if(f->pr == root)
			return f;
		f = fillin(f, root);
		if(f == nil)
			return nil;
		return addnode(f, root);
	}

	/* breadth first search though the protocol graph */
	nf = f;
	for(i = 1; i < 20; i++){
		nf = _fillin(f, last, i);
		if(nf != nil)
			break;
	}
	return nf;
}
Esempio n. 20
0
void print_pid_openfiles(struct text_object *obj, char *p, int p_max_size) {
	DIR* dir;
	struct dirent *entry;
	char buf[p_max_size];
	int length, totallength = 0;
	struct ll_string* files_front = NULL;
	struct ll_string* files_back = NULL;

	dir = opendir(obj->data.s);
	if(dir != NULL) {
		while ((entry = readdir(dir))) {
			if(entry->d_name[0] != '.') {
				snprintf(buf, p_max_size, "%d/%s", strtopid(obj->data.s), entry->d_name);
				length = readlink(buf, buf, p_max_size);
				buf[length] = 0;
				if(inlist(files_front, buf) == 0) {
					files_back = addnode(files_back, buf);
					snprintf(p + totallength, p_max_size - totallength, "%s; " , buf);
					totallength += length + strlen("; ");
				}
				if(files_front == NULL) {
					files_front = files_back;
				}
			}
		}
		closedir(dir);
		freelist(files_front);
		p[totallength - strlen("; ")] = 0;
	} else {
		p[0] = 0;
	}
}
Esempio n. 21
0
void BST<elemtype>::addnode(nodeT *&t,elemtype a){
	if(t==NULL)
	{
		t=new nodeT;
		t->key=a;
		t->left=NULL;
		t->right=NULL;
	}else if(a==t->key)
	{
		cout<<"ALREADY THERE "<<endl;
	}else if(a<t->key)
	{
		addnode(t->left,a);
	}else {
		addnode(t->right,a);
	}
}
void addnode(NODE **root, WORD ch)
{
	if(*root == NULL)
	{
		NODE *tmp;
		tmp = (NODE*) malloc(sizeof(NODE));
		tmp->ch = strndup(ch.ch, ch.len);
		tmp->freq = 1;
		*root = tmp;
	}
	else if( strncmp((*root)->ch, ch.ch, ch.len) < 0) 
		addnode(&(*root)->left, ch);
	else if( strncmp((*root)->ch, ch.ch, ch.len) > 0) 
		addnode(&(*root)->right, ch);
	else
		(*root)->freq++;
}
Esempio n. 23
0
/*-------------------------------------------------------------------*/
void main()
{
int a;
clrscr();
createlist(a);
addnode(a);
addnode(a);
addnode(a);
display();
inserthead(a);
display();
inserttail(a);
display();
delhead();
display();
deltail();
display();
getch();
}
Esempio n. 24
0
///adds node to binary tree. checks whether word is already in tree and increments it.
///if not it creates a node to the left node or right node of the current root depeding on the word
struct node *addnode(struct node *p, char *w)
{
    int cond;

    if (p == NULL)
    {
        p = talloc();
        p->word = strdupl(w);
        p->count = 1;
        p->left = p->right = NULL;
    }
    else if ((cond = strcmp(w, p->word)) == 0)
        p->count++;
    else if (cond < 0)
        p->left = addnode(p->left, w);
    else
        p->right = addnode(p->right, w);
    return p;
}
Esempio n. 25
0
void		addnode(t_list **s, char **name)
{
	if (*s == NULL)
	{
		*s = (t_list *)malloc(sizeof(t_list));
		(*s)->content = copy_env((*s)->content, name);
		(*s)->next = NULL;
	}
	else
		addnode(&((*s)->next), name);
}
Esempio n. 26
0
inline void insert_node(int x,int y)
{
 s[y]++;
 addnode(node);
 row[node]=x;
 col[node]=y;
 insert_col(y,node);
 if(head[x]==-1) head[x]=node;
 else
  insert_row(head[x],node);
}
Esempio n. 27
0
int addnode (tnode ** root, int * a ) {
    if (*root==NULL) {
        tnode * node=nalloc (a);
        *root= node;
        return 1;
        }
    int res=compare (a,(*root)->matrix,9);
    if (res==0){
        return 0;
    }
    if (res<0) {
        tnode ** z = &((*root)->left);
        return addnode ( z,a);
    }
    else {
        tnode ** z = &((*root)->right);
        //puts("pass it");
        return addnode ( z,a);
    }
}
Esempio n. 28
0
File: tools.c Progetto: j13s/devil
/* set/change the cube for the thing t if necessary. */
void setthingcube(struct thing *t) {
    struct node *n;
    int w;


    /* check if the thing is still in the same cube */
    if ( t->nc && checkpntcube(t->nc, &t->p[0]) ) {
        return;
    }

    /* if the thing was in a cube, remove it from the list */
    if (t->nc) {
        for (n = t->nc->d.c->things.head; n->next != NULL; n = n->next) {
            if (n->d.t == t) {
                break;
            }
        }

        if (n->next != NULL) {
            unlistnode(&t->nc->d.c->things, n);

            /* check the neighbours */
            for (w = 0; w < 6; w++) {
                if ( t->nc->d.c->nc[w] &&
                    checkpntcube(t->nc->d.c->nc[w], &t->p[0]) ) {
                    break;
                }
            }

            t->nc = (w < 6 ? t->nc->d.c->nc[w] : NULL);
        }
        else {
            t->nc = NULL;
        }
    }
    else {
        n = NULL;
    }

    /* still no cube found? then brute force: Check all cubes */
    if (!t->nc) {
        t->nc = findpntcube(&l->cubes, &t->p[0]);
    }

    /* if the thing is in a new cube add it to the list */
    if (t->nc) {
        if (n) {
            listnode_tail(&t->nc->d.c->things, n);
        }
        else {
            addnode(&t->nc->d.c->things, -1, t);
        }
    }
}
Esempio n. 29
0
int main(){
	struct tnode *root = NULL;

	char ch, fakech, quit = 1;
	int n;
	do{
		printf("\n");
		printf("1 - Add\n");
		printf("2 - Inorder Print\n");
		printf("3 - Preorder Print\n");
		printf("4 - Delete Tree\n");
		printf("0 - Quit\n");
		printf("\n");
		printf("Enter choice: ");

		scanf("%c", &ch);
		scanf("%c", &fakech);

		switch(ch){
			case '1':
				printf("Enter element: ");
				scanf("%d", &n);
				scanf("%c", &fakech);

				root = addnode(root, n);

				break;

			case '2':
				printf("Tree: ");
				inorder(root);
				printf("\n");
				break;

			case '3':
				printf("Tree: ");
				preorder(root);
				printf("\n");
				break;

			case '4':
				deltree(root);
				root = NULL;
				printf("Deleted!\n");
				break;

			case '0':
				quit = 0;
				break;
		}
	} while(quit);

	return 0;
}
Esempio n. 30
0
/* addtree: add a node with w, at or below p */
struct tnode *addtree(struct tnode *p, char *w, int line_no)
{
	int cond;

	if (p == NULL) {		/* a new word has arrived */
		p = talloc();		/* made a new node */
		p->word  = str_dup(w);
		p->count = 1;
		p->left = p->right = NULL;
		p->head = NULL;
		addnode(p, line_no);
	} else if ((cond = strcmp(w, p->word)) == 0) {
		p->count++;		/* repeated word */
		addnode(p, line_no);
	} else if (cond < 0) 	/* less than into left sub tree */
		p->left = addtree(p->left, w, line_no);
	else				/* greater than into right subtree */
		p->right = addtree(p->right, w, line_no);
	return p;
}