answersLogoWhite

0

What is a simple node?

Updated: 8/19/2019
User Avatar

Wiki User

7y ago

Best Answer

In computer programming, a simple node is a structure that holds two pointers. The first pointer refers to the data that is indirectly associated with the node, while the other refers to the next node in the sequence. Simple nodes are typically used in forward lists (singly-linked lists) where we keep track of the head node only. The last node in the list refers to NULL as there can be no next node after the last node. We can insert new data into the list knowing only the address of the data. A new node is constructed which then refers to the data and to the current head of the list. The new node then becomes the head of the list. Insertions at the head are performed in constant time. Inserting anywhere else in the list requires that we traverse the nodes recursively, following each node's next pointer, starting from the head, stopping at the node that should come after the new node. That node then becomes the new node's next node (if the node reaches the end of the list, the next node is NULL). Since we traversed recursively, we can return the newly inserted node to the caller, which can then be assigned to the caller's next node. All other recursions simply return the node we traversed to, thus leaving the caller's next node unchanged.

The following example shows a simple node implementation for an ordered list of type int:

typedef struct Node_t { // a simple node

int* data;

Node_t* next;

} Node;

Node* insert (int* data, Node* node) { // insert data

if (!node *node->data > *data ) { // insertion point reached (end recursions)

Node* pnode = malloc (sizeof (Node)); // create a new node

pnode->data = data; // attach the data

pnode->next = node; // refer to the next node (may be NULL)

return pnode; // return the new node so the previous node can be updated

}

// if we get this far, the given node comes before the new data

// delegate to the next node, the return value is the new or existing next node

node->next = insert (data, node->next);

return node; // return the node (it remains the previous node's next node)

}

void print_all (Node* node) { // print the given node and all that follow it

while (node) {

printf ("%d ", *node->data);

node = node->next;

}

}

Node* destroy_all (Node* node) // destroy the given node and all that follow it while (node) {

Node* next = node->next; // refer to the next node (may be NULL)

free (node->data); // release the current node's data

free (node); // release the node itself

node = next; // make the next node current (may be NULL)

}

return node; // always NULL

}

int main(void) { // The test program...

srand ((unsigned) time (NULL)); // seed a random number generator

Node* head = NULL; // the head node of the list (initially NULL for an empty list)

for (int i=0; i<10; ++i) { // insert 10 random integers

int* pint = malloc (sizeof (int)); // allocate a new integer on the free store

if (!pint) break; // allocation failed, stop inserting!

*pint = rand () % 10; // assign a random integer value (range 0:9)

head = insert (pint, head); // insert the data (the return value is the new or existing head)

}

print_all (head); // print all the nodes starting from the head node

head = delete_all (head); // destroy all the nodes starting from the head (returns NULL)

return 0;

}

User Avatar

Wiki User

7y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What is a simple node?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

Where in the heart is the heart beat controlled?

Simple answer is sinus node


Can structure contain a pointer itself?

Yes a simple exp is the link list. struct node { int data; struct node *link; }


What is the difference between an incision and excision of lymph nodes?

The difference between incision and excision of lymph nodes is very simple. The incision of a lymph node is when the biopsy only takes part of the lymph node during surgery. The excision of the lymph node is when the whole lymph node is removed.


What is the definition of a list in programming?

A list is data type which implements a linear data sequence container object with elements that are allocated non-contiguously. To navigate a list, we use a node class. A node refers to an element but also refers to the next and previous nodes in the sequence. A simple node may be defined as follows: template&lt;typename T&gt; struct node { T* data; // link to an element (of some type T) node* next; // link to next node node* prev; // link to previous node };


Write the concepts of linked list?

A simple linked list is built out of nodes. Each node consists of two sections, data and a pointer. The data contains whatever information that this node of your list needs to contain, and the pointer contains the memory address of the next node in your list.


Write an iterative function to search an element in a binary search tree?

_node* search (_node* head, _key key) { _node* node; for (node=head; node != NULL;;) { if (key == node-&gt;key) return node; else if (key &lt; node.&gt;key) node = node-&gt;left; else node = node-&gt;right; } return node; }


How to Print data of all nodes of linked list?

for (node=head; node!=null; node=node-&gt;next) printnode(node);


How to find the mirror image of a binary tree?

Refer to http://cslibrary.stanford.edu/110/BinaryTrees.html void mirror(struct node* node) { if (node==NULL) { return; } else { struct node* temp; // do the subtrees mirror(node-&gt;left); mirror(node-&gt;right); // swap the pointers in this node temp = node-&gt;left; node-&gt;left = node-&gt;right; node-&gt;right = temp; } }


Can we use doubly linked list as a circular linked list?

Yes. The tail node's next node is the head node, while the head node's previous node is the tail node.


Algoritm for deleting the last element from a list?

Given a list and a node to delete, use the following algorithm: // Are we deleting the head node? if (node == list.head) { // Yes -- assign its next node as the new head list.head = node.next } else // The node is not the head node { // Point to the head node prev = list.head // Traverse the list to locate the node that comes immediately before the one we want to delete while (prev.next != node) { prev = prev.next; } end while // Assign the node's next node to the previous node's next node prev.next = node.next; } end if // Before deleting the node, reset its next node node.next = null; // Now delete the node. delete node;


Is null node equal to leaf node?

No. A leaf node is a node that has no child nodes. A null node is a node pointer that points to the null address (address zero). Since a leaf node has no children, its child nodes are null nodes.


Each normal heartbeat in initiated by the?

Each heartbeat begins with an action potential generated at the sinoatrial node or simple call the SAnode.