コード例 #1
0
ファイル: btree.c プロジェクト: algking/algorithms-in-c
static void delnode(link_t root, item_t key)
{
    if (root == NULL) {
        return;
    }

    if (root->left) {
        if (internal_node(root->left)) {
            delnode(root->left, key);
        } else if (external_node(root->left) && root->left->item == key) {
            dellink(&root->left);
        }
    }

    if (root->right) {
        if (internal_node(root->right)) {
            delnode(root->right, key);
        } else if (external_node(root->right) && root->right->item == key) {
            dellink(&root->right);
        }
    }

    if (external_node(root) && root->item == key) {
        dellink(&root);
    }
}
コード例 #2
0
ファイル: ms-buffer.c プロジェクト: gokzy/netbsd-src
/* The input function for a log buffer.  */
static int
ms_buffer_input (void *closure, char *data, size_t need, size_t size,
		 size_t *got)
{
    struct ms_buffer *mb = closure;
    int status;

    assert (mb->cur->input);
    status = (*mb->cur->input) (mb->cur->closure, data, need, size, got);
    if (status == -1)
    {
	Node *p;
	/* EOF.  Set up the next buffer in line but return success and no
	 * data since our caller may have selected on the target to find
	 * ready data before calling us.
	 *
	 * If there are no more buffers, return EOF.
	 */
	if (list_isempty (mb->bufs)) return -1;
	buf_shutdown (mb->cur);
	buf_free (mb->cur);
	p = mb->bufs->list->next;
	mb->cur = p->data;
	p->delproc = NULL;
	p->data = NULL;
	delnode (p);
	if (!buf_empty_p (mb->cur)) buf_append_buffer (mb->buf, mb->cur);
	ms_buffer_block (closure, mb->block);
	*got = 0;
	status = 0;
    }

    return status;
}
コード例 #3
0
ファイル: happybirthday.cpp プロジェクト: AlanYume/ACM-ICPC
void maintainlist(int b){
	int t;
	while(b!=-1 && (t=next[b])!=-1 && bcount[b] + bcount[t] < BLOCKSIZE){
		memcpy(data[b]+bcount[b], data[t], bcount[t]*sizeof(int));
		bcount[b]+=bcount[t];
		next[b]=next[t];
		delnode(t);
	}
}
コード例 #4
0
void maintainlist(int b){
	for(; b!=-1; b=next[b])
		for(int t=next[b]; t!=-1 && count[b] + count[t] <= BLOCKSIZE; t=next[b]){
			memcpy(data[b]+count[b], data[t], count[t]*sizeof(int));
			count[b]+=count[t];
			next[b]=next[t];
			delnode(t);
		}
}
コード例 #5
0
ファイル: node.c プロジェクト: 8l/scc
void
deltree(Node *np)
{
	if (!np)
		return;
	deltree(np->left);
	deltree(np->right);
	delnode(np);
}
コード例 #6
0
ファイル: stack.c プロジェクト: algking/algorithms-in-c
int stack_pop(T t)
{
    int reval;
    L tmp;
    assert(!stack_empty(t));
    reval = t->head->entry;
    tmp     = t->head;
    t->head = tmp->next;
    delnode(tmp);
    return reval;
}
コード例 #7
0
ファイル: stack.c プロジェクト: algking/algorithms-in-c
void stack_finalize(T t)
{
    L tmp;

    while ((tmp = t->head) != NULL) {
        t->head = tmp->next;
        delnode(tmp);
    }

    free(t);
}
コード例 #8
0
int erase(int p,int n){
	int b,e;
	find(p,b);
	splite(b,p);
	for(e=next[b]; e!=-1 && n > count[e]; e=next[e])n -= count[e];
	if(n){splite(e,n);e=next[e];}
	for(int t=next[b]; t!=e; t=next[b]){
		next[b]=next[t];
		delnode(t);
	}
	maintainlist(b);
}
コード例 #9
0
void xoatrung(LIST &l)
{
	NODE *p=l.pHead;
	NODE *q;
	while (p)
	{
		q=timtrung(p->pNext,p->info);
		while (q)
		{
			delnode(l,q);
			q=timtrung(p->pNext,p->info);
		}
		p=p->pNext;
	}
}
コード例 #10
0
ファイル: necklace.cpp プロジェクト: 1code/algorithm-oeddyo
void maintainlist(int b){
	for(; b!=-1; b=next[b])
		for(int t=next[b]; t!=-1 && count[b]+count[t]<=BLOCKSIZE; t=next[b]){
			if( !(same[b] && same[t] && samevalue[b]==samevalue[t]) ){
				reverseblock(b);
				reverseblock(t);
				if(same[b])for(int i=count[b]-1; i>=0; --i)data[b][i]=samevalue[b];
				same[b]=false;
				int cb=count[b],*str=data[b];
				if(same[t])for(int i=count[t]-1,sv=samevalue[t];i>=0;--i)str[cb+i]=sv;
				else for(int i=count[t]-1,*a=data[t];i>=0;--i)str[cb+i]=a[i];
			}
			count[b]+=count[t];
			next[b]=next[t];
			delnode(t);
			maintainblock(b);
		}
}
コード例 #11
0
ファイル: stack.c プロジェクト: AhmadTux/DragonFlyBSD
static void *
do_shift (List *stack, int isstring)
{
    void *elem;

    if (isempty (stack)) return NULL;

    if (isstring)
    {
	elem = stack->list->next->key;
	stack->list->next->key = NULL;
    }
    else
    {
	elem = stack->list->next->data;
	stack->list->next->data = NULL;
    }
    delnode (stack->list->next);
    return elem;
}
コード例 #12
0
static uint64_t flush_node (FILE *fp, int features, struct memnode *mp, uint64_t *cntp)
{
    uint64_t curpos, refpos, pos, cnt, tcnt;
    struct memnode *tp;

    if (mp == NULL) return 0;

    tp  = mp;

    while (tp) {
        if (tp->child) {
            tp->childpos = flush_node (fp, features, tp->child, &(tp->subcnt));
            tp->child    = NULL;
        }
        tp   = tp->sib;
    }

    curpos = ftell64 (fp);
    pos    = curpos;
    refpos = pos;
    cnt    = 0;

    while (mp) {
        tcnt = mp->subcnt;
        if (mp->end) tcnt++;

        trie_node_write (fp, features, &refpos, &pos, tcnt, mp->len, mp->end, mp->childpos, mp->dat);

        cnt += tcnt;

        tp   = mp;        
        mp   = mp->sib;
        delnode (tp);
    }
    trie_node_write (fp, features, NULL, NULL, 0, 0, 0, 0, NULL);

    *cntp = cnt;

    return curpos;
}
コード例 #13
0
ファイル: btree.c プロジェクト: algking/algorithms-in-c
void btree_del_node(T t, item_t key)
{
    delnode(t->root, key);
}
コード例 #14
0
int main()  
{  
   int i;  
   Head=NULL;  
  
   while(1)   
   {  
      printf(" \nInsert a number \n1. At Beginning");  
      printf(" \n2. At End");  
      printf(" \n3. At a Particular Location in List");  
      printf(" \n\n4. Print the Elements in the List");  
      printf(" \n5. Print number of elements in the List");  
      printf(" \n6. Delete a Node in the List");  
      printf(" \n7. Reverse the linked List");  
      printf(" \n8. Exit");  
      printf(" \n\nChoose Option: ");  
      scanf("%d",&i);   
  
      switch(i)  
      {  
         case 1:  
        {  
            int num;  
            printf(" \nEnter the Number to insert: ");  
            scanf("%d",&num);  
            addbeg(num);  
            break;  
        }  
         case 2:  
         {  
            int num;  
            printf(" \nEnter the Number to insert: ");  
            scanf("%d",&num);  
            append(num);  
            break;  
         }  
  case 3:  
       {  
           int num, loc, k;  
           printf("\nEnter the Number to insert: ");  
           scanf("%d",&num);  
           printf("\nEnter the location Number: ");  
           scanf("%d",&loc);  
            addafter(num,loc);  
           break;  
      }    
  case 4:  
  {  
     printf(" \nElements in the List: ");  
            display();  
            break;  
        }  
  case 5:  
  {  
     display();  
           printf(" \nTotal number of Elements in the List: %d",count());  
     break;  
     }   
  case 6:  
  {  
     int num;  
           printf(" \nEnter the number to be deleted from List: ");  
           scanf("%d",&num);  
           delnode(num);  
          break;  
      }  
  case 7:  
  {  
     reverse();  
            display();  
            break;  
      }  
    case 8:  
   {  
     struct node *temp;  
      
       while( Head!=NULL)  
     {  
        temp = Head->next;  
        free(Head);  
        Head=temp;  
     }  
       exit(0);  
   }  
  default:   
  {  
     printf("\nWrong Option choosen");  
         }  
      }/* end if switch */  
   }/* end of while */  
}/* end of main */
コード例 #15
0
void main ( int argc, char **argv ) {

    
    long i,a;
    char found = 0;

    char *nome_campo;

    FILE *fp;
    if (argc == 3) {   //
        printf("Usage: ./insert_into <tablename> <FieldX> <ValueX>\n");
        printf("OR\n");
        printf("Usage: ./insert_into <tablename>\n");
        return;
    }

    table *tmpTable;

    read_header(argv[1],&tmpTable);

    if (argc == 2) {   //
        printf("[TABLE: %s; numFields: %i]\n",tmpTable->name, tmpTable->numFields);
        for (i=0;i<tmpTable->numFields;i++) {
            printf("[FIELD: %s; fieldSize: %i]\n",(tmpTable->fields[i]).name, (tmpTable->fields[i]).fieldSize);
        }
        free(tmpTable);
        return;
    }
    
    table_row *row;

    if (hasIndex(tmpTable,argv[2])) {
        printf("[FIELD: %s IS INDEXED!]\n",argv[2]);
        rdstart();
        i = search(argv[3]);
        fclose(fptree);
    } else {
        i = 0;
    }
    
    if (i == -1) {
        printf("Nenhum registro encontrado!\n");
    }

    for (;i>-1;i++) {
        row = get_row(tmpTable,i);
        if (row == NULL) {
            if (found == 0) {
                printf("Nenhum registro encontrado!\n");
            }
            break;
        }
        if (strcmp(get_row_field(tmpTable,row,argv[2]),argv[3]) == 0) {
            found = 1;
            delete_row(tmpTable,i);
            for (a=0;a<tmpTable->numFields;a++) {
                if (hasIndex(tmpTable,(tmpTable->fields[a]).name)) {
                    rdstart();
                    delnode(get_row_field(tmpTable,row,(tmpTable->fields[a]).name));
                    printtree(root);
                    wrstart();
                    fclose(fptree);
                }
            }
            free(row);
            break;
        }
        free(row);
    }

    free(tmpTable);

    return;
}
コード例 #16
0
/*
 * This addline version assumes sorted input. This means that whenever
 * a split occurs all subtrees can be flushed.
 */
