示例#1
0
//插入节点  
bool insert(skiplist *sl,int key,int value)  
{  
    nodeStructure *update[MAX_LEVEL];  
    nodeStructure *p, *q = NULL;  
    p=sl->header;  
    int k=sl->level;  
    //从最高层往下查找需要插入的位置  
    //填充update  
    for(int i=k-1; i >= 0; i--){  
        while((q=p->forward[i])&&(q->key<key))  
        {  
            p=q;  
        }  
        update[i]=p;  
    }  
    //不能插入相同的key  
    if(q&&q->key==key)  
    {  
        return false;  
    }  
    
    //产生一个随机层数K  
    //新建一个待插入节点q  
    //一层一层插入  
    k=randomLevel();  
    //更新跳表的level  
    if(k>(sl->level))  
    {  
        for(int i=sl->level; i < k; i++){  
            update[i] = sl->header;  
        }  
        sl->level=k;  
    }  
    
    q=createNode(k,key,value);  
    //逐层更新节点的指针,和普通列表插入一样  
    for(int i=0;i<k;i++)  
    {  
        q->forward[i]=update[i]->forward[i];  
        update[i]->forward[i]=q;  
    }  
    return true;  
}  
示例#2
0
void LinkedList::appendNodeBack (int key)
{
	node *newNode = createNode(key);

	//if tree is empty
	if (head == NULL)
	{
		head = newNode;
		return;
	}

	//if tree is not empty
	//traverse to the last node in the list
	node *temp = head;
	while (temp->next != NULL)
		temp = temp->next;

	temp->next = newNode;
}
AglLooseBspTreeConstructor::AglLooseBspTreeConstructor(unsigned int p_targetMesh, vector<AglVector3> p_vertices, vector<unsigned int> p_indices)
{
	int count = 1;
	while (count < (int)(p_indices.size() / 3))
	{
		count *= 2;
	}
	count += count-1;
	m_nodes = vector<AglBspNode>(count);
	m_root = 0;
	m_targetMesh = p_targetMesh;

	for (unsigned int i = 0; i < p_indices.size() / 3; i++)
	{
		AglBspTriangle t(i, p_vertices, p_indices);
		m_triangles.push_back(t);
	}
	m_root = createNode(0, m_triangles.size(), 0);
}
示例#4
0
void lladdNpos(linklist* ll,data* d,int n){

  int i = 1;
  node* newNode = createNode(d);
  node* temp = ll->head;
  node* prev = NULL;
  for(i;i < n;i++){

    prev = temp;
    temp = prev->next;
  }
  if(i == 1){

    temp->next = ll->head;
    ll->head = temp;
  }
  prev->next = newNode;
  newNode->next = temp;
}
示例#5
0
bool Client::_setupClient( const std::string& clientArgs )
{
    LBASSERT( isListening( ));
    if( clientArgs.empty( ))
        return true;

    size_t nextPos = clientArgs.find( CO_SEPARATOR );
    if( nextPos == std::string::npos )
    {
        LBERROR << "Could not parse working directory: " << clientArgs
                << std::endl;
        return false;
    }

    const std::string workDir = clientArgs.substr( 0, nextPos );
    std::string description = clientArgs.substr( nextPos + 1 );

    Global::setWorkDir( workDir );
    if( !workDir.empty() && chdir( workDir.c_str( )) == -1 )
        LBWARN << "Can't change working directory to " << workDir << ": "
               << lunchbox::sysError << std::endl;

    nextPos = description.find( CO_SEPARATOR );
    if( nextPos == std::string::npos )
    {
        LBERROR << "Could not parse server node type: " << description
                << " is left from " << clientArgs << std::endl;
        return false;
    }

    co::NodePtr server = createNode( fabric::NODETYPE_SERVER );
    if( !server->deserialize( description ))
        LBWARN << "Can't parse server data" << std::endl;

    LBASSERTINFO( description.empty(), description );
    if( !connect( server ))
    {
        LBERROR << "Can't connect server node using " << *server << std::endl;
        return false;
    }

    return true;
}
示例#6
0
int insert(trie_t  * troot, char key[]){

    int i, index;
    trieNode_t *temp  = troot->root;
    int len = strlen(key);

    for(i=0;i<len;i++){

        index = key[i]-'a';
        if(!temp->child[index]){
            temp->child[index] = createNode();
        }

        temp = temp->child[index];
    }
    troot->count++;

    temp->eos =troot->count ;
}
示例#7
0
VolumeOctreeNode* VolumeOctreeBase::createNode(size_t numChannels) {
    tgtAssert(numChannels > 0 && numChannels <= 4, "number of channels must be between 1 and 4");
    uint16_t* avgValues = new uint16_t[numChannels];
    uint16_t* minValues = new uint16_t[numChannels];
    uint16_t* maxValues = new uint16_t[numChannels];
    for (size_t c=0; c<numChannels; c++) {
        avgValues[c] = 0;
        minValues[c] = 0;
        maxValues[c] = 0;
    }

    VolumeOctreeNode* node = createNode(numChannels, avgValues, minValues, maxValues);

    delete[] avgValues;
    delete[] minValues;
    delete[] maxValues;

    return node;
}
示例#8
0
int main() {
	int val;
	printf("Enter some numbers, ending with -1: ");
	while (1) {
		scanf("%d",&val);
		if (val == -1) {
			break;
		}
		IntNode *node = createNode(val);
		//printf("%d\n",val);
		if (first == NULL) {
			first = node;
		}
		//printf("first is: %d\n",first -> item);
		insert(node,val);
	}

	printAll(first);
}
示例#9
0
NodeT *insertNode(NodeT *root, int value)
{
    if(root == NULL)
    {
        NodeT *p=createNode(value);
        return p;
    }
    else
    {
        if(value > root->data)
            root -> right = insertNode(root->right, value);
        else
            root -> left = insertNode(root->left, value);

        root->height = maxim(compHeight(root->left), compHeight(root->right)) +1;

        return Balance(root);
    }
}
示例#10
0
文件: main.c 项目: Alecs94/DSA-lab
NodeT *getTreeFromList()
{
    NodeT *p;
    int data;
    data=head->data;
    head=head->next;
    if (data=='*')
    {
        return NULL;
        //printf("amajunsaici ");
    }
    else
    {
        p=createNode(data);
        p->left=getTreeFromList( );
        p->right=getTreeFromList();
    }
    return p;
}
示例#11
0
END_TEST

START_TEST(replaceOldNode) {
    UA_Node* n1 = createNode(0,2253);
    ns.insertNode(ns.context, n1, NULL);
    UA_NodeId in1 = UA_NODEID_NUMERIC(0,2253);
    UA_Node* n2;
    UA_Node* n3;
    ns.getNodeCopy(ns.context, &in1, &n2);
    ns.getNodeCopy(ns.context, &in1, &n3);

    /* shall succeed */
    UA_StatusCode retval = ns.replaceNode(ns.context, n2);
    ck_assert_int_eq(retval, UA_STATUSCODE_GOOD);

    /* shall fail */
    retval = ns.replaceNode(ns.context, n3);
    ck_assert_int_ne(retval, UA_STATUSCODE_GOOD);
}
示例#12
0
int main(int argc, char **argv) {
	int i, n;
	Node *root;
	while (scanf("%s", string) != EOF) {
		poolPos = 0;
		n = strlen(string);
		root = createNode();
		for (i = 0; i < n; i++)
			addToTrie(root, i, n);
		
		findSuffix(root);
		getSuffix(root, 0);
		printf("%s\n", suffix);
		
//		destroyNode(root);
	}
	
	return EXIT_SUCCESS;
}
示例#13
0
文件: F.c 项目: jpbat/aed2011
nodePtr rootInsertion(char* word, nodePtr leaf)
{
	if(leaf == NULL)
		return createNode(word);

	if(strcmp(word,leaf->info.word ) < 0)
	{
		leaf->left = rootInsertion(word, leaf->left);
		leaf = rightRotation(leaf);
	}
	
	else if((strcmp(word,leaf->info.word) > 0))
	{
		leaf->right = rootInsertion(word,leaf->right);
		leaf = leftRotation(leaf);
	}
	
	return leaf;
}
示例#14
0
int main()
{
    char line[BUFSIZE];
    char name[NAME]; /*name of the sale itme*/
    double uPrice, sMass; /*unit price and sold mass of the item*/
    pSALE head;
    pSALE sd;
    FILE *stream;
    /*** Read the sales data into four structures s1,s2,s3,s4 and link them together ***/
    stream = fopen("sale.dat","r");
    if(stream == NULL)
    {
        fprintf(stderr,"Error: cannot open 'sale.dat'\n");
        return NULL;
    }
    head = NULL;
    fgets(line, BUFSIZE, stream); // read a line into a buffer
    while(!feof(stream)) {
        while(line[0] == '#') {
            fgets(line,BUFSIZE,stream);
        }
        sscanf(line, "%s%lf%lf",name,&uPrice,&sMass);
        sd = createNode(name,uPrice,sMass);
        appendNode(&head, sd);
        printf("%s %lf %lf\n",sd->name, sd->unitPrice, sd->soldMass);
        fgets(line,BUFSIZE,stream);
    }
    fclose(stream);
    double maxWeight, maxDollar;
    pSALE tmp = head;
    while(tmp->next) {
        if(maxWeight < tmp->soldMass)
            maxWeight = tmp->soldMass;
        if(maxDollar < ((tmp->soldMass)*(tmp->unitPrice)))
            maxDollar = (tmp->soldMass)*(tmp->unitPrice);
        tmp = tmp->next;
    }
    printf("The largest volume in weight is %lf\n", maxWeight);
    printf("The largest volume in weight is %lf\n", maxDollar);
// clear the link list
    clearList(head);
    return 0;
}
示例#15
0
/* Insert data at appropriate place in BST, return new tree root. */
struct TreeNode* insertBST(struct TreeNode* root, int data)
{
	if(root == NULL)
	{
		return createNode(data);
	}
	else
	{
		if (data <= root->data)
		{
			root->left = insertBST(root->left, data);
		}
		else
		{
			root->right = insertBST(root->right, data);
		}
		return root;
	}
}
示例#16
0
int restoreState (char *file) {
    char type = 0, whiteSpace = 0;
    char buff[128];
    FILE *fp = fopen(file, "r");
    
    if (fp == NULL) {
        return 0;
    }
    
    fscanf(fp, "%c", &type);
    fscanf(fp, "%s", buff);
    fscanf(fp, "%c", &whiteSpace);
    
    if ((buff[0] != '/') || (type != 'D')) {
        printf("\tReload file not in proper format.\n");
        return 0;
    }
    // Set up tree to be restored
    cwd = root;
    if (root->childPtr != NULL) {
        // Dealocate Previous tree if it exists 
        recursiveDeallocateNode(root);
        root->childPtr = NULL;
    }
    
    while (!feof(fp)) {
        fscanf(fp, "%c", &type);
        fscanf(fp, "%s", buff);
        fscanf(fp, "%c", &whiteSpace);
        if (!feof(fp)) {
            strcpy(pathname, buff);
            //pathname[strlen(pathname) + 1] = '\n';
            validateAndParsePathname();
            printf("Recovery attempt: %c %s\n", type, buff);
            createNode(type);
        } else {
            break;
        }
    }
    
    fclose(fp);
    return 1;
}
示例#17
0
int main(int argc, char *argv[])
{
  struct Node *head = createNode(11.0);
  
  append(head, 22.1);
  append(head, 33.2);
  append(head, 44.3);

  printList(head);
  
  insertIntoList(head, 2, 999.99);
  
  printList(head);
  
  printf("now deleting list\n");
  deleteList(head);
  
  return 0;
}
示例#18
0
bool LocalNode::_cmdGetNodeDataReply( Command& command )
{
    EQASSERT( _inReceiverThread( ));

    const NodeGetNodeDataReplyPacket* packet = 
        command.get< NodeGetNodeDataReplyPacket >();
    EQVERB << "cmd get node data reply: " << packet << std::endl;

    const uint32_t requestID = packet->requestID;
    const NodeID& nodeID = packet->nodeID;

    // No locking needed, only recv thread writes
    NodeHash::const_iterator i = _nodes->find( nodeID );
    if( i != _nodes->end( ))
    {
        // Requested node connected to us in the meantime
        NodePtr node = i->second;
        
        node->ref( CO_REFERENCED_PARAM );
        serveRequest( requestID, node.get( ));
        return true;
    }

    if( packet->nodeType == NODETYPE_CO_INVALID )
    {
        serveRequest( requestID, (void*)0 );
        return true;
    }

    // new node: create and add unconnected node
    NodePtr node = createNode( packet->nodeType );
    EQASSERT( node.isValid( ));

    std::string data = packet->nodeData;
    if( !node->deserialize( data ))
        EQWARN << "Failed to initialize node data" << std::endl;
    EQASSERT( data.empty( ));

    node->ref( CO_REFERENCED_PARAM );
    serveRequest( requestID, node.get( ));
    return true;
}
示例#19
0
void buildTree(void) {
	buildQueue();
	printQueue();

	int lvalue,rvalue;
	if ( !head ) exit(EXIT_FAILURE);
	while ( head -> right ) {
		Node* temp = createNode();
		temp 	-> bottomleft = head;
		temp	-> bottomright = head -> right;
		head 	=  head  -> right -> right;

		temp	-> bottomleft	-> right = NULL;
		temp	-> bottomright	-> right = NULL;
		
		if ( temp -> bottomleft -> data.datatype.type == LETTER ) 
			lvalue	= characters[ temp -> bottomleft -> data.dataletter.letter ].count;
		else 	lvalue	= temp -> bottomleft -> data.datanum.num;

		if ( temp -> bottomright -> data.datatype.type == LETTER ) 
			rvalue = characters[ temp -> bottomright -> data.dataletter.letter ].count;
		else rvalue	= temp -> bottomright -> data.datanum.num;

		temp -> data.datatype.type = NUMBER;
		temp -> data.datanum.num= lvalue + rvalue;

		if ( !head ) head	= temp;
		else addToQueue( temp );
		}
	buffer	= (unsigned char*)malloc	( currsize );
	traverseTree(head,0UL);
	
	//creation of code is done.

	// tester started..
	for ( int var = 0; var <128 ;++ var ) {
		if ( characters[var].count ){
			printf("%c\t%s\n",var,characters[var].bitstring);
		//	writeBits( characters[var].bitstring );	
			}
		}
	}
