void freenodes (struct Node *pNode) { if (pNode == NULL) return; if (pNode->pLeft != NULL) freenodes (pNode->pLeft); if (pNode->pRight != NULL) freenodes (pNode->pRight); free (pNode); }
/* Release memory allocated to nodes */ void freenodes(struct Node * pNode) { if(pNode == NULL) /* If there's no node... */ return; /* we are done. */ if(pNode->pLeft != NULL) /* If there's a left sub-tree */ freenodes(pNode->pLeft); /* free memory for those nodes. */ if(pNode->pRight != NULL) /* If there's a right sub-tree */ freenodes(pNode->pRight); /* free memory for those nodes. */ free(pNode); /* Free current node memory */ }
/************************************* * Release memory allocated to nodes * * including the memory for the name * * referenced by the pName member of * * each node. * *************************************/ void freenodes(Node * pNode) { if(!pNode) /* If there's no node... */ return; /* we are done. */ if(pNode->pLeft) /* If there's a left sub-tree */ freenodes(pNode->pLeft); /* free memory for those nodes. */ if(pNode->pRight) /* If there's a right sub-tree */ freenodes(pNode->pRight); /* free memory for those nodes. */ free(pNode->pName); /* Release name memory */ free(pNode); /* then current node memory */ }
/************************************* * Release memory allocated to nodes * * including the memory for the name * * referenced by the pName member of * * each node. * *************************************/ void freenodes(Node *pNode) { if(!pNode) // If there's no node... return; // ...we are done. if(pNode->pLeft) // If there's a left sub-tree... freenodes(pNode->pLeft); // ...free memory for those nodes. if(pNode->pRight) // If there's a right sub-tree... freenodes(pNode->pRight); // ...free memory for those nodes. free(pNode->pName); // Release name memory... free(pNode); // ...then current node memory }
/*========================================= 遍历二叉树并释放内存 =========================================*/ void freenodes(struct Node *pNode) { if(pNode == NULL) return; if(pNode->pleft != NULL) { freenodes(pNode->pleft); } if(pNode->pright != NULL) { freenodes(pNode->pright); } free(pNode); }
/*============================================ 主函数 ============================================*/ 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; }
int main(int argc, char const *argv[]) { node * head=createnodes(10); printf("原链表\n"); printnodes(head); insertnode(head,4,100); printf("插入2后链表\n"); printnodes(head); deletenode(head,2); printf("删除1后链表\n"); printnodes(head); //反序输出 printnodesf(head); freenodes(head); return 0; }
int main (void) { int tmpstring_lenth; struct Node *pRoot = NULL; bool needfree = false; printf ("Type some sentences:\n"); while (true) { tmpstring_lenth = 0; char tmpstring[MAX_LEN] = {0}; fgets (tmpstring, MAX_LEN, stdin); if (tmpstring[0] == '\n') break; tmpstring_lenth = static_cast<int>(strlen (tmpstring)); if (pRoot == NULL) { pRoot = createnode (tmpstring, tmpstring_lenth); } else { if (addnode (tmpstring, pRoot, tmpstring_lenth) == NULL) { needfree = true; static struct LinkedList *current = NULL; static struct LinkedList *previous = NULL; current = reinterpret_cast<LinkedList *> (malloc (sizeof (LinkedList))); if (first == NULL) first = current; if (previous != NULL) previous->pnext = current; current->string_lenth = tmpstring_lenth; for (int i = 0; i < tmpstring_lenth; ++i) { current->string[i] = tmpstring[i]; } current->pnext = NULL; previous = current; } } fflush (stdin); } printf ("All the sentences are:\n"); listnodes (pRoot); freenodes (pRoot); if (needfree) freelinked (first); return 0; }
// Function main - execution starts here int main(void) { Node *pRoot = NULL; char answer = 'n'; do { if(!pRoot) pRoot = createnode(readname()); else addnode(readname(), pRoot); printf_s("\nDo you want to enter another (y or n)? "); scanf_s(" %c", &answer, sizeof(answer)); fflush(stdin); // Get rid of the newline } while(tolower(answer) == 'y'); printf_s("The names in ascending sequence are:\n"); listnodes(pRoot); // Output the contents of the tree freenodes(pRoot); // Release the heap memory return 0; }
void Xrdcmds(void) { struct thread *p = runq; word *prompt; flush(err); nerror = 0; if(flag['s'] && !truestatus()) pfmt(err, "status=%v\n", vlook("status")->val); if(runq->iflag){ prompt = vlook("prompt")->val; if(prompt) promptstr = prompt->word; else promptstr="% "; } Noerror(); if(yyparse()){ if(!p->iflag || p->eof && !Eintr()){ if(p->cmdfile) efree(p->cmdfile); closeio(p->cmdfd); Xreturn(); /* should this be omitted? */ } else{ if(Eintr()){ pchr(err, '\n'); p->eof = 0; } --p->pc; /* go back for next command */ } } else{ ntrap = 0; /* avoid double-interrupts during blocked writes */ --p->pc; /* re-execute Xrdcmds after codebuf runs */ start(codebuf, 1, runq->local); } freenodes(); }
/* Function main - execution starts here */ int main(void) { Name *pNewName = NULL; Node *pRoot = NULL; char answer = 'n'; do { pNewName = readname(); if(!pRoot) pRoot = createnode(pNewName); else addnode(pNewName, pRoot); printf("\nDo you want to enter another (y or n)? "); scanf(" %c", &answer); fflush(stdin); /* Get rid of the newline */ } while(tolower(answer) == 'y'); printf("The names in ascending sequence are:\n"); listnodes(pRoot); /* Output the contents of the tree */ freenodes(pRoot); /* Release the heap memory */ return 0; }
/* Function main - execution starts here */ 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 (y or n)? "); scanf(" %c", &answer); } while(tolower(answer) == 'y'); printf("The values in ascending sequence are: "); listnodes(pRoot); /* Output the contents of the tree */ freenodes(pRoot); /* Release the heap memory */ return 0; }
void freenodes (node_t * node) { int i; if (!node) return; if (node->children) { if (node->num_children > 0) { for (i = 0; i < (node->num_children); i++) { if (node->children[i] != 0) { freenodes (node->children[i]); }; }; free (node->children); } }; free (node); };