static void addline_trie (FILE *fp, int features, struct memnode **mpp, unsigned char *dat, int len)
{
    /* Previous sib. Points to the last sib */
    /* visited, or NULL is this is the first*/
    /* sib in the node.                     */
    struct memnode *prvsibp = NULL;

    while (len && dat[len-1] == '\n') len--;

    if (!len) return;

    for (;;) {
        int l1, l2, ct;
        unsigned char *d1, *d2;
        struct memnode *mp, *n1, *n2;

        mp = *mpp;
        /* In case of an empty trie: build one */
        if (mp == NULL) {
            mp = newnode (dat, len);
            mp->end = 1;
            *mpp = mp;
            /* As the input is sorted, we won't*/
            /* change the older sibs! So flush */
            /* and forget prvsibp's subtrie    */
            if (prvsibp) {
                prvsibp->childpos = flush_node (fp, features, prvsibp->child, &(prvsibp->subcnt));
                prvsibp->child    = NULL;
                fflush (fp);
            }
            return;
        }

        /* Move to the next sib if appropriate */
        if (len && dat[0] > mp->dat[0]) {
            prvsibp = mp;
            mpp     = &(mp->sib);
            continue;
        }

        /* Introduce a new sib if appropriate  */
        if (len && dat[0] < mp->dat[0]) {
            abort_error ();
        }
        /* We found the right sib */
        ct = 0;
        d1 = dat;
        l1 = len;
        d2 = mp->dat;
        l2 = mp->len;

        /* At what pos do the sib and the new  */
        /* value differ                        */
        while (l1 && l2 && *d1 == *d2) {
            ct++;
            d1++;
            l1--;
            d2++;
            l2--;
        }

        if (!l2) {
            /* No sib string data left         */
            if (!l1) {
                /* No input string left as well*/
                /* So we have a match!         */

                if (mp->child || mp->childpos) {
                    abort_error ();
                }
                mp->end = 1;
                return;
            }
            /* Some input string data is left  */
            /* so the string is a child of sib   */
            /* Continue there                  */
            mpp     = &(mp->child);
            dat     = d1;
            len     = l1;
            prvsibp = NULL;
            continue;
        }
        /* There's unmatched sib string data   */
        /* left, so split the node             */

        if (!l1) {
            abort_error ();
        }

        /* New node n2 is the non matching tail*/
        /* of mp                               */
        n2 = newnode (d2, l2);
        n2->child = mp->child;
        n2->end   = mp->end;

        /* New node n1 replaces mp, which has  */
        /* The prefix length ct of mp          */
        n1 = newnode (mp->dat, ct);
        n1->sib   = mp->sib;
        n1->child = n2;
        *mpp = n1;
        /* mp is obsolete now                  */
        delnode (mp);

        /* If there's no input string data left*/
        /* Then the mp replacement n1 is an    */
        /* endpoint!                           */
        if (!l1) {
            n1->end = 1;
            return;
        };

        /* Now move on to the child, where the   */
        /* remainder of the input string will  */
        /* reside                              */
        mpp     = &(n1->child);
        dat     = d1;
        len     = l1;
        prvsibp = NULL;
    }
}
コード例 #17
0
ファイル: props.c プロジェクト: fuzzball-muck/fuzzball
/**
 * Delete a property from the given prop set, with the given property
 * name.  It removes it from the AVL of 'list'.  Does not save it to
 * the database right away.
 *
 * @param list Pointer to a PropPtr which is (usually) the root of the
 *             property AVL tree.
 * @param name The name of the property to delete
 * @return Returns the pointer that 'list' is pointing to.  Because 'list'
 *         is modified, there is probably no reason to use the return
 *         value.
 */
