Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++ void CLL::removeNext(SNode *n) { // Longest method - given node n, remove th

ID: 3708323 • Letter: C

Question

C++

void CLL::removeNext(SNode *n) {
// Longest method - given node n, remove the next node in the linked list.
// you have 4 conditions:
//1: there's only one node in the list, in which case you delete that node and set the size to 0
//2: n's next node is a last node, in which case n becomes the new last node and n's next must point // to the first node in the list (remember to decrease the size of the list)
//3: n's next node is the first node in the list (i.e., n is the last node in the list), in which case

// n's next becomes first's next, first is deleted, and first becomes n's next (again, remember

// to decrease the size)
//4: n is just some node in the middle of the list, in which case you should create a tmp node to point

// to n's next, then set n's next to n's next's next, and then delete the tmp node (again, decrease

// size).
}

Explanation / Answer

#include <iostream>

using namespace std;

class node {
public:
int data;
//pointer to the next node
node* next;
  
node() {
data = 0;
next = NULL;
}
  
node(int x) {
data = x;
next = NULL;
}
};

class CLL {
int size;
node *first;
//declaring the functions
public:
  
//function to add Node at front
void addAtFront(node *n);
  
//function to delete any Node
void removeNext(node *n);
  
CLL() {
first = NULL;
size=0;
}
void printList();
};

void CLL :: addAtFront(node *n) {
int i = 0;
/* If the list is empty */
if(first == NULL) {
n->next=n;
//making the new Node as Head
first = n;
size++;
}
else {
n->next = first;
node *last = first;
//get the Last Node and make its next point to new Node
while (last->next != first)
last = last->next;
last->next = n;
//also make the head point to the new first Node
first = n;
size++;
}
  
}

void CLL :: removeNext(node* n) {

//1:only one node
if(n->next == NULL) {
size=0;
delete n;
}
//2: n's next node is a last node, in which case n becomes the new last node and n's next must point
// to the first node in the list
else if(n->next->next == first){
delete n->next;
n->next = first;
size--;
}
//3: n's next node is the first node in the list (i.e., n is the last node in the list), in which case
// n's next becomes first's next, first is deleted, and first becomes n's next (again, remember
// to decrease the size)
else if(n->next == first)
{
n->next = first->next;
delete first;
first=n->next;
size--;
}
//4: n is just some node in the middle of the list, in which case you should create a tmp node to point
// to n's next, then set n's next to n's next's next, and then delete the tmp node (again, decrease
// size).
else{
node *tmp = n->next;
n->next=tmp->next;
delete tmp;
size--;
}
}

void CLL ::printList()
{
if (first != NULL)
{
node *temp = first;
do
{
cout<<temp->data<<"->";
temp = temp->next;
}
while (temp != first);
}

cout<<"NULL ";
}

int main()
{
  
  
CLL cll1;
node *n1 = new node(4);
cll1.addAtFront(n1);
node *n2 = new node(5);
cll1.addAtFront(n2);
node *n3 = new node(3);
cll1.addAtFront(n3);
node *n4 = new node(13);
cll1.addAtFront(n4);

cll1.printList();

//condition number 3. the node's next node is first node   

cll1.removeNext(n1);


cll1.printList();
return 0;
}

Output:

13->3->5->4->NULL                                                                                                              

3->5->4->NULL  

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote