Question
for this code : #include #include using namespace std; class Node { public: int data; Node *next; }; // inserting node at the begining of
for this code :
#include
class Node { public: int data; Node *next; };
// inserting node at the begining of the list void push(Node** head_ref, int new_data){ Node* new_node = new Node(); new_node->data = new_data; new_node->next =(*head_ref); (*head_ref) = new_node; }
// inserting after node void insertAfter(Node* prev_node, int new_data){ if(prev_node == NULL) { cout
Node* new_node = new Node();
new_node->data = new_data; // 30 20 25 15
new_node->next = prev_node->next; prev_node->next = new_node; }
// Append at the end of linked list // Node* point // Node ** pointer to pointer // Append is for adding to the end of the linked list void append(Node** head_ref, int new_data){ // Creating a new node Node* new_node = new Node(); // creating an object of class Node Node *last = *head_ref; // last pointer to track the current position in the linked list
new_node->data = new_data; // assiging value to the new node new_node->next = NULL; // assiging value to the pointer (next) Null
if(*head_ref == NULL){ // If *head pointer is NULL ( Empty Linked List) *head_ref = new_node; // Head node = new_node; Creating a new Linked list return; }
while(last->next != NULL){ // While the next node is not NULL last = last->next; // Move the pointer (last) to next position. }
last->next = new_node; // last->next assigning to the new_node. return; }
void printLinkedList(Node *node) { while(node != NULL) { coutdata; node = node->next; } }
void deleteNode(Node** head, int value) {
Node* current = *head; Node* prev = NULL;
if (current != NULL && current->data == value) { // delete from the begining of linked list *head = current->next; delete current; return; }
else { while (current != NULL && current->data != value) { // if node in the middle or at the end
prev = current; current = current->next; }
if (current == NULL) { return; }
prev->next = current->next; delete current; return; } }
void main(){ Node* head = NULL;
append(&head,15); push(&head,20); push(&head,30); append(&head,5); append(&head, 3); append(&head, 10); append(&head, 4); insertAfter(head->next,25);
cout
deleteNode(&head, 30); cout
c++ language
1) Write function to search for an item in the linked list? 2) Write a function to find the length of the linked listStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started