Question

In: Computer Science

Assume that struct Node{        int item;        Node* link; }; Write function void list_remove(NodePtr& prev_ptr);...

Assume that

struct Node{

       int item;

       Node* link;

};

Write function void list_remove(NodePtr& prev_ptr); The function will remove the node after the node pointed by prev_ptr.

c++

Solutions

Expert Solution

Here is the function and screenshot of how to use it.

// function to remove node after pre_ptr node
void list_remove(Node *&pre_ptr)
{
     // if there is no next node then do nothing
     if (pre_ptr == nullptr || pre_ptr->link == nullptr)
          return;
     else
     {

          Node *temp = pre_ptr->link;          // storing pointer to next node
          pre_ptr->link = pre_ptr->link->link; // pointing the pointer to next->next node
          delete (temp);                       // deleting temp node
     }
     return;
}

Here is the whole code

#include <bits/stdc++.h>
using namespace std;
// structure of Node
struct Node
{
     int item;
     Node *link;
};
// function to remove node after pre_ptr node
void list_remove(Node *&pre_ptr)
{
     // if there is no next node then do nothing
     if (pre_ptr == nullptr || pre_ptr->link == nullptr)
          return;
     else
     {

          Node *temp = pre_ptr->link;          // storing pointer to next node
          pre_ptr->link = pre_ptr->link->link; // pointing the pointer to next->next node
          delete (temp);                       // deleting temp node
     }
     return;
}
int main()
{
     Node *head = nullptr;
     head = new Node();
     head->item = 10l;
     head->link = new Node();
     head->link->item = 200;
     head->link->link = nullptr;
     head->link->link = new Node();
     head->link->link->item = 300;
     head->link->link->link = nullptr;
     list_remove(head);
     cout << head->link->item << endl;
}


Related Solutions

Assume that struct Node { int item; Node* link; }; typedef Node* NodePtr; 1. Write function...
Assume that struct Node { int item; Node* link; }; typedef Node* NodePtr; 1. Write function void list_head_insert(NodePtr& head, int entry); The function should insert a new Node, in which entry is the value of attribute item, in front of the linked list that is pointed by head. 2. Write function void list_head_remove(NodePtr& head); The function will remove the first node from the linked list that is pointed by head. 3. Write function NodePtr list_search(NodePtr head, int target); The function...
#include #include #include int reverse(int); // Stack ADT Type Defintions typedef struct node { void* dataPtr;...
#include #include #include int reverse(int); // Stack ADT Type Defintions typedef struct node { void* dataPtr; struct node* link; } STACK_NODE; typedef struct { int count; STACK_NODE* top; } STACK; /* =============== createStack ============== This algorithm creates an empty stack. Pre Nothing Post Returns pointer to a null stack -or- NULL if overflow */ STACK* createStack(void) { // Local Definitions STACK* stack; // Statements stack = (STACK*)malloc(sizeof(STACK)); if (stack) { stack->count = 0; stack->top = NULL; } // if return...
Consider the following struct that represents a node within a binary tree: struct Node { int...
Consider the following struct that represents a node within a binary tree: struct Node { int data; // Data of interest Node *left // Link to left subtree (nullptr if none) Node *right ; // Link to right subtree (nullptr if none) }; Complete the following function that computes the number of elements in a binary tree: // Counts the number of elements in the binary tree to which t points. // Returns the number of elements. int size(Node *t)...
#include<stdlib.h> #include<stdio.h> typedef struct node {    void* dataPtr;    struct node* next; } QUEUE_NODE; typedef...
#include<stdlib.h> #include<stdio.h> typedef struct node {    void* dataPtr;    struct node* next; } QUEUE_NODE; typedef struct {    QUEUE_NODE* front;    QUEUE_NODE* rear;    int count; } QUEUE; //Prototype Declarations QUEUE* createQueue(void); QUEUE* destroyQueue(QUEUE* queue); bool dequeue(QUEUE* queue, void** itemPtr); bool enqueue(QUEUE* queue, void* itemPtr); bool queueFront(QUEUE* queue, void** itemPtr); bool queueRear(QUEUE* queue, void** itemPtr); int queueCount(QUEUE* queue); bool emptyQueue(QUEUE* queue); bool fullQueue(QUEUE* queue); /*================= createQueue ================ Allocates memory for a queue head node from dynamic memory and returns...
Please do this code with python. Thank you! struct Node {     int data;     struct...
Please do this code with python. Thank you! struct Node {     int data;     struct Node* left;     struct Node* right; }; // Node creation struct Node* newNode(int data) {     struct Node* nn         = new Node;     nn->data = data;     nn->left = NULL;     nn->right = NULL;     return nn; } // Function to insert data in BST struct Node* insert(struct Node* root, int data) {   if (root == NULL)         return newNode(data);     else {...
IN C++ Given a struct Node { int value; Node *left, *right;}; , implement the functions...
IN C++ Given a struct Node { int value; Node *left, *right;}; , implement the functions below. a) int getSmallest(Node * r); // return smallest value in the BST with root r. Assume r not null. b) int getSecondSmallest(Node * r); // return 2nd smallest value in BST with root r. Assume r not null and r has a nonnull left or right child. c) void removeSecondSmallest(Node * r); // remove 2nd smallest value in BST with root r. Assume...
IN C Write a function in the form: void play( int key, int duration) // duration...
IN C Write a function in the form: void play( int key, int duration) // duration units are tenths of a second which generates and prints samples of sin(w*t) for t=0,1,2,...,n-1 which represent a tone corresponding to piano key number key, where: n = (duration/10.0)*8000 w = (2π440rkey-49)/8000 r = 21/12 In the main program call your function to play the first three notes of three blind mice.
Part 1.1 Write a function whose prototype is void exchange ( /*inout*/ int *p, /*inout*/ int...
Part 1.1 Write a function whose prototype is void exchange ( /*inout*/ int *p, /*inout*/ int *q ) that takes two pointers to integer variables and exchanges the values in these variables. Part 1.2 Write a function whose prototype is char lastChar ( /*in*/ const char *str ) that takes a nonempty C-String as parameter and returns the last character in the string. For example the call lastChar ("srjc") will return the character c. Part 1.3 Define an array of...
Given: #include <iostream> using std::cout; template <typename T> struct Node { T data; Node *link;   ...
Given: #include <iostream> using std::cout; template <typename T> struct Node { T data; Node *link;       Node(T data=0, Node *p = nullptr) { //Note, this constructor combines both default and parameterized constructors. You may modify the contructor to your needs this->data = data;        link = p; } }; template <typename T> class linked_list { Node<T> *head,*current; public: //default constructor linked_list() { head = nullptr;//the head pointer current = nullptr;//acts as the tail of the list } //destructor...
Please debug the code and answer the questions: #include <stdio.h> typedef struct node { int value;...
Please debug the code and answer the questions: #include <stdio.h> typedef struct node { int value; struct node *next; } node; int ll_has_cycle(node *first) { node * head = first; while (head->next) { head = head->next; if (head == first) return 1; } return 0; } void test_ll_has_cycle(void) { int i,j; node nodes[5]; for(i=0; i < sizeof(nodes)/sizeof(node); i++) { nodes[i].next = NULL; nodes[i].value = i; } nodes[0].next = &nodes[1]; nodes[1].next = &nodes[2]; nodes[2].next = &nodes[1]; printf("Checking first list for cycles....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT