Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9037483
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T09:15:25+00:00 2026-06-16T09:15:25+00:00

So I have most of my code down the problem is when I try

  • 0

So I have most of my code down the problem is when I try and add a new node to my list as seen in my main it doesn’t seem to work. Ill add it but then when I try to print it nothing comes out.

What I need to do for the lab is just there if you need it for bizarre some reason but it shouldnt be necessary. [Note: A lot of my gui code doesnt work cause i was just copying and pasting from other parts of my code].

Instructions:
Write an object oriented C++ program to create a doubly linked. Each node should be
able to store a person’s first and last names, along with their respective age. Initialize the
list with 5 nodes. The program should include a member function that allows the user to
print out all the nodes whose age is less than a value input by the user (in either
direction). It should also allow the user to search for a particular person’s name. It should
be able to output the lists average age. Finally it should allow for a name to be removed.
The program should allow the user to take choices from a menu.

#include <iostream>
#include <string>
using namespace std;

 typedef string Elem;               // list element type
  class DNode {                 // doubly linked list node
  private:
    Elem elem;                  // node element value
    DNode* prev;                // previous node in list
    DNode* next;                // next node in list
    friend class DLinkedList;           // allow DLinkedList access
  };

class DLinkedList {             // doubly linked list
  public:
    DLinkedList();              // constructor
    ~DLinkedList();             // destructor
    bool empty() const;             // is list empty?
    const Elem& front() const;          // get front element
    const Elem& back() const;           // get back element
    void addFront(const Elem& name);        // add to front of list
    void addBack(const Elem& name);     // add to back of list
    void removeFront();             // remove from front
    void removeBack();              // remove from back
    void displayViaAge();
    void displayViaName();
  private:                  // local type definitions
    DNode* header;              // list sentinels
    DNode* trailer;
  protected:                    // local utilities
    void add(DNode* v, const Elem& name);       // insert new node before v
    void remove(DNode* v);          // remove node v
  };

  DLinkedList::DLinkedList() {          // constructor
    header = new DNode;             // create sentinels
    trailer = new DNode;
    header->next = trailer;         // have them point to each other
    trailer->prev = header;
  }

    DLinkedList::~DLinkedList() {           // destructor
    while (!empty()) removeFront();     // remove all but sentinels
    delete header;              // remove the sentinels
    delete trailer;
  }
                                // insert new node before v
  void DLinkedList::add(DNode* v, const Elem& name) {
    DNode* u = new DNode;  u->elem = name;      // create a new node for e
    u->next = v;                // link u in between v
    u->prev = v->prev;              // ...and v->prev
    v->prev->next = v->prev = u;
  }

  void DLinkedList::addFront(const Elem& name)  // add to front of list
    { add(header->next, name); }

  void DLinkedList::addBack(const Elem& name)   // add to back of list
    { add(trailer, name); }

   void DLinkedList::remove(DNode* v) {     // remove node v
    DNode* u = v->prev;             // predecessor
    DNode* w = v->next;             // successor
    u->next = w;                // unlink v from list
    w->prev = u;
    delete v;
  }

  void DLinkedList::removeFront()       // remove from font
    { remove(header->next); }

  void DLinkedList::removeBack()        // remove from back
    { remove(trailer->prev); }


     bool DLinkedList::empty() const        // is list empty?
    { return (header->next == trailer); }

  const Elem& DLinkedList::front() const    // get front element
    { return header->next->elem; }

  const Elem& DLinkedList::back() const     // get back element
    { return trailer->prev->elem; }

  void DLinkedList::displayViaAge() {                     //Displays person via age

      DNode*temp = header;

       while(temp!=trailer)
       {

           //if(howold = age)
              cout << temp->elem <<endl;
              temp = temp -> next;
       }

       cout << temp->elem<<endl;
  }

int main(){
    char input = 'z';
    string entry;
    int age;
    DLinkedList person;

    person.addFront("Takkun Bradly");
    person.add("Devindra Ardnived");
    person.add("SeboY Wang");
    person.add("DoubleX Slash");
    person.add("Uncle Jelly");

    cout << "What would you like to do?" << endl;
    cout << "Enter 'A' to: Add a new person" << endl;
    cout << "Enter 'B' to: Remove a person" << endl;
    cout << "Enter 'C' to: Search for people via age" << endl;
    cout << "Enter 'D' to: Search for people via name" << endl;
    cout << "Enter 'E' to: Average all the total ages" << endl;
    cout << "Enter 'F' to: Quit" << endl;

while(input != 'f') {

    cin >> input;
    cout << endl;

        while ((input != 'a')&&(input != 'b')&&(input != 'c')&&(input != 'd')&&(input != 'e')&&(input != 'f')) {

            cout << "Please enter a valid selection" << endl;           
            cin >> input;
        }

        if ((input == 'a')){
            cout << "Please enter their name: ";
            cin.ignore();
            getline(cin, entry);

            cout << "Please enter their age: ";
            cin >> age;
            person.addFront(entry);

        }

        if ((input == 'b')){
            cout << "Who would you like to remove: ";
            cin.ignore();
            getline(cin, entry);
            person.removeFront();
        }

        if ((input == 'c')){
            cout << "What is the age of the person you are looking for?: ";
            person.displayViaAge();
        }

        if ((input == 'd')){
            cout << "What is the name of the person you are looking for?: ";
            cin.ignore();
            getline(cin, entry);
            person.addFront(entry);
        }

        if ((input == 'e')){
            return 0;
        }

        cout << endl;
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T09:15:26+00:00Added an answer on June 16, 2026 at 9:15 am

    I think your bug is in this line in your add method:

     v->prev->next = v->prev = u;
    

    This assignments in this line will execute right to left. So v->prev gets set to u, then v->prev->next is set to v->prev which is now pointing at u. So now you have u->next pointing at u, which is not what you want.

    I think you want something like this instead:

    // insert new node before node v
    void DLinkedList::add(DNode* v, const Elem& name) {
        DNode* u = new DNode;   u->elem = name;      // create a new node and set name
        u->next = v;                 // make v the successor of u
        u->prev = v->prev;           // set u's predecessor to v's current predecessor
        u->prev->next = u;           // make u the successor of v's predecessor
        v->prev = u;                 // finally make u the predecessor of v
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We have a setup where most code, before being promoted to full production, is
I have an application with most of the code written in javascript. I am
Most of the code I've written in .NET to make REST calls have been
I've worked out most of the code and have several game classes. The one
I have an OpenCV Android app. Most of its code is in Java but
I have a following code in a most inner loop of my program struct
I have a test of C++ code that in most runs passes, but in
below i have a code that runs in most of my simple programs ..
I currently have code in objective C that can pull out an integer's most
I have a small architecture doubt about organizing code in separate functional units (most

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.