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

C++ Help. Unable to delete items. Saying cannond delete expression of type share

ID: 3903501 • Letter: C

Question

C++ Help. Unable to delete items. Saying cannond delete expression of type shared_ptr<Node<int>> Trying to delete temp,cur, and cur.

template

void DoublyLinkedList::deleteAtIndex(const unsigned int index)

{

if (front == nullptr)

{

cout << "the list is empty" << endl;

return;

}

if (index > count)

{

cout << "index is out of bounds" << endl;

return;

}

if (index == 0)

{

shared_ptr> temp(front);

front = front->forward;

delete temp;

if (count == 0)

{

front = nullptr;

back = nullptr;

count--;

return;

}

count--;

return;

}

if (index == count-1)

{

shared_ptr> temp(nullptr);

shared_ptr> cur(front);

int tracker = 0;

  

while (tracker < index)

{

temp = cur;

cur = cur->forward;

tracker++;

}

  

delete cur;

back = temp;

back->backward = nullptr;

count--;

return;

  

}

shared_ptr> temp(nullptr);

shared_ptr> cur(front);

int tracker = 0;

while (tracker < index)

{

temp = cur;

cur = cur->forward;

tracker++;

}

temp->forward = cur->forward;

   delete cur;

count--;

}

Explanation / Answer

I have written a code that can be clearly understand by you!

#include<iostream>

using namespace std;

struct Node {

int a;

struct Node* next;

struct Node* prev;

};

void deleteNode(struct Node** head, struct Node* del)

{

if (*head == NULL || del == NULL)

return;

if (*head == del)

*head = del->next;

if (del->next != NULL)

del->next->prev = del->prev;

if (del->prev != NULL)

del->prev->next = del->next;

free(del);

}

void deletion(struct Node** head, int n)

{

if (*head == NULL || n <= 0)

return;

struct Node* current = *head;

int i;

for (int i = 1; current != NULL && i < n; i++)

current = current->next;

if (current == NULL)

return;

deleteNode(head, current);

}

void insert(struct Node** head, int data)

{

struct Node* newnode =

(struct Node*)malloc(sizeof(struct Node));

newnode->a = data;

newnode->prev = NULL;

newnode->next = (*head);

if ((*head) != NULL)

(*head)->prev = newnode;

(*head) = newnode;

}

void display(struct Node* head)

{

while (head != NULL) {

cout << head->a << " ";

head = head->next;

}

}

int main()

{

struct Node* head = NULL;

insert(&head, 5);

insert(&head, 2);

insert(&head, 4);

insert(&head, 8);

insert(&head, 10);

cout << "Doubly linked list before deletion";

display(head);

int pos;

cout<<"enter the postion"<<endl;

cin>>pos;

cout<<"the entered postion is "<<pos<<endl;

deletion(&head,pos);

cout << " Doubly linked list after deletion";

display(head);

return 0;

}

input: 4