Question

In: Computer Science

How do I remove a node from a linked list C++? void LinkedList::Remove(int offset){ shared_ptr<node> cursor(top_ptr_);...

How do I remove a node from a linked list C++?

void LinkedList::Remove(int offset){

shared_ptr<node> cursor(top_ptr_);

shared_ptr<node> temp(new node);

if(cursor == NULL) {

temp = cursor-> next;

cursor= temp;

if (temp = NULL) {

temp->next = NULL;

}

}

else if (cursor-> next != NULL) {

temp = cursor->next->next;

cursor-> next = temp;

if (temp != NULL) {

temp->next = cursor;

}

}

}

Solutions

Expert Solution

Please find the updated function below , which removes the node

void LinkedList::Remove(int offset){ //Take the offset

shared_ptr<node> cursor(top_ptr_); // first pointer which will reach the offset

shared_ptr<node> temp(top_ptr_); //Temp pointer which will be behind the offset

int count = 1; // count to match the offset

while(cursor->next ! = NULL){ //iterate entire list

if(cnt == offset){ // if we find the offset

temp->next = cursor ->next; //assign the temp of next to cursor of next(if its 1st node still it wil work)

delete(cursor); // detele the main node;

cout<<" Node removed successfully"<<endl;

return ; // retun from the function

}else{

temp = cursor; // set temp to cursor

cursor = cursor ->next; // move cursor to next node

cnt++; //increase the count

}

//if we are out of the loop means we have iterated all the list then we have not found offset

cout<<"The off set entred is less than or greater than the length of list, please enter correct offset"<<endl;

return;  

}

i have clearly explained each line of the code, it quite straight forward.

Thanks , still if you have any doubt please post a comment.


Related Solutions

Remove the Head element from the code below: public class LinkedList {    class Node{ int...
Remove the Head element from the code below: public class LinkedList {    class Node{ int value; Node nextElement; public Node(int value) { this.value = value; this.nextElement = null; } } public Node first = null; public Node last = null; public void addNewNode(int element) { Node newValueNode = new Node(element);    if(first == null) { first = newValueNode; } else { last.nextElement = newValueNode; } last = newValueNode; } public void displayValues() { Node recent = first; if(first ==...
HOW DO I ACCESS THE HEAD OF A LINKED LIST FROM INT MAIN () WHEN IT'S...
HOW DO I ACCESS THE HEAD OF A LINKED LIST FROM INT MAIN () WHEN IT'S IN ANOTHER CLASS. HERE IS MY CODE FOR MY MAIN AND HEADER FILES. HEADER FILE: #ifndef HANOISTACK_H #define HANOISTACK_H #include <iostream> using namespace std; class HanoiStack { //class private: struct Disc{ //linked list for towers int num; Disc* next; }; Disc* head; public: HanoiStack(){ //constructor head = nullptr; }; //HanoiStack operator+=(const Disc&); //HanoiStack operator<<(const Disc&); void push(int); //push function int pop(); //pop function void...
Consider the following definition of a doubly linked-list: class LinkedList{ public: LinkedList():head(0), tail(0){} ~LinkedList(); void reverse();...
Consider the following definition of a doubly linked-list: class LinkedList{ public: LinkedList():head(0), tail(0){} ~LinkedList(); void reverse(); //reverses the order of elements in the linked list void insert(int value); private: struct Node{ int data; Node* next; Node* prev; }; Node* head; Node* tail; //Add your helper function here that recursively reverses the order of elements in the linked list }; Write the declaration of a helper function in the class provided above that recursively reverses the order of elements in the...
C++ Remove every other node in a circular linked list USING RECURSION. You can only use...
C++ Remove every other node in a circular linked list USING RECURSION. You can only use the preprocessor directives <iostream> and using namespace std; You must use this prototype: int remove_every_other(node *&rear)
In C++, write a member method delete() that deletes a node from a linked list at...
In C++, write a member method delete() that deletes a node from a linked list at a random position. (It should first randomly generate that position. and then delete that node).
Working on a c++ data structures assignment.   Linked List add node. I have the head case...
Working on a c++ data structures assignment.   Linked List add node. I have the head case and the tail case working but the middle/general case I can not get to work for the life of me. I have included the header file and the data struct file below   #ifndef LINKEDLIST_H #define LINKEDLIST_H #include "data.h" #include <iostream>   //take this out using std::cout; class LinkedList{     public:         LinkedList();         ~LinkedList();         bool addNode(int, string);         bool deleteNode(int);         bool getNode(int, Data*);         void printList(bool = false);         int getCount();         void...
Given a doubly linked list in c++, how do I create a function that returns the...
Given a doubly linked list in c++, how do I create a function that returns the pointer to first node in the given pattern, For example, given mainList (a -> b -> c -> d) and sublist  (b -> c), our function should return a Node pointer that points to first node of the sublist in the mainList. If the pattern doesn't exist in the mainList, we should return a nullptr, there are multiple of the same sublist in the mainList,...
PYTHON-- how do i change these linkedlist methods into doublylinkedlist methods? A doublylinked list is the...
PYTHON-- how do i change these linkedlist methods into doublylinkedlist methods? A doublylinked list is the same as a linked list, except that each node has a reference to the node after it(next) and the node before it(prev). #adds item to the head of list. def add(self, item): node = Node(item, self.head) self.head = node if self.size == 0: self.tail = node self.size += 1 #appends item to tail of list def append(self, item): node = Node(item) tail = self.head...
Java program to implement circular linked list. public class CircularLinkedList { private Node tail; private int...
Java program to implement circular linked list. public class CircularLinkedList { private Node tail; private int size; public CircularLinkedList() { tail= null; size = 0; } public int size(){ return size; } public boolean isEmpty() { return size==0; } //if list is not empty return the first element public E first() { if (isEmpty()) return null; //code here return 0; } //if list not empty return last element public E last() { if (isEmpty()) return null; return tail.getElement(); } /*...
how to do circular linked list in c++ (inset and delet and display)
how to do circular linked list in c++ (inset and delet and display)
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT