Example #1
0
void practice89(list_t list)
{

	node_t* nodePtr;

	printf("\n값이 40인 노드 삭제하는 함수\n헤드가 없을때까지 계속 헤드를 삭제하는 함수\n\n");
	printf("전체 리스트\n");
	printAllNodes(&list);
	nodePtr = searchNode(&list, 40);
	deleteNode(&list, findPrevNode(&list, nodePtr), nodePtr);
	printf("값이 40인 노드 삭제\n");
	printAllNodes(&list);
	printf("헤드가 없어질때까지 헤드를 삭제\n");
	while (list.head)
	{
		deleteNode(&list, (node_t*)NULL, list.head);
		printAllNodes(&list);
	}

	fflush(stdin);
	getchar();

	return;
}
Example #2
0
/* function main begins program execution */
int main(void)
{ 
   int i; /* counter to loop from 1-10 */
   int item; /* variable to hold random values */
   TreeNodePtr rootPtr = NULL; /* tree initially empty */

   srand( time( NULL ) ); 
   printf( "The numbers being placed in the tree are:\n" );

   /* insert random values between 1 and 15 in the tree */
   for ( i = 1; i <= 10; i++ ) { 
      item = 1 + rand() % 10;
      printf( "%3d", item );
      insertNode( &rootPtr, item );
   } /* end for */

   printf("\n\nInsert the value to remove: ");
   scanf("%d", &item);

   if( deleteNode( &rootPtr, item ) ) {
      printf("Value not found or value is root: %d\n", item);
      return -1;
   }

   /* traverse the tree preOrder */
   printf( "\nThe preOrder traversal is:\n" );
   preOrder( rootPtr );

   /* traverse the tree inOrder */
   printf( "\n\nThe inOrder traversal is:\n" );
   inOrder( rootPtr );

   /* traverse the tree postOrder */
   printf( "\n\nThe postOrder traversal is:\n" );
   postOrder( rootPtr );

   /* traverse the tree levelOrder */
   printf( "\n\nThe levelOrder traversal is:\n" );
   levelOrder( rootPtr );

   puts("\n\nShow the tree\n\n");
   outputTree(rootPtr, 0);

   putchar('\n');

   return 0; /* indicates successful termination */

} /* end main */
Example #3
0
void main()
{
	int i, choice = 1;
	startNode = NULL;

	while (choice != 0)
	{
		puts("Please select an option:\n");
		puts("---------------------------------------");
		puts("01\tInsert a Node");
		puts("02\tDelete a Node");
		puts("03\tDisplay all Nodes");
		puts("04\tSearch for a Node");
		puts("\n00\tExit");
		puts("---------------------------------------\n");

		fflush(stdin);
		scanf_s("%d", &choice);

		switch (choice)
		{
		case 1:
			insertNode();
			break;

		case 2:
			deleteNode();
			break;

		case 3:
			displayAll();
			break;

		case 4:
			findNode();
			break;

		case 0:
			break;

		default:
			puts("Invalid selection!\n");
			break;
		}
	}
	free(startNode);
	free(currentNode);
}
Example #4
0
int main()
{
struct node * head=NULL;

printf("hello main");



head=pushele(5,head);
head=pushele(15,head);
head=appendHead(10,head);
head=deleteNode(2,head);
printList(head);
return 0;

}
Example #5
0
//TODO
//Read List.h to help you look at the hints
//remove values which are uppercase
//
//given a list X
//(Empty List)
//then removeUpperCase(list);
//the list is still X
//
//given a list a->X
//then removeUpperCase(list);
//the list is a->X
//
//given a list A->X
//then removeUpperCase(list);
//the list is X
//
//given a list A->a->X
//then removeUpperCase(list);
//the list is a->X
//
//given a list C->a->A->D->X
//then removeUpperCase(list);
//the list is a->X
//
//given a list A->a->B->b->C->c->d->D->E->e->X
//then removeUpperCase(list);
//the list is a->b->c->d->e->X
void removeUpperCase(List l) {
   assert(l != NULL);
   Node curr = l->head;
   Node prev = NULL;
   while(curr != NULL) {
       if (curr->value >= 'A' && curr->value <= 'Z') {
           Node next = curr->next;
           deleteNode(l, prev, curr);
           curr = next;
        } else {
            prev = curr;
            curr = curr->next;
        }
   }
   printList(l);
}
Example #6
0
static void
emptyTree(pwr_tStatus *sts, tree_sTable *tp)
{
    tree_sNode *np;
    tree_sNode *mp;
  
    if (tp == NULL) return;

    for (mp = minimumNode(tp, tp->root); mp != NULL;) {
        if (mp == tp->null) return;
        np = successorNode(tp, mp);
        mp = deleteNode(tp, mp);
        freeNode(tp, mp);
	mp = np;
    }
}
Example #7
0
int main()
{
    FILE *f=fopen("input.txt","r");
    if(f==NULL)
    {
        perror("Can't open file!\n");
        return -1;
    }
    NodeT *root=createBinTree(f);
    AVLTree(root);
    root=insertNode(root,11);
    root=deleteNode(root,11);
    prettyPrint(root,0);

    return 0;
}
Example #8
0
int main(void)
{
    NODE *start=NULL;
    insert(&start,10);
    insert(&start,20);
    insert(&start,30);
    insert(&start,40);
    insert(&start,50);
    insert(&start,60);
    display(start);
 //   firstLastReverse(&start);
    largestLast(&start);
    smallestFirst(&start);
    deleteNode(&start,40);
    display(start);
    return 0;
}
Example #9
0
/*
 * Deletes any edges to a vertex in a binary tree
 */
void deleteEdges(binTreeNode_t * pBinaryNode, int tarVertID)
{
    /* delete the edge in this node */
    if(pBinaryNode->value != NULL)
    {
        deleteNode((linkedList_t *) pBinaryNode->value, tarVertID);
    }
    /* run this function recursivly on all children */
    if(pBinaryNode->left != NULL)
    {
        deleteEdges(pBinaryNode->left, tarVertID);
    }
    if(pBinaryNode->right != NULL)
    {
        deleteEdges(pBinaryNode->right, tarVertID);
    }
}
Example #10
0
int main(int argc, char **argv)
{
    struct ListNode n6 = {1, NULL},
                    n5 = {2, &n6},
                    n4 = {3, &n5},
                    n3 = {3, &n4},
                    n2 = {2, &n3},
                    n1 = {1, &n2},
                    n0 = {1, &n4},
                    *res;
    int is_res;
    print(&n0);
    print(&n1);
    deleteNode(&n3);
    print(&n1);
    return 0;
}
Example #11
0
 int internalRemove(const ObjType &u) {
   // returns index of extracted object, or -1 if not there.
   int hashval = computeHash(u);
   SET_NODE* sn = hashTable[hashval];
   SET_NODE** pLast = &(hashTable[hashval]);
   while (sn) {
     if (sn->u == u) {
       int idx = sn->idx;
       *pLast = sn->next;
       deleteNode(sn);
       return idx;
     }
     pLast = &(sn->next);
     sn = sn->next;
   }
   return -1;
 }
Example #12
0
/* Drier program to test above functions*/
int main()
{
    /* Start with the empty list */
    struct node* head = NULL;
 
    push(&head, 7);
    push(&head, 1);
    push(&head, 3);
    push(&head, 2);
 
    puts("Created Linked List: ");
    printList(head);
    deleteNode(&head, 1);
    puts("\nLinked List after Deletion of 1: ");
    printList(head);
    return 0;
}
Example #13
0
int main(){
	int option = 999;
	char *menuStr = getScrOp();
	while(option!=11){
		printf("%sEnter an Option :",menuStr);
		scanf("%d",&option);
		switch(option){
			case 1:
				insert();
				break;
			case 10:
				display();
				break;
			case 2:
				InsertBegin();
				break;
			case 3:
				InsertAnywhere();
				break;
			case 11:
				printf("Exited.");
				break;
			case 4:
				deleteNode();
				break;
			case 5:
				countIteratively();
				break;
			case 6:
				getNth();
				break;
			case 7:
				searchIteratively();
				break;
			case 8:
				searchRecursively(head,getInput());
				break;
			case 9:
				swapByLinks();
				break;
			default :
				printf("Enter an valid option");
		}
	}
	return 0;
}
BDDCalculator &BDDCalculator::operator=(const BDDCalculator &obj)
{
	if(m_proot)
	{
		m_proot -> m_counter--;
		if(m_proot -> m_counter == 0)
		{
			deleteNode(m_proot -> m_pnode);
			delete m_proot;
		}
	}

	m_proot = obj.m_proot;
	m_proot -> m_counter++;

	return *this;
}
Example #15
0
/** Destructor */
GGraph::~GGraph()
{
    freeMarker( nodeTextIsShown);
    for ( GNode *node = firstNode();
          isNotNullP( node);
          )
    {
        GNode* next = node->nextNode();
        int ir_id = node->irId();
        deleteNode( node);
        node = next;
    }
    foreach ( GStyle *style, styles)
    {
        delete style;
    }
}
Example #16
0
static void emptyTree(pwr_tStatus* sts, ptree_sTable* tp)
{
  pool_tRef nr;
  pool_tRef mr;

  if (tp == NULL)
    return;

  for (mr = minimumNode(tp, tp->g->root); mr != pool_cNRef;) {
    if (mr == tp->g->null)
      return;
    nr = successorNode(tp, mr);
    mr = deleteNode(tp, mr);
    freeNode(tp, mr);
    mr = nr;
  }
}
Example #17
0
main()
{

char cmd;   /* Command from user */
char c;
Node * head;  /* Head of the binary search tree */

head = NULL;  /* Initialize the binary search tree to be empty */

printf("Welcome to the binary search tree!\n\n");

while(1) {
   printf("Commands \n");
   printf("     (q) Quit\n");
   printf("     (e) Insert an alphabet \n");
   printf("     (d) Delete an alphabet \n");
   printf("     (t) Display tree \n");
   printf("     (i) List the nodes in in-order\n");
   printf("     (p) List the nodes in post-order\n");

   printf("Enter command: ");
   cmd = getchar();
   while(getchar() != '\n'); /* get rid of extra characters from stdin */

   if (cmd == 'q') return; /* Quit */
   else if (cmd == 'e') insertNode(&head);
   else if (cmd == 'd') deleteNode(&head); /* This is incomplete */
   else if (cmd == 't') displayTree(head);
   else if (cmd == 'i') { /* Display nodes in-order */
      printf("\n");
      printf("In-order list:");
      listInOrder(head);
      printf("\n\n");
   }
   else if (cmd == 'p') {
      printf("\n");
      printf("Post-order list:");
      /* You must implement display nodes post-order */
      listPostOrder(head);
      printf("\n\n");
   }
   else {
      printf("Invalid command, try again\n");
   }
}
}
 ListNode* removeElements(ListNode* head, int val) {
     ListNode dummyHead(0);
     dummyHead.next = head;//虚拟头节点,可以将删除head节点转换成一般情况,无需特殊处理
     ListNode *previous = &dummyHead;//前趋
     ListNode *current = head;
     while(current) {
         if(current->val == val) {
             deleteNode(previous, current);//删除current节点
             current = previous->next;
         }
         else {
             previous = current;
             current = current->next;
         }
     }
     return dummyHead.next;//返回真正的head
 }
Example #19
0
bool MainWindow::eventFilter(QObject *object, QEvent *event)
{
    if (object == ui->insNum && event->type() == QEvent::KeyPress){
        QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
        if (keyEvent->key() == Qt::Key_Return){
            addNode();
            return true;
        }
        else{
            return QMainWindow::eventFilter(object, event);
        }
    }
    else if (object == ui->borrarNum && event->type() == QEvent::KeyPress){
        QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
        if (keyEvent->key() == Qt::Key_Return){
            deleteNode();
            return true;
        }
        else{
            return QMainWindow::eventFilter(object, event);
        }
    }
    else if (object == ui->buscNum && event->type() == QEvent::KeyPress){
        QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
        if (keyEvent->key() == Qt::Key_Return){
            buscarNodo();
            return true;
        }
        else{
            return QMainWindow::eventFilter(object, event);
        }
    }
    else if (object == ui->boxArchivo && event->type() == QEvent::KeyPress){
        QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
        if (keyEvent->key() == Qt::Key_Return){
            leerArchivo();
            return true;
        }
        else{
            return QMainWindow::eventFilter(object, event);
        }
    }
    else{
        return QMainWindow::eventFilter(object, event);
    }
}
Example #20
0
/* ===== deleteCar =====
    This function deletes the car entry
    Pre list, hash, tree
    Post nothing
 */
void deleteCar(D_LIST *list, HASH *hash, BST_TREE *tree){
	CAR car;
	char plate[50];
	char tempString[50];
    
	do{
		printf("Enter your plate number: ");
		gets(plate);
		if(strlen(plate)!=6){
			printf("Invalid input. Try Again\n");
		}
	}while(strlen(plate)!= 6);
	strcpy(car.plate, plate);
	deleteNode(list, plate);
	deleteHash(hash, plate);
	deleteTree(tree, plate);
}// deleteCar
Example #21
0
bool hashTree::remove(treeNode*& root, char * key)
{
    if(!root)
		return false;
	char name[100];
	root->item->getName (name);
	int temp = strcmp(name, key);
	if(temp == 0)
	{
		deleteNode(root);
		return true;
	}
	else if (temp < 0)
		return remove(root->right, key);
	else
		return remove(root->left, key);
}
Example #22
0
/* Drier program to test above function*/
int main()
{
  struct node *root = NULL;
 
  /* Constructing tree given in the above figure */
    root = insert(root, 9);
    root = insert(root, 5);
    root = insert(root, 10);
    root = insert(root, 0);
    root = insert(root, 6);
    root = insert(root, 11);
    root = insert(root, -1);
    root = insert(root, 1);
    root = insert(root, 2);
 
    /* The constructed AVL Tree would be
            9
           /  \
          1    10
        /  \     \
       0    5     11
      /    /  \
     -1   2    6
    */
 
    printf("Pre order traversal of the constructed AVL tree is \n");
    preOrder(root);
 
    root = deleteNode(root, 10);
 
    /* The AVL Tree after deletion of 10
            1
           /  \
          0    9
        /     /  \
       -1    5     11
           /  \
          2    6
    */
 
    printf("\nPre order traversal after deletion of 10 \n");
    preOrder(root);
 
    return 0;
}
void MaterialWindow::OnMenuEvent( wxCommandEvent& evt )
{
    switch( evt.GetId() )
    {
    // Add Nodes
    case 0:
        addNode(mMaterialAsset->getTemplate(), "Opaque");
        break;
    case 1:
        addNode(mMaterialAsset->getTemplate(), "Float");
        break;
    case 2:
        addNode(mMaterialAsset->getTemplate(), "Vec2");
        break;
    case 3:
        addNode(mMaterialAsset->getTemplate(), "Vec3");
        break;
    case 4:
        addNode(mMaterialAsset->getTemplate(), "Vec4");
        break;
    case 5:
        addNode(mMaterialAsset->getTemplate(), "Texture");
        break;
    case 6:
        addNode(mMaterialAsset->getTemplate(), "Time");
        break;
    case 7:
        addNode(mMaterialAsset->getTemplate(), "Cos");
        break;
    case 8:
        addNode(mMaterialAsset->getTemplate(), "Sin");
        break;
    case 9:
        addNode(mMaterialAsset->getTemplate(), "Multiply");
        break;
    case 10:
        addNode(mMaterialAsset->getTemplate(), "Lerp");
        break;

    // Delete Node
    case 100:
        deleteNode(mMaterialAsset->getTemplate(), mHoverNode);
        break;
    }
}
main()
      {
      int n;
      printf("Enter from the following option\n");
      while(1)
              {
              printf("1.Add Elements to the Linked List \n");
              printf("2.Delete Node\n");
              printf("3.Display Element \n");   
              printf("4.Display Reverse\n");
              printf("5.Exit\n");   
              scanf("%d",&n);
              switch(n)
                  {
                  case 1:{
                          printf("Enter the Value:\n");
                          int value;
                          scanf("%d",&value);
                          addNodes(value);
                          break;
                          }
                  case 2:{
                         printf("Enter the node to delete\n");
                         int value;
                         scanf("%d",&value);
                         deleteNode(value);
                         break;
                         }        
                  case 3:{
                         display();
                         break; 
                          }  
                  case 4:{
                         displayReverse();
                         break; 
                          }          
                  case 5:
                         exit(1);         
                  default:
                          printf("Wrong Input\n");            
                            
                  }
              printf("\n\n");    
              }                    
      }        
