Question: I am trying to insert a node after a specific node in a linked list. The node contains an event (string) and a priority (int).
I am trying to insert a node after a specific node in a linked list. The node contains an event (string) and a priority (int). Here is what I have so far for the function. Feel free to change the entire function it could all be incorrect.
// Insert after void List::after(string event, int priority, string loc) { Node *tmp = new Node(event, priority); // if (loc == "!HEAD!") { // tmp->prev = head; // if (head == nullptr) { // head = tmp; // } else { // head->next = tmp; // } // head = tmp; // } // Insert after tail if (loc == "!TAIL!") { tmp->prev = tail; if (head == nullptr) { head = tmp; } else { tail->next = tmp; } tail = tmp; } // Insert after event // if location == a preexisting event if (loc == event) { // set tmp3 to the location we search for string tmp2 = loc; // See if list is empty if (head == nullptr) { tmp->next = nullptr; head = tmp; } else { // search to find location while (tmp != nullptr) { if (tmp->event == event) { } } } } } }
Here is my list.h:
#include
#ifndef LIST_H #define LIST_H
class Node { public: string event; int priority; Node *next; Node *prev;
Node() : event(0), priority(0), next(nullptr), prev(nullptr) {} Node(string event, int priority) : event(event), priority(priority), next(nullptr), prev(nullptr) {} Node(string event, int priority, Node *next, Node *prev) : event(event), priority(priority), next(next), prev(prev) {} };
class List { private: Node *head; Node *tail;
public: List() : head(nullptr), tail(nullptr) {} ~List();
void after(string event, int priorirty, string loc); void before(string event, int priority, string loc); bool search(string event); // void find // bool remove(int data);
void display(ostream &os); void display_backward(ostream &os); friend ostream &operator<<(ostream &os, List &right); };
#endif
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