示例#20
0
/**
 * The exploration policy for selecting the next node
 */
treeNode* nextNode(treeNode* current, POLICY p) {
	int ret,move;
	if ((ret = _DOM->getGameStatus(current->rep))!= _DOM->incomplete) {
		return NULL;
	}
	if (current->n == 0){
		move = validMove(current);	
		rep_t newRep = _DOM->cloneRep(current->rep);
		int newSide = current->side; 
		_DOM->makeMove(newRep,&newSide, move); 
		current->children[move] = createNode(newRep,newSide,current);
		return current->children[move];
	}
	if (p == EXPLORATION){
		move = validRandomMove(current);
	} else {
		move =  selectMoveExploitation(current);
	}
	return current->children[move];
}
示例#21
0
// adds a new node at a specific location
void insertAt(int value, Node **head, Node **tail, int loc) {
    int cnt = count(*head);
    Node *node = createNode(value);

    if (cnt < loc || loc < 0) {
        append(node, head, tail);
    } else if (loc == 0) {
        node -> next = *head;
        *head = node;
    } else {
        Node *ptr = *head;

        while (--loc > 0) {
            ptr = ptr -> next;
        }

        node -> next = ptr -> next;
        ptr -> next = node;
    }
}
示例#22
0
//////////////////////////////////////////////////////////////////////////
// subDivide
void BcOctTree::subDivide( BcOctTreeNode* pNode )
{
	//
	BcVec3d Center = pNode->AABB_.centre();

	// Create the 8 corners and setup child nodes.
	for( BcU32 i = 0; i < 8; ++i )
	{
		// Create AABB for this corner.
		BcAABB NewAABB;

		NewAABB.expandBy( Center );
		NewAABB.expandBy( pNode->AABB_.corner( i ) );

		// Setup child node.
		pNode->aChildNodes_[ i ] = createNode( NewAABB );
		pNode->aChildNodes_[ i ]->pParent_ = pNode;
		pNode->aChildNodes_[ i ]->pTree_ = this;
	}
}
示例#23
0
LinkedListImpl::IteratorImpl LinkedListImpl::insert(LinkedListImpl::IteratorImpl before, const int &value) {
	list_length++;

    Node *pNodePrev = before.pCurrentNode->prev;
	Node *pCurrentNode = before.pCurrentNode;
	Node *pNewNode = createNode(value);

	if (!pNodePrev) {
		pNewNode->next = head;
		head->prev = pNewNode;
		head = pNewNode;
		return LinkedListImpl::IteratorImpl(head);
	}

	pNodePrev->next = pNewNode;
	pNewNode->prev = pNodePrev;
	pNewNode->next = pCurrentNode;
	pCurrentNode->prev = pNewNode;
	return LinkedListImpl::IteratorImpl(pNewNode);
}
VX_API_ENTRY vx_node VX_API_CALL vxTensorAddNode(vx_graph graph, vx_tensor input1, vx_tensor input2, vx_enum policy, vx_tensor output)
{
    vx_node node = NULL;
    vx_context context = vxGetContext((vx_reference)graph);
    if (vxGetStatus((vx_reference)context) == VX_SUCCESS) {
        vx_scalar s_policy = vxCreateScalarWithSize(context, VX_TYPE_ENUM, &policy, sizeof(policy));
        if (vxGetStatus((vx_reference)s_policy) == VX_SUCCESS)
        {
            vx_reference params[] = {
                (vx_reference)input1,
                (vx_reference)input2,
                (vx_reference)s_policy,
                (vx_reference)output
            };
            node = createNode(graph, VX_KERNEL_TENSOR_ADD, params, sizeof(params) / sizeof(params[0]));
            vxReleaseScalar(&s_policy);
        }
    }
    return node;
}
示例#25
0
NodeID NodeTree::duplicateNode(NodeID nodeID)
{
    if(!validateNode(nodeID))
        return InvalidNodeID;
    NodeTypeID typeID = _nodes[nodeID].nodeTypeID();

    // Generate unique name
    std::string newNodeTitle = generateNodeName(typeID);

    // Create node of the same typeID
    NodeID newNodeID = createNode(typeID, newNodeTitle);
    if(newNodeID == InvalidNodeID)
        return InvalidNodeID;

    // Set properties
    for(const auto& prop : _nodes[nodeID].config().properties())
        _nodes[newNodeID].setProperty(prop.propertyID(), _nodes[nodeID].property(prop.propertyID()));

    return newNodeID;
}
示例#26
0
//Creates a new Graph.
Graph* createGraph(char* file)
{
	FILE *fp = fopen(file,"r");
	int n,m;
	fscanf(fp,"%d %d\n",&n,&m);
	Graph* graph = (Graph*)malloc(sizeof(Graph));
	graph->arr = (GNode*)malloc(sizeof(GNode)*n);
	int i,src,dst,wt;
	for(i=0;i<n;i++)
		createNode(i,n,m,graph);

	for(i=0;i<m;i++)
	{
		fscanf(fp,"%d %d %d\n",&src,&dst,&wt);
		addEdge(graph->arr+src,graph->arr+dst,graph,wt);
	}
	graph->nodes = n;
	graph->edges = m;
	return graph;
}
示例#27
0
int main(void){

    Node *n;
    Node *head = createNode("", "S", 
        createNode("Пливуть ", "V", NULL, NULL),
        createNode("", "NP", 
            createNode("", "AP", 
                createNode("осінні ", "A", NULL, NULL), 
                createNode("тихі ", "A", NULL, NULL)),
            createNode("небеса", "N", NULL, NULL)));

    print(head);

    // Modify tree
    n = findParentOfNode(head, "тихі ");    
    n->right = createNode("білі ", "A", NULL, NULL);

    print(head);


    // TODO cleanup
}
	void addNode(List l ,void * data , HeapHandler hh){

		Node * n = createNode(data ,hh);

		if(l->elements == NULL){
			l->elements = n;
		}else{
			Node * aux = l->elements;
			while(aux->next != NULL
					&& l->sortingCriteria(aux->data , n->data)){
				aux = aux->next;
			}
			if( aux->next != NULL){
				n->next = aux->next;
			}
			aux->next = n;
		}

		l->size++;	
	}
示例#29
0
/*PROBABLY or MOST LIKELY DOESN"T WORK!!!!!!!*/
node * addBack (node * head, Sparse * num)
{
  node * newNode = NULL;
  node * back = head;
  
  if(head == NULL)
  {
    newNode = addFront(head, num);
  }
  else
  {
    while(back->next != NULL)
      back = back->next;
    newNode = createNode(num);
    back->next = newNode;
    newNode->next = NULL;
  }
  
  return head;
}
示例#30
0
int enqueue(void* queueAddress,void* data, size_t priority,compare* compare){
     List* queue = queueAddress;
    element* node = createNode(data,priority);
    element *previous,*next,*temp;
    int result;
    temp = node->data;
    if(queue->length == 0)
        return insertNode(queue, queue->length, node);
    if(compare(&node->priority,&temp->priority) < 0)
        return insertNode(queue,0, node);
    while(temp != NULL){
        previous = temp->data;
        temp->data = queue->head->next;
        if(node->priority < next->priority){
                previous->data = node;
                    return ++queue->length;
        }
    };
    return 0;
};