Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

need help unable to get to compile code list.h #include #include Node.h using namespace std; #ifndef HW3_LIST_H #define HW3_LIST_H class List { private: Node *headPtr;

need help unable to get to compile code

list.h

#include #include "Node.h" using namespace std; #ifndef HW3_LIST_H #define HW3_LIST_H class List { private: Node *headPtr; Node *tailPtr; public: List() { headPtr = new Node(); tailPtr = new Node(); headPtr->setNextPtr(NULL); tailPtr->setPrevPtr(NULL); } //accessor functions //complete the definitions of accessor functions. Node *getHeadPtr() const; Node *getTailPtr() const; // insert function // takes a integer number as an input // insert the number at the end of the list void insert(int value); // insertAtIndex function // takes a integer number and an index as inputs // insert the number at the given index void insertAtIndex(int data, int index); // ideleteAtIndex function // takes an index as input // delete the number from the given index void deleteAtIndex(int index); // readItem function // takes an index as input // return the number from the given index void readItem(int index); // reverseList function // reverse the list. Don not swap the values, please swap the nodes. void reverseList(); //printforward function //print the list in forward direction void printForward(); //printBackward //print the list in backward (reverse) direction void printBackward(); }; #endif //HW3_LIST_H

list.cpp

#include "List.h" #include using namespace std; void List::insert(int value) { Node *newNode = new Node(value, tailPtr->getPrevPtr(),tailPtr); tailPtr->getPrevPtr()->setNextPtr(newNode); tailPtr->setPrevPtr(newNode); } void List::insertAtIndex(int data, int index) { int currentIndex = 0; Node *current = headPtr->getNextPtr(); while(current != tailPtr && currentIndex , index){ current = current->getNextPtr(); currentIndex++; } if(currentIndex == index){ Node* NewNode = new Node(date, current->getPrevPtr(), current); current->getPrevPtr()->setNextPtr(newNode); current->setPrevPtr(newNode); } } void List::deleteAtIndex(int index) { int currentIndex = 0; Node* current = headPtr->getNextPtr(); while(current != tailPtr && currentIndex < index){ current = current->getNextPtr(); currentIndex++; } if(current != tailPtr && currentIndex == index){ current->getPrevPtr()->setNextPtr(current->getNextPtr()); current->getNextPtr()->setPrevPtr(current->getPrevPtr()); delete current; } } void List::readItem(int index) { int currentIndex = 0; Node* current = headPtr->getNextPtr(); while (current != tailPtr && currentIndex < index) { current = current->getNextPtr(); currentIndex++; } if (current != tailPtr && currentIndex == index) { return current->getData(); } return -1; } void List::reverseList() { Node* current = headPtr; Node* tempPtr; while (current != nullptr) { tempPtr = current->getPrevPtr(); current->setPrevPtr(current->getNextPtr()); current-> setNextPtr(tempPtr); current = current-> getPrevPtr(); } void List::printForward() { Node *curr = headPtr->getNextPtr(); while(curr != NULL){ cout<getData()<<" "; curr = curr->getNextPtr(); } cout << endl; } void List::printBackward() { Node *curr = tailPtr->getPrevPtr(); while(curr != NULL){ cout << curr->getData() << " "; curr->getPrevPtr } }

Node.h

#ifndef HW3_NODE_H #define HW3_NODE_H class Node { private: int data; Node *next; Node *prev; public: Node() : data(0), next(NULL), prev(NULL) {} Node(int val) : data(val), next(NULL), prev(NULL) {} Node(int val, Node *n, Node *p) : data(val), next(n), prev(p) {} //mutator functions void setData(int val) { data = val; } void setNextPtr(Node *n) { next = n; } void setPrevPtr(Node *p) { prev = p; } //accessor functions int getData() const { return data; } Node *getNextPtr() const { return next; } Node *getPrevPtr() const { return prev; } }; #endif //HW3_NODE_H

main.cpp

#include  using namespace std; #include "Node.h" #include "List.h" int main() { List head; int num, item, index; int menu; //Print a menu //Each of the menu option will be the operation of //Doubly Linked List Abstract DataType while (1) { cout << "Please enter a choice (1-8)" << endl; cout << "1:Insert at the end of the list" << endl; cout << "2:Insert at a given index" << endl; cout << "3:delete from a given index" << endl; cout << "4:read an item" << endl; cout << "5:reverse the list" << endl; cout << "6:print the list forward" << endl; cout << "7:print the list backward" << endl; cout << "8:exit" << endl; cin >> menu; if (menu == 8) break; switch (menu) { case 1: //user enters number to insert the item at the end of the list //enter -1 to end the input. cout << "enter item to insert, enter -1 to end" << endl; while (1) { cin >> num; if (num == -1) break; head.insert(num); } break; case 2: cin >> item; cout << "Insert the index: "; cin >> index; head.insertAtIndex(item, index); break; case 3: cout << "Insert the index to delete: "; cin >> index; head.deleteAtIndex(index); break; case 4: cout << "Insert the index to read: "; cin >> index; head.readItem(index); break; case 5: cout << "Reversing the list" << endl; head.reverseList(); break; case 6: head.printForward(); break; case 7: head.printBackward(); break; default: cout << "Enter a valid choice" << endl; } } return 0; }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Professional SQL Server 2012 Internals And Troubleshooting

Authors: Christian Bolton, Justin Langford

1st Edition

1118177657, 9781118177655

More Books

Students also viewed these Databases questions