C++
Write a code snippet to insert( or remove) a data value from a linked list.
Don't use plagiarized sources. Get Your Custom Essay on
Answered! C++ Write a code snippet to insert( or remove) a data value from a linked list….
GET AN ESSAY WRITTEN FOR YOU FROM AS LOW AS $13/PAGE
Expert Answer
// Link list node
struct node
{
int data;
struct node* next;
};
// Function to insert node
void push(struct node** refNode, int newData)
{
// allocate node
struct node* newNode =
(struct node*) malloc(sizeof(struct node));
// put in the data
newNode->data = newData;
// link to list
newNode->next = (*refNode);
// move to head node
(*refNode) = newNode;
}
// Function to delete the entire linked list
void deleteList(struct node** refNode)
{
struct node* current = *refNode;
struct node* next;
// loops untill it match end of node and shift the positions
while (current != NULL)
{
next = current->next;
free(current);
current = next;
}
}