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

Doubly linked list Create a doubly linked list from a file (input.txt). The numb

ID: 3757256 • Letter: D

Question

Doubly linked list

Create a doubly linked list from a file (input.txt). The number of items is unknown. (Delete from head) Prompt the user for a node number. Starting from the head, moving forward, go to the node and delete it.(Delete from tail) Prompt the user for another node number. Starting from the tail, moving backward, go to the node and delete it. Write the contents of the modified list into the file (output.txt).

NOTE: The node to be deleted can be the head, the tail or something in the middle. If there is only one node in the list and it is being deleted, then both the head and tail are affected by this.

Example:

Input file

59 22 23 58

Which item do you want to delete from the beginning?

3

Which item do you want to delete end?

2 //the first delete would remove 23 from the list.

//The second delete would remove 22 from the list.

Output file

59 58

Explanation / Answer

#include #include using namespace std; class List { struct Node { int data; Node *left, *right; //Constructor Node(int n, Node* l, Node* r) : data(n), left(l), right(r){} } *head, *tail; public: List() : head(nullptr), tail(nullptr){} //Function to insert elements into the list void insert(const int& num) { if(head == nullptr) //When list is empty { head = new Node(num, nullptr, nullptr);//Create new Node and pass parameters into the constructor of Node tail = head; } else { Node *newNode = new Node(num, tail, nullptr);//Create new Node and pass parameters into the constructor of Node tail->right = newNode; tail = newNode; } } //Function to write current list into output.txt void writeList() { ofstream f("output.txt", ios::out); Node *ptr = head; while(ptr != nullptr) { fnum; insert(num); } f.close(); } //Function to delete a node from the list void deleteNode(Node *_node) { if(_node == head && _node == tail) //When _node is the only node in the list { delete _node; head = tail = nullptr; } else if(_node == head) //When _node is the head node { head->right->left = nullptr; head = head->right; delete _node; } else if(_node == tail) //When _node is the tail node { tail->left->right = nullptr; tail = tail->left; delete _node; } else //When _node is a node in somewhere middle of the list { _node->left->right = _node->right; _node->right->left = _node->left; delete _node; } } //Function to delete a node from the list from the beginning void deleteFromHead() { int nodeNum, count = 1; coutnodeNum; Node *ptr = head; while(count != nodeNum && ptr != nullptr) { count++; ptr = ptr->right; } deleteNode(ptr); } //Function to delete a node from the list from the end void deleteFromTail() { int nodeNum, count = 1; coutnodeNum; Node *ptr = tail; while(count != nodeNum && ptr != nullptr) { count++; ptr = ptr->left; } deleteNode(ptr); } }; int main() { List l; l.createList(); l.deleteFromHead(); l.deleteFromTail(); l.writeList(); return 0; }
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