List *prependAndTag(List *l, void *data, Tag tag) { #ifdef DEBUG printf("\033[32m%s\033[00m\n", __func__); #endif if (l == NULL) { l = createNewList(); } if (l->head == NULL) { // First item being added to the list l->head = createNewNode(); l->head->data = data; l->head->tag = tag; l->tail = l->head; } else { // Adding to the front Node *newEnd = createNewNode(); newEnd->data = data; newEnd->next = l->head; newEnd->tag = tag; l->head = newEnd; l->tail->next = l->head; } ++l->size; return l; }
//FIXME add path to where to add this void QQMlDom::writeConfigPage(const QString path) { // create the default children nodes QDomElement rootInstaller = m_document.createElement("Installer"); m_document.appendChild(rootInstaller); QStringList nodeLists ; nodeLists << getDefaultConfigXml(); for (int i = 0 ; i < nodeLists.length(); i++) { QDomElement configNodes = m_document.createElement(nodeLists.at(i)); rootInstaller.appendChild(configNodes); } createNewNode(m_document,rootInstaller,"RunProgramArguments","Argument"); createNewNode(m_document,rootInstaller,"RemoteRepositories","Repository"); createNewNode(m_document,rootInstaller,"Repository","Url"); // Now Finaly write it all to a file. QString installerPath = QString("%1%2").arg(path).arg("/config.xml"); QFile instalerFile(installerPath); // Create the root node if ( ! instalerFile.open(QFile::WriteOnly | QFile::Text) ) { error("Could not open the Installer Configure File IE config.xml"); } else { QTextStream stream(&instalerFile); stream << m_document.toString(); instalerFile.close(); } }
void QQMlDom::writePackagePage(const QString path) { m_document.clear(); QDomElement packageInstaller = m_document.createElement("Package"); m_document.appendChild(packageInstaller); QStringList nodeLists ; nodeLists << getDefaultPackageXml(); for (int i = 0 ; i < nodeLists.length(); i++) { QDomElement packageNodes = m_document.createElement(nodeLists.at(i)); packageInstaller.appendChild(packageNodes); } createNewNode(m_document,packageInstaller,"Licenses","License"); createNewNode(m_document,packageInstaller,"UserInterfaces","UserInterface"); createNewNode(m_document,packageInstaller,"Translations","Translation"); // Now Finaly write it all to a file. QString installerPath = QString("%1%2").arg(path).arg("/package.xml"); QFile instalerFile(installerPath); if ( ! instalerFile.open(QFile::WriteOnly | QFile::Text) ) { error("Could not open the Installer Configure File IE package.xml"); } else { QTextStream stream(&instalerFile); stream << m_document.toString(); instalerFile.close(); } }
node *insertInDll(node *head,int data){ if(NULL==head) return createNewNode(data); node *temp; temp=createNewNode(data); node *curr=head; while(curr->next) curr=curr->next; curr->next=temp; temp->prev=curr; return head; }
void addLL(linked_list* list, void* data) { assert(list); if (!(list->head)) { createNewNode(&list->head, data); } else { linked_list_node_t* curr = list->head; while (curr->next != NULL) curr = curr->next; createNewNode(&curr->next, data); } (list->size)++; }
node *intersection(node *head1,node *head2){ if(NULL==head1 || NULL==head2) return NULL; node *head=NULL,*prev=NULL; int p=-1; node *curr1=head1,*curr2=head2,*temp=NULL; while(curr1 && curr2){ if(curr1->data==curr2->data){ if(curr1->data!=p){ p=curr1->data; temp=createNewNode(p); if(NULL==head) head=temp; else prev->next=temp; prev=temp; } curr1=curr1->next; curr2=curr2->next; }else{ if(curr1->data<curr2->data) curr1=curr1->next; else curr2=curr2->next; } } return head; }
void addToBST(char *name, char *phone, char *email, char *memo) { struct node *new_node = createNewNode(name,phone, email,memo); if(root==0) { root = new_node; return; } else { struct node *cur_node=root; while(1) {if(strcmp(cur_node->name, new_node->name)>0) {if(cur_node->left==0) {cur_node->left=new_node; return; }cur_node=cur_node->left; } else if(strcmp(cur_node->name,new-node->name)<0) {if(cur_node->right==0) {cur_node->right = new_node; return;} cur_node = cur_node->left; } else if(strcmp(cur_node->name, new_node->name)<0) { if(cur_node->right==0) {cur_node->right=new_node; return;} cur_node=cur_node->right; } else {printf("same name");} } }
Ainfo getAccessNode(const char *file1, const char *file2, int mode, pid_t pid) { Ainfo node = createNewNode(); node->cwd = getCWDFromPid(pid); node->userName = getEUserFromPid(pid); node->groupName = getEGroupFromPid(pid); node->fileOwner = getFileOwner(file1); if (file1[0] == '/') { node->fileName1 = (char*) malloc((strlen(file1) + 1) * sizeof (char)); strcpy(node->fileName1, file1); } else { node->fileName1 = (char*) malloc((strlen(node->cwd) + strlen(file1) + 2) * sizeof (char)); strcpy(node->fileName1, node->cwd); strcat(node->fileName1, "/"); strcat(node->fileName1, file1); } //Not every access node has file2 if (file2) { if (file2[0] == '/') { node->fileName2 = (char*) malloc((strlen(file2) + 1) * sizeof (char)); strcpy(node->fileName2, file2); } else { node->fileName2 = (char*) malloc((strlen(node->cwd) + strlen(file2) + 2) * sizeof (char)); strcpy(node->fileName2, node->cwd); strcat(node->fileName2, "/"); strcat(node->fileName2, file2); } } node->mode = mode; node->pid = pid; return node; }
void LinkedList<NodeType>::insertAtFront( const NodeType & insertionData) { if( isListEmpty()) { firstNode = createNewNode( insertionData); lastNode = firstNode; nodeCount++; return; } ListNode<NodeType>* newNode = createNewNode( insertionData); newNode -> nextNodePtr = firstNode; firstNode = newNode; nodeCount++; return; }
void list_add_at (List list ,Void ele,int index ){ ListNode node=list->nodes; ListNode newNode= createNewNode(ele); list->nodes= mergeListNodeAt(list->nodes ,index,newNode); list->size++; }
/*create a empty Trie with root*/ struct Node* initialization() { ROOT = createNewNode('#'); ROOT->type = UNCOMPLETED; ROOT->level = 0; return ROOT; }
Node* AppendFactory::createNewNode(Node* parent) { // debug weights if(false) { for(auto link: parent->children) { std::cout << link.weight << " "; } std::cout << std::endl; std::cout << std::endl; } // choose instruction int i = chooseNextInstruction(parent); currentProgram.push_back(i); if(parent->children[i].node == nullptr) { // create and return new node return parent->children[i].node = allocateNode(i, parent); } else { // keep exploring return createNewNode(parent->children[i].node); } }
//Insert Node to Trie int insert(const char chars[],int len,int lineNum) { struct Node* ptr = ROOT; int i = 0; while(i < len) { int tempidx = charToindex(chars[i]); if(ptr->child[tempidx] == NULL) //child doesn't exist { if(ptr->type == LEAF) // at leaf node { ptr->type = COMPOSITE; } if(i < (len - 1)) //intermediate node { ptr->child[tempidx] = createNewNode(chars[i]); ptr->child[tempidx]->type = UNCOMPLETED; ptr->child[tempidx]->level = ptr->level + 1; } if(i == (len - 1)) //leaf node { ptr->child[tempidx] = createNewNode(chars[i]); ptr->child[tempidx]->type = LEAF; ptr->child[tempidx]->lineNum = lineNum; ptr->child[tempidx]->level = ptr->level + 1; ptr->child[tempidx]->content = chars; } ptr = ptr->child[tempidx]; } else if(ptr->child[tempidx] != NULL) // the node already existed { if(i == (len - 1)) { ptr->child[tempidx]->type = COMPOSITE; ptr->child[tempidx]->lineNum = lineNum; ptr->child[tempidx]->content = chars; } ptr = ptr->child[tempidx]; } i++; } return 0; }
int createT(Tree *dic) { Tree root; root = createNewNode(); *dic = root; root -> code = 1; root -> head = 1; return 1; }
//Insert nodes into a linked list void insertLinkedListNode(int value){ if(head==NULL){ head = createNewNode(value); //head->link = NULL; } else{ newNode *llNode= createNewNode(value); //llNode= createNewNode(value); if(llNode){ newNode *temp = head; while(temp->link != NULL){ temp = temp->link; } temp->link = llNode; } } }
struct tree* insert(struct tree* head, int input) { if (head == NULL) return createNewNode(input); if (input >= head->data) head->right = insert(head->right, input); else head->left = insert(head->left, input); return head; }
ProgramInfo AppendFactory::createNewProgram() { // note: the intention here is to clear the vector but retain allocated memory // freeing and reallocating is a waste of time currentProgram.clear(); ProgramInfo p; p.node = createNewNode(root); p.program = currentProgram; return p; }
void insert(const char chars[], int len) { struct Node* ptr = ROOT; int i; for(i = 0; i < len; i++) { if(ptr->child[charToindex(chars[i])] == NULL) { ptr->child[charToindex(chars[i])] = createNewNode(chars[i]); } ptr = ptr->child[charToindex(chars[i])]; } ptr->type = COMPLETED; }
void NGRandomNetBuilder::createNet(int numNodes) { myNumNodes = numNodes; NGNode* outerNode = new NGNode(myNet.getNextFreeID()); outerNode->setX(0); outerNode->setY(0); outerNode->setMaxNeighbours(4); myNet.add(outerNode); myOuterNodes.push_back(outerNode); bool created = true; while (((int) myNet.nodeNo() < numNodes) && (myOuterNodes.size() > 0)) { // brings last element to front if (!created) { myOuterNodes.push_front(myOuterNodes.back()); myOuterNodes.pop_back(); } outerNode = myOuterNodes.back(); findPossibleOuterNodes(outerNode); created = false; if ((myConNodes.size() > 0) && (RandHelper::rand() < myConnectivity)) { // create link NGEdge* newLink = new NGEdge(myNet.getNextFreeID(), outerNode, myConNodes.back()); if (canConnect(outerNode, myConNodes.back())) { // add link myNet.add(newLink); myOuterLinks.push_back(newLink); // check nodes for being outer node if (outerNode->LinkList.size() >= outerNode->getMaxNeighbours()) { removeOuterNode(outerNode); } if (myConNodes.back()->LinkList.size() >= myConNodes.back()->getMaxNeighbours()) { removeOuterNode(myConNodes.back()); } created = true; } else { delete newLink; } } else { int count = 0; do { created = createNewNode(outerNode); count++; } while ((count <= myNumTries) && !created); if (!created) { outerNode->setMaxNeighbours((SUMOReal) outerNode->LinkList.size()); myOuterNodes.remove(outerNode); } } } }
int main(){ node *head; int n; head=NULL; head=createNewNode(11); head->next=createNewNode(12); head->next->next=createNewNode(14); head->next->next->next=createNewNode(15); head->next->next->next->next=createNewNode(16); head->next->next->next->next->next=createNewNode(17); //making a loop //head->next->next->next->next->next->next=head->next->next; head->next->next->next->next->next->next=head->next->next->next->next->next; //1->2->3->4->5->NULL int len; node *temp=NULL; temp=containsLoop(head); if(temp){ printf("\nThe linked list contains the loop"); len=loopLength(head,temp); printf("\nLoop length is:%d",len); if(len==1) temp->next=NULL; else breakLoop(head,len); }else printf("\nThe linked list does not contain any loop"); printf("\nLinked list is:"); printList(head); printf("\n"); return 0; }
node *recursive(node *curr1,node *curr2,int prev){ if(NULL==curr1 || NULL==curr2) return NULL; if(curr1->data<curr2->data) return recursive(curr1->next,curr2,prev); if(curr2->data<curr1->data) return recursive(curr1,curr2->next,prev); if(prev!=curr1->data){ node *temp=createNewNode(curr1->data); temp->next=recursive(curr1->next,curr2->next,temp->data); return temp; }else return recursive(curr1->next,curr2->next,prev); }
void Fadd(char *name, char *tel, char *mail, char *note) { struct node *new_node = createNewNode(name, tel, mail, note); addtoDLL(new_node); if( root == 0) { root = new_node; return; } else { struct node *cur_node = root; while(1) { // strcmp : strin compare strcmp(s1,s2) // return 0. s1 == s2 김철수 == 김철수 // return -1 strcmp(s1, s2) < 0 s1 < s2 : s1==김철수, s2==나철수 if( strcmp(cur_node->name, new_node->name) < 0)//right { if(cur_node->right == 0) { cur_node->right = new_node; return; } else { cur_node = cur_node->right; } } else if( strcmp(cur_node->name, new_node->name) > 0 )//left { if(cur_node->left == 0) { cur_node->left = new_node; return; } else { cur_node = cur_node->left; } } else // strcmp == 0 동일 이름 { printf("same name\n"); return; } } } }
void GraphTranslator::makeBigBang() { for (size_type i = 0; i < mNodeNumber; ++i){ std::string name = (boost::format("%1%-%2%") % mPrefix % i).str(); createNewNode(name, mClass[i]); } for (size_type i = 0; i < mNodeNumber; ++i) { for (size_type j = 0; j < mNodeNumber; ++j) { if (mGraph[j][i]) { connectNodes(j, i); } } } }
void insertSequence(struct tnode *parent, int pos, char letter, int code, int singleChar) { struct tnode *newNode = createNewNode(); newNode -> code = code + 256; newNode -> head = 0; int childCount = strlen(parent -> childChar); char *newChildChar = realloc(parent -> childChar, childCount + 2); newChildChar[childCount] = letter; newChildChar[childCount + 1] = '\0'; parent -> childChar = newChildChar; struct tnode**newChildren = realloc(parent -> children, (childCount + 2) * sizeof(struct tnode)); newChildren[childCount] = newNode; newChildren[childCount + 1] = NULL; parent -> children = newChildren; }
int main() { /* declare variables to be used in program */ char name[15]; char oneChar; int strLength, done = 0, i, age; Node *newPerson; do { /* prompt and read in name and age */ printf("\nPlease enter a name (or 'q' to quit):\n"); scanf("%s", name); strLength = strlen(name); if (strLength == 1 && tlower(name[0] == 'q')) { done = 1; /* we are done! */ } /* end if */ else if (strLength == 0) { printf("\n\t***You must enter a name!***\n\n"); } /* end else */ else /* a valid name has been entered */ { printf("Please enter an age:\n"); scanf("%d", &age); /*Create a new node for this person */ newPerson = (Node *) createNewNode(name, age); /* add this node to the linked list */ add(newPerson); } /* end else statement */ /* proceed if we are not done */ if (!done) { printList(); } /* end if */ } while (!done); printf("\nThis program was created by Harry Staley."); printf("\nEnd of program"); return 0; /*exit program normally */ } /* end function main */
//Insert strings into the trie data structure void trieInsert(newTrie *trie,char word[]){ int j; int index; int wordLength = strlen(word); newNode *nodeLevel; nodeLevel = trie->root; for(j=wordLength-1; j>=0; j--){ index = word[j]-'a'; if(nodeLevel->children[index] == NULL){ nodeLevel->children[index] = createNewNode(); } nodeLevel = nodeLevel->children[index]; } nodeLevel->isWord = 1; nodeLevel->word = word; }
int main(){ node *head1,*head2; int n; head1=NULL; head1=createNewNode(11); head1->next=createNewNode(11); head1->next->next=createNewNode(12); head1->next->next->next=createNewNode(12); head1->next->next->next->next=createNewNode(16); head1->next->next->next->next->next=createNewNode(17); head2=NULL; head2=createNewNode(11); head2->next=createNewNode(12); head2->next->next=createNewNode(12); head2->next->next->next=createNewNode(14); head2->next->next->next->next=createNewNode(16); head2->next->next->next->next->next=createNewNode(19); printf("\nLinked lists are:\n"); printList(head1); printf("\n"); printList(head2); node *head=recursive(head1,head2,-1); printf("\nLinked list is:"); printList(head); printf("\n"); return 0; }
/* This routine initializes the memory pool buffer It allocates memory for an array of Nodes and for an array of node pointers Then it loads all the nodes into a memory in case that the size of on-disk B-tree is less than MAX_NODES_INMEM Otherwise it loads only root node, since we cannot predict what nodes will be used first If btree does not contain any nodes, it creates a root node. In any case it assigns the root node to the lastPath [0] */ int initMemoryPool(SystemState_t *state) { unsigned int nodesInFile,i; int res; MemoryPool_t *memPool; InMemNodeInfo_t *memPoolPointers; BTreeNode_t* node; //1. Allocate memory for memory pool pointer memPool=(MemoryPool_t *) calloc (1, sizeof(MemoryPool_t)); if(memPool==NULL) { printf("Failed to allocate memory for Memory pool of size: %lu \n", (sizeof(MemoryPool_t))); return 1; } // 2. allocate memory for BTree nodes array memPool->nodes=(BTreeNode_t *) calloc (MAX_NODES_INMEM, sizeof(BTreeNode_t)); if(memPool->nodes==NULL) { printf("Failed to allocate memory for BTreeNode_t memory pool of size: %d \n", MAX_NODES_INMEM); return 1; } //3. Allocate memory for node pointers array memPoolPointers=(InMemNodeInfo_t *) calloc (MAX_NODES_INMEM, sizeof(InMemNodeInfo_t)); if(memPoolPointers==NULL) { printf("Failed to allocate memory for InMemNodeInfo_t memory pool pointers of size: %d \n", MAX_NODES_INMEM); return 1; } //5. determine the number of nodes on disk nodesInFile=getBTreeSize(state); //6. set state fields pointers at the allocated arrays state->memPool=memPool; // memory pool is empty, Current free position is 0 state->memPool->currentFreePosition=0; state->memPoolPointers=memPoolPointers; state->maxNodesOnDisk=nodesInFile; //7. Depending on the number of nodes in btree file //7a. No nodes - create root node if(nodesInFile==0) { //it creates and makes it persistent - writes to disk //the last created new Node is always in state->bufferNode node=createNewNode(state, ROOT); memPoolPointers[0].nodeID=0; memPoolPointers[0].isOccupied=TRUE; state->maxNodesOnDisk++; state->curTreeLevel=0; state->lastPath[0]=node; state->memPool->currentFreePosition++; return 0; } //7b. If nodes fit, load them all if(nodesInFile<MAX_NODES_INMEM) { res=fread(&state->memPool->nodes[0],sizeof(BTreeNode_t),nodesInFile,state->btreefile); rewind(state->btreefile); if((unsigned int)res!=nodesInFile) { printf("Error reading Btree nodes from file: supposed to read %u and read %d \n",nodesInFile,res); return 1; } for(i=0;i<nodesInFile;i++) { memPoolPointers[i].nodeID=state->memPool->nodes[i].header.nodeID; memPoolPointers[i].isOccupied=TRUE; } state->curTreeLevel=0; state->lastPath[0]=&(state->memPool->nodes[0]); state->memPool->currentFreePosition=nodesInFile; printf("There were %d nodes in an existing BTree file\n",nodesInFile); state->lastPathCurrentPointers[0]=0; return 0; } //7c. The general situation when file is big so we load only the root node res=fread(&state->memPool->nodes[0],sizeof(BTreeNode_t),1,state->btreefile); rewind(state->btreefile); if(res!=1) { printf("Error reading Btree root from file\n"); return 1; } memPoolPointers[0].nodeID=0; memPoolPointers[0].isOccupied=TRUE; state->curTreeLevel=0; state->lastPath[0]=&(state->memPool->nodes[0]); state->memPool->currentFreePosition=1; state->lastPathCurrentPointers[0]=0; return 0; }
void initialization() { //intiazation: creat an empty tree, with only a ROOT ROOT = createNewNode(' '); }
//Initialize the trie void trieInitialization(newTrie *trie){ trie->root = createNewNode(); }