// Remove key from pp. Returns true if pp needs rebalancing. false if not
bool TreeSetImpl::nodeDelete(TreeSetNode *&pp, const void *key) {
  TreeSetNode *p = pp;

  if(p == NULL) {
    return false;                  // key not found
  }

  const int c = m_comparator->cmp(p->m_key, key);
  if(c > 0) {                      // pp.key > key. Continue search in left subtree
    if(nodeDelete(p->m_left, key)) {
      return balanceL(pp);
    } else {
      return false;
    }
  }
  if(c < 0) {                      // pp.key < key. Continue search in right subtree
    if(nodeDelete(p->m_right, key)) {
      return balanceR(pp);
    } else {
      return false;
    }
  }

  bool result = false;
  if(p->m_right == NULL) {
    pp = p->m_left;
    result = true;
  } else if(p->m_left == NULL) {
    pp = p->m_right;
    result = true;
  } else {
    m_deleteHelper = p;
    if(nodeDel(p->m_left)) {
      result = balanceL(pp);
    }
    p = m_deleteHelper;
  }

  deleteNode(p);

  m_size--;
  m_updateCount++;

  return result;
}
Example #26
0
// remove helper
// Removes a TreeData object equal to data from the tree. If there is only
// one TreeData object, the node containing that object is removed and
// deleted.
// preconditions:	node->m_item must be a valid TreeData object not equal
//					to nullptr; this not equal to nullptr.
// postconditions:	If data is not found, then false is returned. If data
//					is found and m_itemCount > 1 then m_itemCount is 
//					decremented by one; if m_itemCount == 1 then the node
//					is removed from the tree and deleted.
//
bool BSTree::remove(const TreeData &data, Node *&node) {
	if(node == nullptr) {
		return(false);
	} else if(data == *node->m_item) {
		if(node->m_itemCount > MIN_ITEM_COUNT) {
			node->m_itemCount--;
		} else {
			deleteNode(node);
		}
		return(true);
	} else if(data < *node->m_item) {
		return(remove(data, node->m_left));
	} else {
		return(remove(data, node->m_right));
	}

	return(false);
}
Example #27
0
bool medTree::deleteElement(myLL* node,int num2del)
{
	if (node == NULL)
	{
		printf("Element not found \n");
		return false;
	}
	else if (node->value == num2del)
	{
		printf("Node with num 2 be deleted found \n");
		deleteNode(node);
		return true;
	}
	else if (node->value > num2del)
		return (deleteElement(node->left,num2del)); //num to search is less than root..so check left sub-tree.
	else if (node->value < num2del)
		return (deleteElement(node->right,num2del));//num to search is more than root..so check right sub tree.
}
Example #28
0
int main()
{
    struct node* head=NULL;
    insert(&head,1);
    insert(&head,2);
    insert(&head,3);
    insert(&head,4);
    insert(&head,5);
    insert(&head,6);

    print(head);

    deleteNode(&head,3);

    print(head);

    return 0;
}
Example #29
0
void findAndDelete(bpGraph_t *pGraph, int vertId, int partite)
{
	vlNode_t *pCurrNode = NULL;

	/* search for all elements in in-neighbourhood. */
	if (partite == 1) {
        pCurrNode = pGraph->vertices1->pHead;
	}
	else if (partite == 2) {
        pCurrNode = pGraph->vertices2->pHead;
	}
	while (pCurrNode != NULL) {

		/* Delete from the edge list */
		deleteNode(pCurrNode->edges, vertId);
		pCurrNode = pCurrNode->pNext;
	}
} /* end of findAndDelete() */
Example #30
0
 /**
  * @param root: The root of the binary search tree.
  * @param value: Remove the node with given value.
  * @return: The root of the binary search tree after removal.
  */
 public TreeNode removeNode(TreeNode root, int value) {
     TreeNode dummy = new TreeNode(0);
     dummy.left = root;
     
     TreeNode parent = findNode(dummy, root, value);
     TreeNode node;
     if (parent.left != null && parent.left.val == value) {
         node = parent.left;
     } else if (parent.right != null && parent.right.val == value) {
         node = parent.right;
     } else {
         return dummy.left;
     }
     
     deleteNode(parent, node);
     
     return dummy.left;
 }