//------------------------------------------------------------------------------ // Create a star tree with n leaves void NTree::StarTree (int n) { Leaves = n; Internals = 1; Root = NewNode(); Root->SetWeight (n); Root->SetDegree (n); CurNode = NewNode(); CurNode->SetLeaf(true); CurNode->SetLeafNumber(1); CurNode->SetLabelNumber(1); Root->SetChild (CurNode); CurNode->SetAnc (Root); // Remaining leaves for (int i = 1; i < n; i++) { NodePtr q = NewNode (); q->SetLeaf(true); q->SetLeafNumber(i+1); q->SetLabelNumber(i+1); q->SetAnc (Root); CurNode->SetSibling (q);; CurNode = q; } MakeNodeList(); Update(); BuildLeafClusters (); }
bool StudentTree::Insert(unsigned int id, char* fullname, unsigned int flags, bool reorder) { if ((int)id <= 0 || Exists(id)) return false; if(!ValidFullname(fullname)) return false; tempData = (TREE_NODE_DATA*)malloc(sizeof(TREE_NODE_DATA)); tempData->id = id; tempData->fullname = fullname; tempData->flags = flags; tempData->bstId = tempData->bstName = 0; tempNode = NewNode(tempData); InsertSortedId(&(bstId->root), tempNode); tempNode = NewNode(tempData); InsertSortedName(&(bstName->root), tempNode); if (reorder) SetMode(treeMode); return true; }
NODE * InsertIterative (NODE * root ,int data ) { if(root==NULL ) { return(NewNode(data)); } NODE * ptr =root ; NODE * save =NULL; NODE * newInsert=NewNode(data); while(ptr!=NULL) { if(ptr->data > data ) { save =ptr; ptr=ptr->left; } else { save =ptr; ptr=ptr->right; } } if(save->data > data) { save->left=newInsert; } else { save->right=newInsert; } return root ; }
// b: by calling newNode() three times, and using only one pointer variable struct node* build123b() { struct node* root = NewNode(2); root->left = NewNode(1); root->right = NewNode(3); return root; }
void Init() { num[0]=0; val[0]=0; NewNode(size=0,rt,0); NewNode(rt,son[rt][1],0); makeTree(0,n-1,keyTree,son[rt][1]); Update(son[rt][1]); Update(rt); }
struct node* buildBSTree() { struct node* root = NewNode(4); root->left = NewNode(2); root->left->right = NewNode(3); root->left->left = NewNode(1); root->right = NewNode(5); return root; }
void Init() { for(int i = 1; i <= n; i++) scanf("%d", &a[i]); root = 0; rear = head = 0; f[root] = Node(0, 0, 0, 0, 0, 0, 0); NewNode(root, 0, -1); NewNode(f[root].ch[1], root, -1); Build(Key_value, 1, n, f[root].ch[1]); }
struct node* build123_a() { struct node* node1 = NewNode(1); struct node* node2 = NewNode(2); struct node* node3 = NewNode(3); node2->left = node1; node2->right = node3; return node2; }
// a: by calling newNode() three times, and using three pointer variables struct node* build12345() { struct node* root = NewNode(4); struct node* left = NewNode(2); struct node* right = NewNode(5); root->left = left; root->right = right; root->left->left = NewNode(1); root->left->right = NewNode(3); return root; // <<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>> }
List *InitList() { List *L; L = (List *)malloc(sizeof(List)); memset(L,0,sizeof(List)); L->size = 0; L->header = NewNode(0,NULL); L->trailer = NewNode(0,NULL); L->header->Next = L->trailer; return L; }
int main() { struct edges *Node=NULL; char ch; int i,j,weight; scanf("%d %d",&n,&m); if(n<=0||n>=200000) exit(EXIT_FAILURE); if(m<=0||m>=200000) exit(EXIT_FAILURE); struct node *start[n]; struct edges *ptr[n]; for(i=0;i<n;i++) { start[i]=NULL; ptr[i]=NULL; } int k; for(k=0;k<m;k++) { scanf("%d %d %d",&i,&j,&weight); if(i<=0||i>=200000) exit(EXIT_FAILURE); if(j<=0||j>=200000) exit(EXIT_FAILURE); Node=NewNode(); if(start[i]==NULL) start[i]=(struct node*)Node; else ptr[i]->next=Node; Node->vertex=j; Node->weight=weight; ptr[i]=Node; Node=NewNode(); if(start[j]==NULL) start[j]=(struct node*)Node; else ptr[j]->next=Node; Node->vertex=i; Node->weight=weight; ptr[j]=Node; } for(i=0;i<n;i++) { printf("%d ->",i); PrintList((struct edges*)start[i]); } return 0; }
static void GetInstChain(RSTREE R, typrect newrect, int depth) { int i; refcount c; i= 1; while (i < depth) { if ((*R).NInst[i+1] != NULL) { /* already in path */ (*R).E[i]= (*R).EInst[i]; (*R).EInst[i]= -1; i++; if ((*R).N[i] != (*R).NInst[i]) { (*R).P[i]= (*(*R).N[i-1]).DIR.entries[(*R).E[i-1]].ptrtosub; free((*R).N[i]); (*R).N[i]= NULL; (*R).N[i]= (*R).NInst[i]; } (*R).NInst[i]= NULL; } else if ((*R).EInst[i] != -1) { /* known ... */ (*R).E[i]= (*R).EInst[i]; (*R).EInst[i]= -1; i++; if ((*(*R).N[i-1]).DIR.entries[(*R).E[i-1]].ptrtosub != (*R).P[i]) { /* but not in path */ NewNode(R,i); } } else { /* not known */ ChooseSubtree(R,newrect,i,&(*(*R).N[i]).DIR,&(*R).E[i]); i++; if ((*(*R).N[i-1]).DIR.entries[(*R).E[i-1]].ptrtosub != (*R).P[i]) { /* and not in path */ NewNode(R,i); } } } c= &(*R).count; if ((*c).countflag) { if (depth == (*R).parameters._.height) { (*c).dirvisitcount+= depth - 1; (*c).datavisitcount++; } else { (*c).dirvisitcount+= depth; } } }
RedBlackTree::RedBlackTree(int nRootKey /*= -1*/) { m_pNull = NewNode(-2, black); if (nRootKey != -1) { m_pRoot = NewNode(nRootKey, black); } else { m_pRoot = nullptr; } }
void Init() { root = tot1 = tot2 = 0; ch[root][0] = ch[root][1] = size[root] = pre[root] = 0; same[root] = rev[root] = sum[root] = key[root] = 0; lx[root] = rx[root] = mx[root] = -INF; NewNode(root,0,-1); NewNode(ch[root][1],root,-1); for(int i = 0;i < n;i++) scanf("%d",&a[i]); Build(Key_value,0,n-1,ch[root][1]); push_up(ch[root][1]); push_up(root); }
node_idx Forest::ProjectVals(level k, node_idx p, level cutoff) { Node *nodeP; node_idx result; node_idx flag; //Check Base Cases if (p == 0) return 0; if (k == 0) return 1; //Check Cache result = ProjectCache[k]->hit(p); if (result >= 0) { return result; } nodeP = &FDDL_NODE(k, p); if (k < cutoff) { flag = 0; for (node_idx i = 0; i < nodeP->size; i++) { flag = ProjectVals(k - 1, FDDL_ARC(k, nodeP, i), cutoff); if (flag != 0) break; } if (flag != 0) { result = NewNode(k); for (node_idx i = 0; i <= maxVals[k]; i++) { SetArc(k, result, i, flag); } result = CheckIn(k, result); ProjectCache[k]->add(result, p); return result; } } else { result = NewNode(k); for (node_idx i = 0; i < nodeP->size; i++) { SetArc(k, result, i, ProjectVals(k - 1, FDDL_ARC(k, nodeP, i), cutoff)); } result = CheckIn(k, result); ProjectCache[k]->add(result, p); return result; } return 0; }
node_idx Forest::Projection(level k, node_idx p, level * mask) { Node *nodeP; node_idx result; node_idx flag; node_idx u; //Check Base Cases if (p == 0) return 0; if (k == 0) return 1; //Check Cache result = ProjectCache[k]->hit(p); if (result >= 0) { return result; } nodeP = &FDDL_NODE(k, p); if (mask[k] == 0) { flag = 0; for (node_idx i = 0; i < nodeP->size; i++) { u = Projection(k - 1, FDDL_ARC(k, nodeP, i), mask); flag = InternalMax(k - 1, u, flag); } if (flag != 0) { result = NewNode(k); for (node_idx i = 0; i <= maxVals[k]; i++) { SetArc(k, result, i, flag); } result = CheckIn(k, result); ProjectCache[k]->add(result, p); return result; } } else { result = NewNode(k); for (node_idx i = 0; i < nodeP->size; i++) { SetArc(k, result, i, Projection(k - 1, FDDL_ARC(k, nodeP, i), mask)); } result = CheckIn(k, result); ProjectCache[k]->add(result, p); return result; } return 0; }
void Insert(struct HNode* root,int data){ if(root->next==NULL){ root->next=NewNode(data); } else{ struct LNode* tmpNode=root->next; while(tmpNode->next){ tmpNode=tmpNode->next; } tmpNode->next=NewNode(data); } root->count++; }
int main() { struct node *Node=NULL, *ptr=NULL; struct node *start=NULL,*start1=NULL,*start2=NULL; char ch; do { ptr=Node; Node=NewNode(); printf("\nEnter data :"); scanf("%d",&Node->data); if(start1==NULL) start1=Node; else ptr->next=Node; printf("Do you want to continue(Y/N)"); }while(toupper((ch=getch()))!='N'); printf("\nComplete Linked list:\n"); PrintList(start1); do { ptr=Node; Node=NewNode(); printf("\nEnter data :"); scanf("%d",&Node->data); if(start2==NULL) start2=Node; else ptr->next=Node; printf("Do you want to continue(Y/N)"); }while(toupper((ch=getch()))!='N'); printf("\nComplete Linked list:\n"); PrintList(start2); start=SortedMerge(start1,start2); printf("\n\nMerged Linked list:\n"); PrintList(start); return 0; }
static void ShrinkTree(RSTREE R) { refparameters par; refcount c; int i; par= &(*R).parameters._; if ((*R).P[2] == 0) { (*R).E[1]= 0; NewNode(R,2); } free((*R).N[1]); for (i= 1; i <= (*par).height; i++) { (*R).N[i]= (*R).N[i+1]; } (*R).Nmodified[1]= TRUE; c= &(*R).count; if ((*c).countflag) { (*c).dirmodifycount++; } PutPageNr(R,(*R).P[2],2); for (i= 2; i <= (*par).height; i++) { (*R).P[i]= (*R).P[i+1]; (*R).Nmodified[i]= (*R).Nmodified[i+1]; (*par).pagecountarr[i]= (*par).pagecountarr[i+1]; } (*R).E[(*par).height]= -1; (*par).height--; }
POSITION CPtrList::InsertAfter(POSITION position, void* newElement) { ASSERT_VALID(this); if (position == NULL) return AddTail(newElement); // insert after nothing -> tail of the list // Insert it before position CNode* pOldNode = (CNode*) position; ASSERT(AfxIsValidAddress(pOldNode, sizeof(CNode))); CNode* pNewNode = NewNode(pOldNode, pOldNode->pNext); pNewNode->data = newElement; if (pOldNode->pNext != NULL) { ASSERT(AfxIsValidAddress(pOldNode->pNext, sizeof(CNode))); pOldNode->pNext->pPrev = pNewNode; } else { ASSERT(pOldNode == m_pNodeTail); m_pNodeTail = pNewNode; } pOldNode->pNext = pNewNode; return (POSITION) pNewNode; }
//------------------------------------------------------------------------------ // Add Node below Below. Doesn't update any clusters, weights, etc. void Tree::AddNodeBelow (NodePtr Node, NodePtr Below) { NodePtr Ancestor = NewNode (); Ancestor->SetChild (Node); Node->SetAnc (Ancestor); NodePtr q = Below->GetAnc (); Internals++; if (Node->IsLeaf()) Leaves++; if (q == NULL || Below == q->GetChild()) { Node->SetSibling (Below); Ancestor->SetAnc (q); Ancestor->SetSibling (Below->GetSibling()); Below->SetSibling (NULL); Below->SetAnc (Ancestor); if (q == NULL) Root = Ancestor; else q->SetChild (Ancestor); } else { // Get left sibling of Below NodePtr r = Below->LeftSiblingOf(); while (Below != r->GetSibling()) r = r->GetSibling(); Node->SetSibling (Below); Ancestor->SetAnc (q); Ancestor->SetSibling (Below->GetSibling()); Below->SetSibling (NULL); Below->SetAnc (Ancestor); r->SetSibling (Ancestor); } }
int main() { struct list*Node=NULL,*ptr=NULL; char ch; int item; do { ptr=Node; Node=NewNode(); printf("\nEnter data :"); scanf("%d",&Node->data); if(start==NULL){ start=Node; start->next=start; } else ptr->next=Node; printf("Do you want to continue(Y/N)"); }while(toupper((ch=getch()))!='N'); printf("\nOriginal Linked List :"); PrintList(); printf("\n\nEnter the item to be Inserted :"); scanf("%d",&item); Insert_last(item); printf("\n\nLinked List After Inserting :"); PrintList(); return 0; }
POSITION CPtrList::InsertBefore(POSITION position, void* newElement) { ASSERT_VALID(this); if (position == NULL) return AddHead(newElement); // insert before nothing -> head of the list // Insert it before position CNode* pOldNode = (CNode*) position; CNode* pNewNode = NewNode(pOldNode->pPrev, pOldNode); pNewNode->data = newElement; if (pOldNode->pPrev != NULL) { ASSERT(AfxIsValidAddress(pOldNode->pPrev, sizeof(CNode))); pOldNode->pPrev->pNext = pNewNode; } else { ASSERT(pOldNode == m_pNodeHead); m_pNodeHead = pNewNode; } pOldNode->pPrev = pNewNode; return (POSITION) pNewNode; }
//------------------------------------------------------------------------------ // This code needs Tree to be a friend of Node void Tree::copyTraverse (NodePtr p1, NodePtr &p2) const { if (p1) { p2 = NewNode (); p1->Copy (p2); // Note the call to p2->child, not p2->GetChild(). Calling the field // is essential because calling GetChild merely passes a temporary // copy of the child, hence we are not actually creating a child of p2. // We can access child directly by making Tree a friend of Node (see // TreeLib.h). copyTraverse (p1->GetChild(), p2->Child); if (p2->GetChild()) p2->GetChild()->SetAnc (p2); // Ensure we don't copy RootedAt sibling. If the sibling is NULL then // we won't anyway, but this line ensures this for all cases. if (p1 != CurNode) copyTraverse (p1->GetSibling(), p2->Sib); // note sib if (p2->GetChild ()) { NodePtr q = p2->GetChild()->GetSibling(); while (q) { q->SetAnc (p2); q = q->GetSibling(); } } } }
CCounter *FindCounters(HashTable *table, unsigned long long key) { Node *n; int i,j; i = (unsigned int)(key % (unsigned long long)hash_size); // the hash code for(n = table->hash_table[i] ; n != NULL ; n = n->next) for(j = 0 ; j < n->n_entries ; j++) if(n->keys[j] == key) return &n->counters[j][0]; // key not found, insert it n = table->hash_table[i]; if(n == NULL || n->n_entries == max_entries) { n = NewNode(table); n->next = table->hash_table[i]; n->n_entries = 0; table->hash_table[i] = n; } j = n->n_entries++; n->keys[j] = key; n->counters[j][0] = 0; n->counters[j][1] = 0; n->counters[j][2] = 0; n->counters[j][3] = 0; table->n_used_keys++; return &n->counters[j][0]; }
TNode *ReadTip(FILE *fv, char ch, TTree *tree, int numNames, char **names) { int i; char *P; char name[256]; TNode *node; node=NewNode(tree); i=0; P=name; while (!feof(fv) && ch!=':' && ch!=',' && ch!=')' && i<MAX_NAME_LEN) { if (!isspace(ch)) { *P=ch; i++;P++; } ch=fgetc(fv); } *P='\0'; CheckCapacity(tree, tree->numTips+1); if (numNames == 0) { node->tipNo=tree->numTips; if (tree->names[node->tipNo]==NULL) { if ( (tree->names[node->tipNo]=(char *)malloc(MAX_NAME_LEN+1))==NULL ) { strcpy(treeErrorMsg, "Out of memory creating name."); return NULL; } } strcpy(tree->names[node->tipNo], name); } else { /* we already have some names so just look it up...*/ i = 0; while (i < numNames && strcmp(name, names[i]) != 0) i++; if (i == numNames) { sprintf(treeErrorMsg, "Taxon names in trees for different partitions do not match."); return NULL; } node->tipNo=i; } tree->tips[node->tipNo]=node; tree->numTips++; while (!feof(fv) && ch!=':' && ch!=',' && ch!=')') ch=fgetc(fv); if (feof(fv)) { sprintf(treeErrorMsg, "Unexpected end of file"); return NULL; } ungetc(ch, fv); return node; }
// Creates a new list using a head node so we can find, delete and insert nodes more easily. List NewList() { List head_node; head_node = malloc(sizeof(StructList)); head_node = NewNode(0, "\0"); return head_node; }
status_t BnRTCBinder::onTransact( uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) { switch(code) { case kDefault_CALL_ID: { rtc_binder_data_t out; out.data = &data; out.reply = reply; // Reserved 4 bytes reply->writeInt32(0); int ret = Transport(&out); int pos = reply->dataPosition(); reply->setDataPosition(0); // Rewrite 4 bytes reply->writeInt32(ret); reply->setDataPosition(pos); return NO_ERROR; } break; case kDefault_CreateNodeID: { CHECK_INTERFACE(IRTCBinder, data, reply); int type = 0; type = data.readInt32(); sp<IRTCNode> node = NewNode(type); #if ANDROID_SDK_VERSION >=23 reply->writeStrongBinder(node->asBinder(node.get())); #else reply->writeStrongBinder(node->asBinder()); #endif return NO_ERROR; } break; default: return BBinder::onTransact(code, data, reply, flags); } }
/* Puts element in front of the list. * Gets the original list and element to add. * Returns new list. */ static NODE* AddToList(NODE *list, /* write-only original list */ int row, /* row from cell to add */ int col) /* column from cell to add */ { NODE *c; #ifdef NEVER if(InList(list, row, col)) /* CW not neccessary when * building initial list */ return list; # endif if (Set1BitMatrix(inList, row,col)) /* already in list */ return list; c = NewNode(row,col); if(c == NULL) { list = FreeList(list); return NULL; /* allocation error */ } /* initialize node, row,col set in NewNode */ c->prev = NULL; if(list == NULL) list = firstOfList = c; /* first element in list */ else { firstOfList->prev = c; /* put in front of 1st element */ firstOfList = c; } return list; }
/*------------------------------------- * usage: 相链表插入新数据函数 * pre: 第一次调用时,要满足*list=NULL * arguments: list -- 链表表头指针 !! * data -- 新插入数据指针 * n -- 插入数据大小 * cmp -- 数据比较函数指针 * return: 成功 -- 0 * 失败 -- -1 * comment:新节点插入链表头 *-------------------------------------*/ int InsertList (list_t **list, const void *data, size_t n) { listnode_t *tempnode; void *tempdata; if (*list == NULL) { *list = (list_t *) malloc (sizeof (list_t)); if (*list == NULL) { printf("msg:InsertList: malloc list_t"); return -1; } (*list)->head = NULL; } if ((tempdata = (void *) malloc (n)) == NULL) {/* allocate data space*/ printf("msg:InsertList: malloc data space"); return -1; } memcpy (tempdata, data, n); if ((tempnode = NewNode ()) == NULL) { printf("msg:InsertList: NewNode()"); return -1; } tempnode->u.data = tempdata; tempnode->next = (*list)->head; (*list)->head = tempnode; return 0; }