Пример #1
0
void delete_elem(node** t, int val) {
	if(!(*t))
		return;
	if((*t)->data < val)
		delete_elem(&(*t)->right, val);
	else if((*t)->data > val)
		delete_elem(&(*t)->left, val);
	else {
		if((*t)->dup_count)							// there were no duplicates
			--(*t)->dup_count;
		else {
			node* del;
			if(!(*t)->left) {
				del = (*t);
				(*t) = (*t)->right;
				free(del);
			}
			else if(!(*t)->right) {
				del = (*t);
				(*t) = (*t)->left;
				free(del);
			}
			else {
				del = find_min((*t)->right);
				(*t)->data = del->data;
				delete_elem(&(*t)->right, del->data);
			}
		}
	}
}
Пример #2
0
int delete_from_buffer(bool right) {
	if(right) {
		forward_buffer();
	} else {
		wprintf(L"\033[1D");
	}

	if(buffer->current == NULL) {
		return RET_OK;
	} else {
		wprintf(L"\033[K");
	}
	
	delete_elem(buffer);

	if(buffer->current == NULL) {
		print_buffer(true);
		wprintf(L"\033[1D");
		return RET_OK;
	}

	if(buffer->current->next != NULL) {
		forward_buffer();
		print_buffer(true);
		backward_buffer();
		wprintf(L"\033[1D");
	}

	return RET_OK;
}
Пример #3
0
int free_list(List *list) {
	while(list->queue != NULL) {
		delete_elem(list);
	}

	return RET_OK;
}
Пример #4
0
static
void delete_list(list_t *list)
{
    elem_t *iter = list->head;
    while (iter != NULL) {
        elem_t *next = iter->next;
        delete_elem(iter);
        iter = next;
    }

    dr_global_free(list, sizeof(list_t));
}
Пример #5
0
t_list		*delete_elem(t_list *list, char *data)
{
  t_list	*tmp_next;

  if (list == NULL)
    return (NULL);
  if (list->data == data)
    {
      tmp_next = list->next;
      return (tmp_next);
    }
  list->next = delete_elem(list->next, data);
  return (list);
}
Пример #6
0
int main() {
	srand(time(NULL));
	node* tree = create_bst(40);
	for(uint i = 0; i < 10; ++i) {
		int elem = rand() % 100 + 1;
		insert(&tree, elem);
	}
	display_tree(tree);
	
	int input;
	do {
		printf("Enter value to delete: ");
		scanf("%d", &input);
		delete_elem(&tree, input);
		display_tree(tree);
	} while(input != -1);
	
	return 0;
}
Пример #7
0
void format(struct item *head)
{
    struct item *elem1 = head;
    struct item *elem2 = head->next;
    while (elem1 != NULL && elem1->next != NULL)
    {
        elem2 = elem1->next;
        while (elem2 != NULL)
        {
            if (elem1->power == elem2->power)
            {
                elem1->num = elem1->num + elem2->num;
                struct item *prev = find_prev(head, elem2);
                delete_elem(&head, elem2);
                elem2 = prev->next;
            }
            else
                elem2 = elem2->next;
        }
        elem1 = elem1->next;
    }
}
Пример #8
0
int		ft_unset(char **scmd, t_env **env, t_cmd *cmd, t_data *data)
{
	int		ret;

	if (!scmd[1])
	{
		putstr_builtin(cmd, "unset: Too few arguments.\n", 2);
		return (1);
	}
	ret = 1;
	if (isset_arg(*env, scmd[1]))
	{
		delete_elem(env, scmd[1]);
		ret = 0;
	}
	if (isset_var(data->var, scmd[1]))
	{
		delete_elem_var(&(data->var), scmd[1]);
		ret = 0;
	}
	return (ret);
}
Пример #9
0
void recieveUpdate(){
  struct sockaddr_in addr;
  int fd, nbytes,addrlen;
  struct ip_mreq mreq;
  char buf[BUF_SIZE];
  
  u_int yes=1;        
  
  /* create what looks like an ordinary UDP socket */
  if ((fd=socket(AF_INET,SOCK_DGRAM,0)) < 0) {
    perror("socket");
    exit(1);
  }
  
  /**** MODIFICATION TO ORIGINAL */
  /* allow multiple sockets to use the same PORT number */
  if (setsockopt(fd,SOL_SOCKET,SO_REUSEADDR,&yes,sizeof(yes)) < 0) {
    perror("Reusing ADDR failed");
    exit(1);
  }
  /*** END OF MODIFICATION TO ORIGINAL */
  
  /* set up destination address */
  memset(&addr,0,sizeof(addr));
  addr.sin_family=AF_INET;
  addr.sin_addr.s_addr=htonl(INADDR_ANY); /* N.B.: differs from sender */
  addr.sin_port=htons(PORT);
  
  /* bind to receive address */
  if (bind(fd,(struct sockaddr *) &addr,sizeof(addr)) < 0) {
    perror("bind");
    exit(1);
  }
  
  printf("\nlisten on socket UDP %i\n", PORT); 
  
  /* use setsockopt() to request that the kernel join a multicast group */
  mreq.imr_multiaddr.s_addr=inet_addr(DST_IP);
  mreq.imr_interface.s_addr=htonl(INADDR_ANY);
  if (setsockopt(fd,IPPROTO_IP,IP_ADD_MEMBERSHIP,&mreq,sizeof(mreq)) < 0) {
    perror("setsockopt");
    exit(1);
  }
  
  /* now just enter a read-print loop */
  while (1) {
    addrlen=sizeof(addr);
    if ((nbytes=recvfrom(fd,buf,BUF_SIZE,0,(struct sockaddr *) &addr,&addrlen)) < 0) {
      perror("recvfrom");
      exit(1);
    }
    printf("recived RIP update from %s\n", intToIp(addr.sin_addr.s_addr));
    int entrySize = sizeof(entry)-sizeof(UT_hash_handle);
    int i;
    int numOfEntries = (nbytes - sizeof(header))/entrySize;
    for(i = 0; i < numOfEntries; i++){
      entry *e = malloc(entrySize);
      memcpy(e, &buf[4+i*entrySize], entrySize);
      uint32_t network = e->network_addr;
      struct entry *entr = find_elem(network);
      if(entr == NULL && e->metric <= 15){
	sem_wait(&mutex); 
	add_elem(entr);
	printf("%s/%s Metric %u",intToIp(entr->network_addr), intToIp(entr->subnet_mask), entr->metric);
	sem_post(&mutex); 
      }else{
	if(entr->network_addr == 2130706432) continue;
	if(e->metric < entr->metric && e->metric <= 15){
	  sem_wait(&mutex);
	  printf("%s/%s Metric %u",intToIp(entr->network_addr), intToIp(entr->subnet_mask), entr->metric);
	  printf("found better route to %s %s Metric %u through %s\n", intToIp(entr->network_addr), intToIp(entr->subnet_mask), entr->metric, intToIp(e->next_hop));
	  delete_elem(entr);
	  add_elem(e);
	  sem_post(&mutex); 
	}
      }
    }
  }
}
Пример #10
0
int main(int argc, char *argv[]) {
	int ch;
	int k_size=0,val_size=0;
	char* k=NULL,*val=NULL;
	do{
		printf("Choose the option:\n");
		printf("1.Create new table\n");
		printf("2.Insert new element\n");
		printf("3.Search element\n");
		printf("4.Get hashed value of a key\n");
		printf("5.Delete table\n");
		printf("6.Print key-value pairs of the table\n");
		printf("7.Delete element\n");
		scanf("%d",&ch);
		printf("You entered\t%d\n",ch);
		switch(ch){
			case 1:
				if(ht)
				free(ht);
				ht=(HT*)malloc(table_size*sizeof(HT));
				for(ch=0;ch<table_size;ch++)
				ht[ch].coll_res=-1;
				if(!ht){
					printf("Error allocating memory for table\n");
					break;
				}else
				printf("Empty Table of size \t%d\tcreated\n",table_size);
				break;
			case 2:
				if(!ht){
					printf("First create empty table\n");
					break;
				}
				/*printf("Enter the maximum sizes of key and value\n");
				scanf("%d%d",&k_size,&val_size);*/
				k=(char*)malloc((k_size+1)*sizeof(char));
				val=(char*)malloc((val_size+1)*sizeof(char));
				if(k && val){
						printf("Enter key-value pair to insert\n");
						scanf("%s%s",k,val);
						elem1=(elem*)malloc(sizeof(elem));
						if(!elem1){
							printf("Error allocating memory for new  element\n");
							break;
						}
						elem1->key=k;
						elem1->value=val;
						insert(elem1);
					break;
				}else{
				printf("Error allocating memories for key-value pair\n");
				break;
			    }
			case 3:
				 if(!ht){
					printf("First create empty table\n");
					break;
					}
				/*printf("Enter the maximum size of key to be searched\n");
				scanf("%d",&k_size);*/
				k=(char*)malloc((k_size+1)*sizeof(char));
				printf("Enter key to search for element\n");
				scanf("%s",k);
				search(k);
				break;
			case 4:
				 if(!ht){
					printf("First create empty table\n");
					break;
					}
				/*printf("Enter the maximum size of key \n");
				scanf("%d",&k_size);*/
				k=(char*)malloc((k_size+1)*sizeof(char));
				printf("Enter key to get its hashed value\n");
				scanf("%s",k);
				int hash=0;
				printf("Hashed Value of the key \t%s\t is\t%d\n",k,h(k));
				break;
			case 5: 
				if(!ht){
					printf("First create empty table\n");
					break;
					}
				if(ht){
				for(ch=0;ch<table_size;ch++)
				{
				if(ht[ch].el!=NULL)
					{
					   if(ht[ch].el->key!=NULL)
						free(ht[ch].el->key);
					   if(ht[ch].el->value!=NULL)
						free(ht[ch].el->value);
					 ht[ch].el=NULL;
					}
				}
				ht=NULL;
				      }
				if(ht){
					printf("Error deleting table,Try again...\n");
					break;
				}else{
					elem_count=0;
					table_size=0;
					printf("Table deleted\n");
					break;
				}
			case 6:
				if(!ht){
					printf("First create empty table\n");
					break;
					}
				print_key_value_pairs();
				break;
			case 7:
				if(!ht){
					printf("First create empty table\n");
					break;
					}
				/*printf("Enter the maximum size of key to be deleted\n");
				scanf("%d",&k_size);*/
				k=(char*)malloc((k_size+1)*sizeof(char));
				printf("Enter key to be deleted for element\n");
				scanf("%s",k);
				delete_elem(k);
				break;
			default:
				printf("Exiting.\n");
				return 0;
		}
	}while(true);
	return 0;
}