Linear Linked lists with classes

Can someone please help me figure out what is wrong with my display function. From what I understand it should work but it doesn't. Please help!!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#include <iostream>
#include <string.h>
using namespace std;


 


class list   //Class for the elements of a journal entry and the functions for it
{
      public:
             list();
             ~list();
             void insert();
             void display_all();
             void read();
      private:
              char * date;  //variable for the date
              char * subject; //variable for the subject
              char * writing; //variable for the body of the entry
};

struct node  //structure to create a node containing the appropriate information
{
       list data;  //variable to hold the members of the entry struct
       node * next;//creates a pointer to the next node
      
};

list::list()
{
      date=NULL;
      subject=NULL;
      writing=NULL;
}

list::~list()
{
      delete[]date;
      delete[]subject;
      delete[]writing;
}
        
void list::display_all()
{
      node * head=NULL;
      node * current=head;
      while(current!=NULL)
      {
              cout<<head->data.date<<'\t'<<head->data.subject<<endl;
              cout<<head->data.writing<<endl;
              current=current->next;
      }
} 

       

void list::insert()  //Function to add a journal entry
{
      char temp[501]; //temporary variable
      node * head;    //head is a pointer to a node
      head=NULL;     //Making sure that head has no value
      node * current=head; //temporary pointer 
      
      if(!head)
      {
               head=new node;       //assigns what head is pointing to
               cout<<"Please enter the date of the entry followed by the enter key"<<endl;
               cin.get(temp,501,'\n');
               head->data.date=new char[strlen(temp)+1];
               strcpy(head->data.date,temp);
               cin.ignore(100,'\n'); 
      
               cout<<"Please enter the subject of the entry followed by the enter key"<<endl;
               cin.get(temp,501,'\n');
               head->data.subject=new char[strlen(temp)+1];
               strcpy(head->data.subject,temp);
               cin.ignore(100,'\n'); 
      
               cout<<"Please type your entry followed by the enter ':' symbol"<<endl;
               cin.get(temp,501,'\n');
               head->data.writing=new char[strlen(temp)+1];
               strcpy(head->data.writing,temp);
               cin.ignore(1000,'\n'); 
               head->next=NULL;   //attaches the list 
      }
      else
      {
               char array[501];
               node * temp=new node;
               cout<<"Please enter the date of the entry followed by the enter key"<<endl;
               cin.get(array,501,'\n');
               temp->data.date=new char[strlen(array)+1];
               strcpy(temp->data.date,array);
               cin.ignore(100,'\n'); 
      
               cout<<"Please enter the subject of the entry followed by the enter key"<<endl;
               cin.get(array,501,'\n');
               temp->data.subject=new char[strlen(array)+1];
               strcpy(temp->data.subject,array);
               cin.ignore(100,'\n'); 
      
               cout<<"Please type your entry followed by the enter ':' symbol"<<endl;
               cin.get(array,501,'\n');
               temp->data.writing=new char[strlen(array)+1];
               strcpy(temp->data.writing,array);
               cin.ignore(1000,'\n'); 
               temp->next=head;
      }                                                                                     
}

void welcome()
{
     cout<<"Welcome to Robin's Electronic Journal"<<endl;
     cout<<"You can use this program to create a journal entry"<<endl;
     cout<<"to find a journal entry or to display all entries"<<endl<<endl;
}
     
int main()
{
     welcome();
     list love;
     love.insert();
     love.display_all();
     cin.get();
     return 0;
}
   


The function starts on line 44
Well, have another look at these two lines:

1
2
node * head=NULL;
node * current=head;


One could say that another "mistake" is that you're using char arrays instead of std::string.
Indeed, if you're using C++, it's usually a better idea to use C++ features than C features when possible.

And, I'll expand on Athar's post. You're effectively setting current to NULL, so the loop will never run.

-Albatross
Last edited on
Okay, two things. One we haven't been taught how to use but a few string functions (copy and compare). Two she isn't giving us the whole picture. I don't understand what I am supposed to be saying in order to make it run correctly. All I am given is that I need to assign a temporary pointer that is pointing to the same address as head. However if I set node * current=head; I get the error that head is an undefined function is there a website or a link that could help me get a better understanding of how to set this function up correctly. I have perused the masterial on this website and have not found anything that is particularly useful to me.

Thanks
http://cplusplus.com/doc/tutorial/pointers/
You mean that didn't explain what was wrong with your program? And head being an undefined sounds wrong, rather it doesn't sound like the error your compiler might return. In fact, your compiler should return no errors, just a warning that one of your currents is unused.

Also, we're not talking about string functions, we mean actual proper strings, not char*s.
http://cplusplus.com/reference/string/string/

-Albatross
Last edited on
Well, usually a linked list stores a pointer to a head node, from which you can reach all other nodes.
Your setup is a bit different: each node contains a list and the list has no nodes, so that doesn't make any sense.
date, subject and writing should probably be in node as well.
There are a lot of problems with this program. You don't seem to understand what a linked list is at all and it makes me crazy that teachers would ask a beginner programmer to do such a thing.

Let's start from the beginning. A linked list is a list of nodes that are linked together with pointers. First you don't even have a properly defined node class. The node class should not contain a list object. A node should simply contain the data for a single node along with a pointer to the next node and the previous node (previous node pointer only needed for doubly linked list). The list class should have a pointer to the first node called head.

Let's say that I want a linked list of car objects. I won't define car but let's assume that a well defined class called car exists.
1
2
3
4
5
6
struct CarNode
{
    Car theCarObject;
    node* next;
    node* previous; // if you want bidirectional list
};



1
2
3
4
5
6
7
8
9
10
11
12
class CarList   //Class for the elements of a journal entry and the functions for it
{
      public:
             list();
             ~list();
             void insert();  // you probably want this to take an arg that indicates where to insert
             void display_all();
             //void read();  I'm not sure what this would do.  I recommend deleting it.
      private:
             CarNode* head;
             CarNode* tail;  // if you need bidirectional support or if you simply want to insert at front or back
};



Let's see how far you can get with that bit of advice and the others that have been given. by the way the code for getting data for each node should not be within the list class itself. The list class itself should be very generic. Ideally I think that it should be a template but that concept is probably way over your head right now.
Am I on the right track now? I started over, I figured it would be easier to start over than try to correct all my errors.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>
using namespace std;

struct node;
class journal;

class journal
{
      public:
             journal();  //class constructor
             ~journal(); //class destructor
             void new_entry();  //Function to add a journal entry
             void display_all(); //Function to display journal entries
      private:
              node * head;
};       
struct node
{
       journal entry;  //class object
       node* next;   
       char * date;  //variable for the date
       char * subject; //variable for the subject
       char * writing; //variable for the body of the entry
};

journal::journal()
{
      head=new node;
}
Why does a node contain a journal object? That is still incorrect. The node objects only point to other nodes but should not have an entire linked list as an object. Have you done a web search to find other examples of linked lists?
http://www.codepedia.com/1/LinkedList
http://en.wikipedia.org/wiki/Linked_list
http://www.inversereality.org/tutorials/c++/linkedlists.html

There are all kinds of articles and examples out there to study. Also, there is a C++ linked list called std::list so I am not sure why you need to make your own. Since it is a template class all you need to do is define the node type and then instantiate the node. By the way, use std::string and not the char*. you have enough problems without having to worry about dynamically allocated memory of strings.
1
2
3
4
5
6
7
8
9
struct JounalNodeType
{
    std::string date;
    std::string subject;
    std::string entry;
    JournalNodeType* next;
};

std::list<JournalNodeType> MyJournal;


Now use the list reference material to figure out how to construct nodes, insert them, and use iterators to access the data.
http://cplusplus.com/reference/stl/list/
I mostly got it figured out, unfourtunately I required to use dynamically allocated memory as that is one of our most recent lessons. Thanks for all your help.
Topic archived. No new replies allowed.