PropPtr
delete_prop(PropPtr * list, char *name)
{
    *list = delnode(name, *list);
    return (*list);
}
コード例 #18
0
ファイル: main.c プロジェクト: aagontuk/assignments
int main(){
	struct node *head = NULL;

	int n;
	char ch, fakech, quit = 1;
	do{
		printf("\n");
		printf("1 - Insert\n");
		printf("2 - Display\n");
		printf("3 - Find\n");
		printf("4 - Delete\n");
		printf("5 - Delete All\n");
		printf("0 - Quit\n");
		printf("\n");
		printf("Enter choice: ");
		
		scanf("%c", &ch);
		scanf("%c", &fakech);

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

				printf("Add front or back?(f/b): ");
				scanf("%c", &ch);
				scanf("%c", &fakech);

				if(ch == 'f'){
					head = addfront(head, n);
				}
				else{
					head = addback(head, n);
				}

				break;

			case '2':
				display(head);
				break;

			case '3':
				printf("Find: ");
				scanf("%d", &n);
				scanf("%c", &fakech);

				printf("Result: %p\n", find(head, n));

				break;

			case '4':
				printf("Delete: ");
				scanf("%d", &n);
				scanf("%c", &fakech);

				struct node *p;
				if((p = find(head, n)) != NULL){
					head = delnode(head, p);
				}
				else{
					printf("Not found in the list!\n");
				}

				break;

			case '5':
				freelist(head);
				head = NULL;
				break;

			case '0':
				quit = 0;
				break;
		}

	} while(quit);

	return 0;
}
コード例 #19
0
void main()
{
    int ch=0,ans=0;
    int val=0;
    char str[15];
    struct node *new1,*new2;


    clrscr();

    /* initialization of list with the existing records */

    fp=fopen("phone.txt","r");

    if(fp==NULL)
    {
        printf("File cant be opened !");
        getch();
        exit(1);
    }

    while(!feof(fp))
    {
        new1=getrecord();

        if(insert(new1)==-1)
            printf("Cant insert record ! Error...");
        else
            printf("Record entered...");
    }
    getch();
    fclose(fp);

    do
    {
        clrscr();
        printf("Menu");
        printf("1. Add a New phone record.");
        printf("2. Delete an existing Record.");
        printf("3. show all Records.");
        printf("4. Modify a particular Record.");
        printf("5. Exit.");
        printf("Enter your choice--->");
        scanf("%d",&ch);

        switch(ch)
        {
        case 1:

            new1 = getnode();
            val = insert(new1);
            if(val==-1)
                printf("Employee id already exists ! try again...");
            else
                printf("Employee details successfully stored");
            break;

        case 2:
            printf("Enter the phone no.you wish to delete--->");
            scanf("%d",&val);
            ans=delnode(val);

            if(ans==-1)
                printf("Record doesnt exist ! Try again...");
            if(ans==0)
                printf("Record deleted !");
            break;

        case 3:
            display();
            break;

        case 4:
            printf("Search by phone no. or Name ? (1/2)--->");
            scanf("%d",&ch);
            if(ch==1)
            {
                printf("Enter the phone no. you wish to search for--->");
                scanf("%d",&val);
                new2=query(val,&str,1);
            }
            else
            {
                printf("Enter the name of the person you wish to search for--->");
                scanf("%s",&str);
                new2=query(val,&str,0);
            }
            if(new2)
            {
                printf("Enter the New name of the person--->");
                scanf("%s",&new2->name);
                printf("Enter new phone no. of the person--->");
                scanf("%d",&new2->num);
                printf("Record modified successfully !");
            }
            break;
        case 5:
            printf("Leaving Database,writing back to file...");
            CopyToFile();
            getch();
            free(list);
            exit(1);
            break;
        }
        getch();
    }
    while(1);

}