Question
void firstNode

Write a function in c++ to insert element after P Activate Windos Go totivate Windows
0 0
Add a comment Improve this question Transcribed image text
Answer #1

`Hey,

Note: If you have any queries related to the answer please do comment. I would be very happy to resolve all your queries.

#include <bits/stdc++.h>
using namespace std;
  
// A linked list node
class Node
{
public:
int data;
Node *next;
};
  
/* Given a reference (pointer to pointer)
to the head of a list and an int, inserts
a new node on the front of the list. */
void push(Node** head_ref, int new_data)
{
/* 1. allocate node */
Node* new_node = new Node();
  
/* 2. put in the data */
new_node->data = new_data;
  
/* 3. Make next of new node as head */
new_node->next = (*head_ref);
  
/* 4. move the head to point to the new node */
(*head_ref) = new_node;
}
  
/* Given a node prev_node, insert a new node after the given
prev_node */
void insertAfter(Node* prev_node, int new_data)
{
/*1. check if the given prev_node is NULL */
if (prev_node == NULL)
{
cout<<"the given previous node cannot be NULL";
return;
}
  
/* 2. allocate new node */
Node* new_node = new Node();
  
/* 3. put in the data */
new_node->data = new_data;
  
/* 4. Make next of new node as next of prev_node */
new_node->next = prev_node->next;
  
/* 5. move the next of prev_node as new_node */
prev_node->next = new_node;
}
  
/* Given a reference (pointer to pointer) to the head
of a list and an int, appends a new node at the end */
void append(Node** head_ref, int new_data)
{
/* 1. allocate node */
Node* new_node = new Node();
  
Node *last = *head_ref; /* used in step 5*/
  
/* 2. put in the data */
new_node->data = new_data;
  
/* 3. This new node is going to be
the last node, so make next of
it as NULL*/
new_node->next = NULL;
  
/* 4. If the Linked List is empty,
then make the new node as head */
if (*head_ref == NULL)
{
*head_ref = new_node;
return;
}
  
/* 5. Else traverse till the last node */
while (last->next != NULL)
last = last->next;
  
/* 6. Change the next of last node */
last->next = new_node;
return;
}
  
// This function prints contents of
// linked list starting from head
void printList(Node *node)
{
while (node != NULL)
{
cout<<" "<<node->data;
node = node->next;
}
}
  
/* Driver code*/
int main()
{
/* Start with the empty list */
Node* head = NULL;
  
// Insert 6. So linked list becomes 6->NULL
append(&head, 6);
  
// Insert 7 at the beginning.
// So linked list becomes 7->6->NULL
push(&head, 7);
  
// Insert 1 at the beginning.
// So linked list becomes 1->7->6->NULL
push(&head, 1);
  
// Insert 4 at the end. So
// linked list becomes 1->7->6->4->NULL
append(&head, 4);
  
// Insert 8, after 7. So linked
// list becomes 1->7->8->6->4->NULL
insertAfter(head->next, 8);
  
cout<<"Created Linked list is: ";
printList(head);
  
return 0;
}

main.cpp Ctrl+S טש1 upper oreuu, 0); me. // Insert 7 at the beginning. // So linked list becomes 7->6->NULL push(head, 7); 10

Kindly revert for any queries

Thanks.

Add a comment
Know the answer?
Add Answer to:
void firstNode Write a function in c++ to insert element after P Activate Windos Go totivate...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Q. write the algorithm for each function in this code: void insert(int x, node*&p) { //cheak...

    Q. write the algorithm for each function in this code: void insert(int x, node*&p) { //cheak if the pointer is pointing to null. if (p==NULL) {     p = new node;     p->key=x;     p->left=NULL;     p->right=NULL; } else {     //Cheak if the element to be inserted is smaller than the root.     if (x < p->key)     {       //call the function itself with new parameters.       insert(x,p->left);     }     //cheak if the alement to be inserted...

  • write a function to insert an element in the middle of a queue. Please use C++...

    write a function to insert an element in the middle of a queue. Please use C++ and make sure its a function. Thanks

  • Write a C++ function to insert new items into an array and move the rest of...

    Write a C++ function to insert new items into an array and move the rest of the contents down. If we assume that there are places left in the array: int array[10] = {1, 2, 3}; There are 7 places left in the array to allow insertion. Below is the header for the function you are to write: void insertInArray(int arr[], const int position, const int newItem)

  • using C++, NOT C language 1. Write a function called insert() to insert a node to...

    using C++, NOT C language 1. Write a function called insert() to insert a node to the beginning of a linked list. The data is passed into the function. For example insert(8) will insert a node at the beginning of the list with the number 8 in the data field. Each node in the linked list is a ListNode struct as discussed in class. 2. Write a function called print() that will traverse the entire linked list and print out...

  • C Programming 23 points Write a function for the following specs: type: void parameters: FILE", int[],...

    C Programming 23 points Write a function for the following specs: type: void parameters: FILE", int[], int size • Behavior: Write the text value of each element from the int[] to the file File format: an integer on each line of the file. Le: 4 3 1 5

  • C++: Find Time Analysis Worst case O() of each member function with explanation: 1) /** insert function:    parameters:...

    C++: Find Time Analysis Worst case O() of each member function with explanation: 1) /** insert function:    parameters: obj    Description: inserts new elements into the correct position in the    sorted array, sliding elements over the position to right, as needed **/ template <typename Object> SortedArray::void insert(const Object &obj) {    if (theSize >= theCapacity)    {        cout << "Error: there is no enough memory space. " << endl;        return;    }    else    {        for (int i = ((2 * theSize) + 1); (i...

  • For my assignment, I'm trying to write Insert function like C++ insert. My function should insert...

    For my assignment, I'm trying to write Insert function like C++ insert. My function should insert one string into other string at a certain position. Inputs: destination : Be my friend today source : good position : 6 Output: Be my good friend today How should I make room in the middle of the string ? This is my code so far. Insert PROC PROC uses edi esi ebx ecx, destination:DWORD , source:DWORD, position:DWORD    mov esi,destination ;add esi address...

  • Can anyone help me write a C function that goes through an input string and insert...

    Can anyone help me write a C function that goes through an input string and insert spaces(" ")infront and after "<",">","|"? For example: if my input string is: HELLO<WO|RLD>!, the output of the function will be a array like: HELLO < WO | RLD > ! Thanks!

  • I need to write a method in java that returns void and takes a String element....

    I need to write a method in java that returns void and takes a String element. ex :public void addelemt(String element) the method job is to add elements to an ArrayList as many times as the person wants. please, no use of a switch or do.

  • Please slove all these questions in C language Write a function insert At Position N ()...

    Please slove all these questions in C language Write a function insert At Position N () for a singly-linked list that has the following declaration and precondition: int insert At Position N (strict node **p Head, int n, int new Data); Precondition: n > 0 and the list always has enough nodes to satisfy the position specified by n. The function should allocate memory for a new node, and initialize it with the new Data value. It should then insert...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT