C++ Creating doubly linked list program

This is a discussion on C++ Creating doubly linked list program within the C++ Programming forums, part of the General Programming Boards category; Haven't been able to figure out how to make a doubly linked list from this code using m_pPrev (for previous), ...

  1. #1
    Registered User
    Join Date
    Jul 2003
    Posts
    13

    C++ Creating doubly linked list program

    Haven't been able to figure out how to make a doubly linked list from this code using m_pPrev (for previous), any suggestions would be appreciated. I have a sinus infection and can't think for the past two days!

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <string>
    #include <iostream>
    
    using namespace std;
    class Node
    {
    public:
       Node ( void ) { m_pNext=0; m_pContent=0;};	
       ~Node ( void ) { };					
       void SetNext ( Node *a_pn) {m_pNext = a_pn; };	
       void SetContent (void *a_pv) {m_pContent = a_pv;};
    
       const Node* GetNext (void) {return m_pNext;};
       const void* GetContent (void) {return m_pContent; };
    
    private:
       Node *m_pNext;		
       void *m_pContent;
    };
    
    class List
    {
    public:
       List (void) {m_pHead = 0; m_pLast = 0; m_pCache = 0;};	
       ~List (void) { };										
       void Append (void *a_pv)		
          {
          Node *pn = new Node;
          pn->SetContent (a_pv);
    
          if (0==m_pHead && 0 == m_pLast )	
             {
             m_pHead = m_pLast = pn;
             }
          else if (m_pHead == m_pLast)		
             {
             m_pHead->SetNext (pn);
             m_pLast = pn;
             }
          else						  
             {
              m_pLast->SetNext (pn);
              m_pLast = pn;
              }
          };
    
          const void* GetHead (void)
             {
             const void *pv=0;
    	
            m_pCache = m_pHead;
    
          if (m_pCache !=0)
          {
          pv=m_pCache->GetContent();
          }
    
       return pv;
       };
    
    const void* GetNext (void)
       {
       const void *pv=0;
    
       if (m_pCache !=0)
          {
           m_pCache = (Node*)m_pCache->GetNext();
           }
    
       if (m_pCache !=0)
          {
          pv=m_pCache->GetContent();
          }
    
       return (pv);
         };
    
    private:		
       Node *m_pHead;
       Node *m_pLast;
    
       Node *m_pCache;		
       };
    class Person			
    {
    public:
       Person () { };		
       ~Person () { };		
    
       string m_sFirstName;	
       string m_sLastName;
       };
    
    void main (int argc, char *argv[] )
       {
       if (argc <2)
          {
          cout << "no input data" << endl;
          return;
          }
    
    List list;		
    
       for (int i = 1; i < argc; i++)		
            {
            Person *p=new Person;		
    	 
           p->m_sFirstName = argv [i];	
    
           list.Append (p);			
           }
    
          cout << "you entered" << (i-1) << "names - dumping list" <<  endl;
          Person *pi = (Person*)list.GetHead();	
          int indx=0;
    
         while (pi !=0)	
         {
          cout << "Person("<<indx++ <<") FirstName("<<pi- >m_sFirstName <<")" << endl;
    
          pi = (Person*)list.GetNext();	
         }
    }
    Last edited by tinkerbelle; 10-08-2003 at 06:00 PM.
    tinkerbelle

  2. #2
    Registered User
    Join Date
    Jul 2003
    Posts
    13

    Unhappy Need HELP desperately with creating doubly linked list

    HELP:


    Haven't been able to figure out how to make a doubly linked list from this code using m_pPrev (for previous), any suggestions would be appreciated. I have a sinus infection and can't think for the past two days!


    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <string>
    #include <iostream>
    
    using namespace std;
    class Node
    {
    public:
       Node ( void ) { m_pNext=0; m_pContent=0;};	
       ~Node ( void ) { };					
       void SetNext ( Node *a_pn) {m_pNext = a_pn; };	
       void SetContent (void *a_pv) {m_pContent = a_pv;};
    
       const Node* GetNext (void) {return m_pNext;};
       const void* GetContent (void) {return m_pContent; };
    
    private:
       Node *m_pNext;		
       void *m_pContent;
    };
    
    class List
    {
    public:
       List (void) {m_pHead = 0; m_pLast = 0; m_pCache = 0;};	
       ~List (void) { };										
       void Append (void *a_pv)		
          {
          Node *pn = new Node;
          pn->SetContent (a_pv);
    
          if (0==m_pHead && 0 == m_pLast )	
             {
             m_pHead = m_pLast = pn;
             }
          else if (m_pHead == m_pLast)		
             {
             m_pHead->SetNext (pn);
             m_pLast = pn;
             }
          else						  
             {
              m_pLast->SetNext (pn);
              m_pLast = pn;
              }
          };
    
          const void* GetHead (void)
             {
             const void *pv=0;
    	
            m_pCache = m_pHead;
    
          if (m_pCache !=0)
          {
          pv=m_pCache->GetContent();
          }
    
       return pv;
       };
    
    const void* GetNext (void)
       {
       const void *pv=0;
    
       if (m_pCache !=0)
          {
           m_pCache = (Node*)m_pCache->GetNext();
           }
    
       if (m_pCache !=0)
          {
          pv=m_pCache->GetContent();
          }
    
       return (pv);
         };
    
    private:		
       Node *m_pHead;
       Node *m_pLast;
    
       Node *m_pCache;		
       };
    class Person			
    {
    public:
       Person () { };		
       ~Person () { };		
    
       string m_sFirstName;	
       string m_sLastName;
       };
    
    void main (int argc, char *argv[] )
       {
       if (argc <2)
          {
          cout << "no input data" << endl;
          return;
          }
    
    List list;		
    
       for (int i = 1; i < argc; i++)		
            {
            Person *p=new Person;		
    	 
           p->m_sFirstName = argv [i];	
    
           list.Append (p);			
           }
    
          cout << "you entered" << (i-1) << "names - dumping list" <<  endl;
          Person *pi = (Person*)list.GetHead();	
          int indx=0;
    
         while (pi !=0)	
         {
          cout << "Person("<<indx++ <<") FirstName("<<pi- >m_sFirstName <<")" << endl;
    
          pi = (Person*)list.GetNext();	
         }
    }
    tinkerbelle

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sorting linked list please help with CODE
    By scarlet00014 in forum C Programming
    Replies: 3
    Last Post: 09-28-2008, 12:24 AM
  2. Creating a global linked list?
    By TwistedJester in forum C Programming
    Replies: 1
    Last Post: 11-12-2007, 03:23 AM
  3. Replies: 6
    Last Post: 03-02-2005, 02:45 AM
  4. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 11:21 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21