本文整理汇总了C++中create_node函数的典型用法代码示例。如果您正苦于以下问题:C++ create_node函数的具体用法?C++ create_node怎么用?C++ create_node使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了create_node函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: insert_sorted
/**
* Puts a new node containing i at the appropriate position in a list
* sorted in ascending order.
*/
void insert_sorted(int i)
{
// initalize new node
node* new_node = create_node();
// set new node's i value
new_node->i = i;
// set new node's next value to NULL
new_node->next = NULL;
// check if list is empty
if (first == NULL)
first = new_node;
// then check if i belongs at beginning of list
else if (new_node->i < first->i)
{
new_node->next = first;
first = new_node;
}
// else check if i belongs at end or middle of the list
else
{
// initialize pred_node with same value's as first node's
node* pred_node = first;
while (true)
{
// check for insertion at end of list
if (pred_node->next == NULL)
{
pred_node->next = new_node;
break;
}
// check for insertion in the middle of the list
else if (pred_node->next->i > new_node->i)
{
new_node->next = pred_node->next;
pred_node->next = new_node;
break;
}
// in each loop, set pred_node to the next node in the linked list
pred_node = pred_node->next;
}
}
}
示例2: create_node
void LinkedList::add_at_n (int data,int n)
{
node *temp = create_node(data);
node *temp2 = head;
for(int i=0; i< n-2;i++)
{
temp2 = (*temp2).link;
}
node* temp3 = (*temp2).link;
(*temp2).link = temp;
(*temp).link = temp3;
}
示例3: push_back
/** push_back
*
* Adds the data to the back/end of the linked list
*
* @param llist a pointer to the list.
* @param data pointer to data the user wants to store in the list.
*/
void push_back(list* llist, void* data)
{
node *newTail = create_node(data);
if (llist->size == 0) {
llist->head = newTail;
llist->tail = newTail;
} else {
node *oldTail = llist->tail;
oldTail->next = newTail;
llist->tail = newTail;
newTail->prev = oldTail;
}
llist->size++;
}
示例4: main
int main(int argc,char* argv[])
{
struct node *first = NULL;
struct node *last = NULL;
struct node *p = NULL;
if ( (first = create_node()) == NULL )
{
return 0;
}
if ( (first = add_to_node( first, 0)) == NULL )
{
printf("add false!\n");
}
last = first;
if ( (first = add_to_node( first, 0)) == NULL )
{
printf("add false!\n");
}
if ( (first = add_to_node( first, 1)) == NULL )
{
printf("add false!\n");
}
if ( (first = add_to_node( first, 2)) == NULL )
{
printf("add false!\n");
}
if ( (first = add_to_node( first, 3)) == NULL )
{
printf("add false!\n");
}
first = delete_node(first);
show(first);
printf("\b\n");
show(last);
printf("\b\n");
return 0;
}
示例5: createMinBST
//creates a bst with the minimum height
bst* createMinBST(int a[], int start, int end){
if (start > end)
return NULL;
int middle = (start+end)/2;
bst* root = create_node(a[middle]);
root->left = createMinBST(a, start, middle-1);
root->right = createMinBST(a, middle + 1, end);
return root;
}
示例6: ft_strlen
t_cmd *create_last_node(char *str, int **occ)
{
t_cmd *node;
int cmd_len;
node = NULL;
if (str)
{
cmd_len = ft_strlen(str);
node = create_node(str, NULL);
(*occ)++;
}
return (node);
}
示例7: prepend
/**
* Puts a new node containing i at the front (head) of the list.
*/
void prepend(int i)
{
// initalize new node
node* new_node = create_node();
// set new node's i value
new_node->i = i;
// set new node's next pointer to first node in list
new_node->next = first;
// new node now becomes first/front/head node in list
first = new_node;
}
示例8: merge_two
Node * merge_two(Node * head)
{//merge the last two nodes in the list into one tree node
if (head -> next -> next == NULL)
{//we are at the left child to merge, head -> next is the right child
Node * newParent = create_node(0);
newParent -> left = head;
newParent -> right = head -> next;
newParent -> left -> next = NULL;
return (newParent);
}
//head -> next becomes the new parent
head -> next = merge_two(head -> next);
return (head);
}
示例9: init
void init(const char *s)
{
int i;
int freq[128] = {0};
char cxt[16];
while (*s) {
freq[(int)*s]++;
s++;
}
for (i = 0; i < 128; i++) {
if (freq[i]) {
insert(create_node(freq[i], i, 0, 0));
}
}
while (tail > 2) {
insert(create_node(0, 0, pop(), pop()));
}
prepare(tree[1], cxt, 0);
}
示例10: setup_tree
errcode_t setup_tree(root_t **roots, const char *word)
{
root_t *root;
node_t *p;
int len, i, idx;
char c;
len = strlen(word);
assert(roots && word && len > 0);
if ((c = to_lowercase(word[0])) < 0) {
return 0;
}
idx = c - 'a';
root = roots[idx];
for (i = 1, p = root->n; i < len; i++) {
/* Illegal word, skip it, resulting in leaf node's cnt == 0 */
if ((c = to_lowercase(word[i])) < 0) {
return 0;
}
idx = c - 'a';
if (p->children[idx] != NULL) {
p = p->children[idx];
continue;
}
pthread_mutex_lock(&root->mutex);
if (!p->children[idx]) {
if (!(p->children[idx] = create_node(c))) {
pthread_mutex_unlock(&root->mutex);
return ERR_NO_MEM;
}
}
pthread_mutex_unlock(&root->mutex);
p = p->children[idx];
}
/* update counter on the leaf node */
pthread_mutex_lock(&root->mutex);
p->cnt++;
pthread_mutex_unlock(&root->mutex);
return 0;
}
示例11: append
/**
* Puts a new node containing i at the end (tail) of the list.
*/
void append(int i)
{
// initalize new node
node* new_node = create_node();
// set new node's i value
new_node->i = i;
// set new node's next value to NULL
new_node->next = NULL;
// insert node immediately following previous node
insert_node(new_node);
}
示例12: create_node
Node* Parser::parse_expression()
{ // expects TOKEN_INTEGER, TOKEN_WORD or TOKEN_OPENING_PAREN as current token
Node *node = create_node(NODE_EXPRESSION);
if (_current->kind == TOKEN_INTEGER) {
Node *node_const = create_node(NODE_CONSTANT);
node_const->constant_number = _current->integer;
node->next.push_back(node_const);
} else if (_current->kind == TOKEN_WORD) {
Node *node_word = create_node(NODE_WORD);
node_word->word_word = _current->word;
node->next.push_back(node_word);
} else if (_current->kind == TOKEN_OPENING_PAREN) {
node->next.push_back(parse_function_call());
} else {
printf("Error: unknown expression: ");
_current->print();
}
next_token();
return node;
}
示例13: push_back
/** push_back
*
* Adds the data to the back/end of the linked list
*
* @param llist a pointer to the list.
* @param data pointer to data the user wants to store in the list.
*/
void push_back(list* llist, void* data)
{
node* add_node = create_node(data);
if(llist->head == NULL && llist->tail == NULL){
llist->head = add_node;
llist->tail = add_node;
} else{
add_node->prev = llist->tail;
llist->tail->next = add_node;
llist->tail = add_node;
}
llist->size += 1;
}
示例14: addNode
Node * addNode(Node *node, int value){
if(node == NULL){
return create_node(value);
}
else{
if (node->value > value){
node->left = addNode(node->left, value);
}
else{
node->right = addNode(node->right, value);
}
}
return node;
}
示例15: add_iterative
void add_iterative(struct linkedlist* l, int x)
//@ requires list(l, ?vs);
//@ ensures list(l, append(vs, cons(x, nil)));
{
//@ open list(l, vs);
if(l->head == 0) {
struct node* n = create_node(0, x);
l->head = n;
//@ open lseg(0, 0, _);
//@ close lseg(0, 0, _);
//@ close lseg(n, 0, cons(x, nil));
//@ close list(l, append(vs, cons(x, nil)));
} else {
struct node* head = l->head;
struct node* current = l->head;
//@ close lseg(head, head, nil);
//@ open lseg(head, 0, vs);
while(current->next != 0)
//@ invariant current!= 0 &*& lseg(head, current, ?vs1) &*& current->value |-> ?v &*& current->next |-> ?n &*& malloc_block_node(current) &*& lseg(n, 0, ?vs2) &*& vs == append(vs1, cons(v, vs2));
{
//@ open lseg(n, 0, _);
struct node* oldcurrent = current;
current = current->next;
//@ appendlemma(head, oldcurrent);
//@ appendlemma2(vs1, v, vs2);
}
//@ open lseg(0, 0, _);
struct node* nn = create_node(0, x);
current->next = nn;
//@ close lseg(0, 0, nil);
//@ close lseg(nn, 0, _);
//@ close lseg(current, 0, _);
//@ appendlemma3(head, current);
//@ appendlemma2(vs1, v, cons(x, nil));
//@ close list(l, append(vs, cons(x, nil)));
}
}