hello, im having troblue getting my linked list to show everything on the list. it only shows the first node, and nothing afterwards.

#include <iostream>
#include <string>

class list
{
   private:
      struct node
      {
         std::string name;
         std::string phone;
         node *next;
      }*head, *tail;

   public:
      list() : head(0), tail(0) {}
      ~list();
      void print() const;
      void push_front(std::string, std::string);
      bool isempty() const;

};

int main()
{
   using namespace std;

   do
   {
      cout << "Enter name: ";
      string n;
      cin >> n;  

      cout << "Enter phone number: ";
      string p;
      cin >> p;
      cout << endl;
      list ll;

      ll.push_front(n, p); 
      ll.print();

      cout << "continue? (y/n)?" << endl;
      cout << ">> ";
      char yesno;
      cin >> yesno;

      if(yesno == 'n' || yesno == 'N')
         break;       

   }while(1);


   return 0;
}

void list::print() const 
{  
   if(isempty())
   {
      std::cout << "node is empty..." << std::endl;
      return;
   }

   node *temp = head;

   while(temp != 0)
   {
      std::cout << "name : "<< temp->name << std::endl;
      std::cout << "phone number : " << temp->phone << std::endl;
      std::cout << std::endl;
      temp = temp->next; 
   }

   std::cout << "\n\n";
}

void list::push_front(std::string n, std::string p)
{
   node *temp = new node;
   temp->name = n;
   temp->phone = p;
   temp->next = 0;

   if(isempty())
      head = tail = temp;
   else
   {
      temp->next = head;
      head = temp;
   }

}

bool list::isempty() const
{
   return head == 0;
}

list::~list()
{
   node *temp = head;
   node *tmp;

   while(temp != 0)
   {
      tmp = temp;
      temp = temp->next;

      delete tmp;
   }
}

You create a new list on every iteration of the loop. Move the definition of ll outside of the loop and it'll work better.

omg... i cant believe i missed that. thank you so much ;)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.