Question

C++ - I have a doubly linked list, but I haven't been able to get the...

C++ - I have a doubly linked list, but I haven't been able to get the "reverse list" option in the code to work(It's option #in the menu in the program).  I received this guidance for testing: 
Test 4 cases by entering (in this order) c,a,z,k,l,m This tests empty list, head of list, end of list and middle of list. Then delete (in this order) a,z,l. This tests beginning, end and middle deletes.  This exhaustively tests for pointer errors.
#include <string>
using namespace std;

class node
{
public:
   string name;
   node *next;
   node *prev;
};

class linkedList
{
public:
   linkedList() :top(NULL) {}
   bool empty() {
       return top == NULL;
   }
   node *getTop() {
       return top;
   }
   void setTop(node *n) {
       top = n;
   }
   void add(string);
   int menu();
   void remove(string);
   ~linkedList();
   friend ostream& operator << (ostream&, const linkedList&);
private:
   node *top;
};

void main() {
   linkedList l;
   cout << l.empty() << endl;
   int option = 0;
   string s;
   bool go = true;
   while (go)
   {
       option = l.menu();
       switch (option)
       {
       case 1: cout << "enter a name: "; cin >> s; l.add(s); break;
       case 2: cout << "enter name to delete: "; cin >> s; l.remove(s); break;
       case 3: cout << l; break;
       case 4: cout << "this is what needs to be finished" << endl;
       case 5: cout << "exiting" << endl; go = false; break;
       }
   }
   system("PAUSE");
}

void linkedList::remove(string s) {
   bool found = false;
   node *curr = getTop(), *prev = NULL;
   while (curr != NULL)
   {
       if (curr->name == s)
       {
           found = true;
           if (prev == NULL)
           {
               node *temp = getTop();
               setTop(curr->next);
                           top->prev = NULL;
               delete(temp);
           }
           else
           {
               prev->next = curr->next;
                           if(curr->next != NULL) {
                                   curr->next->prev = prev;
                           }
               delete(curr);
           }
       }
       if (!found)
       {
           prev = curr;
           curr = curr->next;
       }
       else curr = NULL;
   }
   if (found)cout << "Deleted " << s << endl;
   else cout << s << " Not Found " << endl;
}

void linkedList::add(string s) {
   node *n = new node();
   n->name = s;
   n->next = NULL;
   n->prev = NULL;
   if (empty())
   {
       top = n;
   }
   else if (getTop()->name > s)
   {
       n->next = getTop();
           top->prev = n;
       setTop(n);
   }
   else
   {
       node *curr = getTop(), *prev = curr;
       while (curr != NULL)
       {
           if (curr->name > s)break;
           prev = curr;
           curr = curr->next;
       }
       if (curr != NULL)
       {
           n->next = curr;
                   curr->prev = n;
                   n->prev = prev;
           prev->next = n;
       }
       else if (curr == NULL)
       {
           prev->next = n;
                   n->prev = prev;
       }
   }
}

ostream& operator << (ostream& os, const linkedList& ll) {
   node *n = ll.top;
   if (n == NULL)cout << "List is empty." << endl;
   else
       while (n != NULL)
       {
           os << n->name << endl;
           n = n->next;
       }
   return os;
}
linkedList::~linkedList() {
   cout << "~linkedList called." << endl;
   node *curr = getTop(), *del;
   while (curr != NULL)
   {
       del = curr;
       curr = curr->next;
       delete(del);
   }
}

int linkedList::menu() {
   int choice = 0;
   while (choice < 1 || choice > 5)
   {
       cout << "\nEnter your choice" << endl;
       cout << " 1. Add a name." << endl;
       cout << " 2. Delete a name." << endl;
       cout << " 3. Show list." << endl;
       cout << " 4. Show reverse list. " << endl; // to be implemented by students
       cout << " 5. EXIT " << endl;
       cin >> choice;
   }
   return choice;
}
0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <iostream>

using namespace std;

class node

{

public:

   string name;

   node *next;

   node *prev;

};

class linkedList

{

public:

   linkedList() :top(NULL) {}

   bool empty() {

       return top == NULL;

   }

   node *getTop() {

       return top;

   }

   void setTop(node *n) {

       top = n;

   }

   void add(string);

   int menu();

   void remove(string);

   ~linkedList();

   void reverseList(); // reverse the list

   friend ostream& operator << (ostream&, const linkedList&);

private:

   node *top;

};

void main() {

                  linkedList l;

                  cout << l.empty() << endl;

                  int option = 0;

                  string s;

                  bool go = true;

                  while (go)

                  {

                      option = l.menu();

                      switch (option)

                      {

                      case 1: cout << "enter a name: "; cin >> s; l.add(s); break;

                      case 2: cout << "enter name to delete: "; cin >> s; l.remove(s); break;

                      case 3: cout << l; break;

                      case 4: l.reverseList();break;

                      case 5: cout << "exiting" << endl; go = false; break;

                      }

                  }

     system("PAUSE");

}

void linkedList::remove(string s) {

   bool found = false;

   node *curr = getTop(), *prev = NULL;

   while (curr != NULL)

   {

       if (curr->name == s)

       {

           found = true;

           if (prev == NULL)

           {

               node *temp = getTop();

               setTop(curr->next);

                           top->prev = NULL;

               delete(temp);

           }

           else

           {

               prev->next = curr->next;

                           if(curr->next != NULL) {

                                   curr->next->prev = prev;

                           }

               delete(curr);

           }

       }

       if (!found)

       {

           prev = curr;

           curr = curr->next;

       }

       else curr = NULL;

   }

   if (found)cout << "Deleted " << s << endl;

   else cout << s << " Not Found " << endl;

}

void linkedList::add(string s) {

   node *n = new node();

   n->name = s;

   n->next = NULL;

   n->prev = NULL;

   if (empty())

   {

       top = n;

   }

   else if (getTop()->name > s)

   {

       n->next = getTop();

           top->prev = n;

       setTop(n);

   }

   else

   {

       node *curr = getTop(), *prev = curr;

       while (curr != NULL)

       {

           if (curr->name > s)break;

           prev = curr;

           curr = curr->next;

       }

       if (curr != NULL)

       {

           n->next = curr;

                   curr->prev = n;

                   n->prev = prev;

           prev->next = n;

       }

       else if (curr == NULL)

       {

           prev->next = n;

                   n->prev = prev;

       }

   }

}

ostream& operator << (ostream& os, const linkedList& ll) {

   node *n = ll.top;

   if (n == NULL)cout << "List is empty." << endl;

   else

       while (n != NULL)

       {

           os << n->name << endl;

           n = n->next;

       }

   return os;

}

linkedList::~linkedList() {

   cout << "~linkedList called." << endl;

   node *curr = getTop(), *del;

   while (curr != NULL)

   {

       del = curr;

       curr = curr->next;

       delete(del);

   }

}

int linkedList::menu() {

   int choice = 0;

   while (choice < 1 || choice > 5)

   {

       cout << "\nEnter your choice" << endl;

       cout << " 1. Add a name." << endl;

       cout << " 2. Delete a name." << endl;

       cout << " 3. Show list." << endl;

       cout << " 4. Show reverse list. " << endl; // to be implemented by students

       cout << " 5. EXIT " << endl;

       cin >> choice;

   }

   return choice;

}

void linkedList:: reverseList()

{

               node *curr = getTop();

               if (curr == NULL) // empty list

               {

                              cout << "List is empty." << endl;

               }else{

                              // get the last node of the list

                              while(curr->next != NULL)

                                             curr =curr->next;

                              // loop till you reach end of list in reverse order

                              while(curr != NULL)

                              {

                                             cout<<curr->name<<endl; // print the name

                                             curr =curr->prev; // get the previous node

                              }

               }

}

//end of program

Output:

Add a comment
Know the answer?
Add Answer to:
C++ - I have a doubly linked list, but I haven't been able to get the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Q) Modify the class Linked List below to make it a Doubly Linked List. Name your...

    Q) Modify the class Linked List below to make it a Doubly Linked List. Name your class DoublyLinkedList. Add a method addEnd to add an integer at the end of the list and a method displayInReverse to print the list backwards. void addEnd(int x): create this method to add x to the end of the list. void displayInReverse(): create this method to display the list elements from the last item to the first one. Create a main() function to test...

  • Please rewrite this function using recursive function #include using namespace std; struct Node { char ch;...

    Please rewrite this function using recursive function #include using namespace std; struct Node { char ch; Node* next; }; class LinkedList { Node* head; public: LinkedList(); ~LinkedList(); void add(char ch); bool find(char ch); bool del(char ch); friend std::ostream& operator<<(std::ostream& out, LinkedList& list); }; LinkedList::LinkedList() { head = NULL; } LinkedList::~LinkedList() { Node* cur = head, * tmp; while (cur != NULL) { tmp = cur->next; delete cur; cur = tmp; } } void LinkedList::add(char ch) { Node* cur = head,...

  • Linkedlist implementation in C++ The below code I have written is almost done, I only need...

    Linkedlist implementation in C++ The below code I have written is almost done, I only need help to write the definition for delete_last() functio​n. ​ Language C++ // LinkedList.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include <string> #include <iostream> using namespace std; struct Node { int dataItem;//Our link list stores integers Node *next;//this is a Node pointer that will be areference to next node in the list }; class LinkedList { private: Node *first;...

  • I have a C++ code that lets me enter, display and delete a student record. I...

    I have a C++ code that lets me enter, display and delete a student record. I need to implement a function that prints the average grade score of the students I input. Below is my code and a picture of how my code looks right now. #include<iostream> #include<stdlib.h> using namespace std; //Node Declaration struct node {    string name;    string id;    int score;    node *next;   }; //List class class list {        private:        //head...

  • Doubly Linked List Is there a way to further modify/simplify/improve this program? I was thinking of maybe changing how...

    Doubly Linked List Is there a way to further modify/simplify/improve this program? I was thinking of maybe changing how to get size. I'm not sure. import java.util.Scanner; /* Class Node */ class Node { protected int data; protected Node next, prev; /* Constructor */ public Node() { next = null; prev = null; data = 0; } /* Constructor */ public Node(int d, Node n, Node p) { data = d; next = n; prev = p; } /* Function...

  • Programming in C: I am trying to modify this linked list to be doubly linked list....

    Programming in C: I am trying to modify this linked list to be doubly linked list. I’m also trying to add a print in reverse function. I’m really struggling with how to change the insert function to doubly link the nodes without effecting the alphabetical sorting mechanism. Example of desired output: Enter your choice: 1 to insert an element into the list. 2 to delete an element from the list. 3 to end. ? 1 Enter a character: a The...

  • CSCI 2010 Lab11 Link-Lists Lab 11A Linked-Lists Preparation Create a Visual Studio C++ Project C...

    CSCI 2010 Lab11 Link-Lists Lab 11A Linked-Lists Preparation Create a Visual Studio C++ Project C2010Lab11A Add the following to the project. //LinkedList.cpp #include <cstdlib> #include "LinkedList.h" using namespace std; //--------------------------------------------------- //List Element Members //--------------------------------------------------- ListElement::ListElement(int d, ListElement * n) {    datum=d;    next=n; } int ListElement::getDatum () const {    return datum; } ListElement const* ListElement::getNext () const {    return next; } //--------------------------------------------------- //LinkedList Members //--------------------------------------------------- LinkedList::LinkedList () {    head=NULL; } void LinkedList::insertItem(int item) {    ListElement *currPtr = head;    ListElement *prevPtr =...

  • Doubly Linked List The assignment is to modify the below code in any way (like changing the method of a function). Time...

    Doubly Linked List The assignment is to modify the below code in any way (like changing the method of a function). Time complexity is omitted. Any methods/functions below could be changed into something different. I was thinking of changing the method of getting size of list and maybe change from numbers to letters for nodes. import java.util.Scanner; /* Class Node */ class Node { protected int data; protected Node next, prev; /* Constructor */ public Node() { next = null;...

  • #include <iostream> using namespace std; struct ListNode { float value; ListNode *next; }; ...

    #include <iostream> using namespace std; struct ListNode { float value; ListNode *next; }; ListNode *head; class LinkedList { public: int insertNode(float num); void deleteNode(float num); void destroyList(); void displayList(); LinkedList(void) {head = NULL;} ~LinkedList(void) {destroyList();} }; int LinkedList::insertNode(float num) { struct ListNode *newNode, *nodePtr = head, *prevNodePtr = NULL; newNode = new ListNode; if(newNode == NULL) { cout << "Error allocating memory for new list member!\n"; return 1; } newNode->value = num; newNode->next = NULL; if(head==NULL) { cout << "List...

  • In C++, for the provided template linked list class create a derived class of it which...

    In C++, for the provided template linked list class create a derived class of it which adds the functionality to it to find the high and low value of any given data stored in the list. The derived class must be a template. LinkedList.h #pragma once #include <iostream> using namespace std; template <class T> class ListNode {    public:        T data;        ListNode<T>* next;        ListNode(T data)        {            this->data = data;...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT