Ejemplo n.º 1
0
void input_deque()
{ int option,val;
 clrscr();
do{ printf("\n ********** input deque menu ********");
 printf("\n 1.insert at right");
 printf("\n 2. delete from left");
 printf("\n 3. delete from right");
 printf("\n 4. display");
 printf("\n enter your choice");
 scanf("%d",&option);
 switch(option)
 { case 1: printf("\n enter value to be inserted");
	   scanf("%d",&val);
	   insert_r(val);
	   break;
   case 2: val=delete_f();
	   if(val!=-999)
	   printf("\n value deleted from left is = %d",val);
	   break;
   case 3: val=delete_r();
	   if(val!=-999)
	   printf("\n value deleted from right is = %d",val);
	   break;
   case 4: display();
	   break;

   default: printf("\n wroung statement");
 }
}while(option<=4 && option>=1);
}
Ejemplo n.º 2
0
spltreeNode_t* cdsl_spltreeDelete(spltreeRoot_t* root, trkey_t key) {
	if (!root)
		return NULL;
	uint8_t context = ROOT;
	spltreeNode_t* delNode = NULL;
	root->entry = delete_r(root->entry, key, &context, &delNode);
	return delNode;
}
Ejemplo n.º 3
0
static spltreeNode_t* delete_r(spltreeNode_t* current, trkey_t key,	uint8_t* context, spltreeNode_t** found) {
	if (!current) {
		*context = NOT_FOUND;
		*found = NULL;
		return NULL;
	}
	if (current->key == key) {
		*found = current;
		if (current->left) {
			current = largest_r(current->left, context, FALSE, TRUE);
			*context = FOUND;
			if (current == (*found)->left) {
				current->right = (*found)->right;
				return current;
			}
		} else if (current->right) {
			current = smallest_r(current->right, context, FALSE, TRUE);
			*context = FOUND;
			if (current == (*found)->right) {
				current->left = (*found)->left;
				return current;
			}
		} else {
			*context = FOUND;
			return NULL;
		}
		current->left = (*found)->left;
		current->right = (*found)->right;
		return current;
	}
	uint8_t direction = *context;
	if (current->key < key) {
		*context = RIGHT;
		current->right = delete_r(current->right, key, context, found);
		if (!current->right){
			return current;
		}

		switch (*context) {
		case NOT_FOUND:
			return current;
		case ZIGZIG_STEP:
			*context = FOUND;
			return rotateLeft(rotateLeft(current));
		case ZIGZAG_STEP:
			*context = FOUND;
			current->right = rotateRight(current->right);
			return rotateLeft(current);
		}
		if (direction == ROOT) {
			return rotateLeft(current);
		}
		if (*context == FOUND) {
			*context = (direction == RIGHT ? ZIGZIG_STEP : ZIGZAG_STEP);
			return current;
		}
	} else {
		*context = LEFT;
		current->left = delete_r(current->left, key, context, found);
		if (!current->left) {
			return current;
		}

		switch (*context) {
		case NOT_FOUND:
			return current;
		case ZIGZIG_STEP:
			*context = FOUND;
			return rotateRight(rotateRight(current));
		case ZIGZAG_STEP:
			*context = FOUND;
			current->left = rotateLeft(current->left);
			return rotateRight(current);
		}
		if (direction == ROOT)
			return rotateRight(current);
		if (*context == FOUND) {
			*context = (direction == LEFT ? ZIGZIG_STEP : ZIGZAG_STEP);
			return current;
		}
	}
	return NULL;
}