Adding linear linked list capability to an existing program.

For my final program for my CS class, I am supposed to convert this program I already wrote which is basically an electronic journal to store the journal entries in a linear linked list and output them from that list.

I am having problems because we learned some linear linked list stuff, but we didnt really do anything related to this and thus far, I have not been able to do much with it. Here is my code, any help is MUCH appreciated. I am guessing I need to create a list class....but i dont really know how to go about it.

Header file
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
//jounral.h
#include<iostream>
using namespace std;
#include<cstring>

const int DATE = 11; //const int for the date
const int SUB = 51; //const int for the subject
const int MAX = 2000; //const int for the TEMP ENTRY

struct entry
{
       char date[DATE]; //array for holding the date
       char subject[SUB]; //array for holding the subject
       char *entry; //dynamic array for holding the journal entry
};
       
class journal
{
      public:
             journal(int size);  //constructor with argument for size
             ~journal();  //destructor
             void menu();             //menu function
             void new_entry();  //create a new Journal Entry
             void display_all(); //display all of the entries
             void search(); //find an entry based on subject
      private:
              entry *my_journal;//dynamic array for the number of entries
              int my_journal_size; //int for holding the size of the jounral
              int num_entries; //keeps track of the number of entries
};
              
void welcome(); //prototype for the welcome function
void termination(); //termination function
journal object(1); //initialize the class 




Here is the main code:
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
#include"journal.h"

int main()
{
    welcome();//calls the welcome function
    object.menu();//calls the menu function
    termination();//calls the termination function
}

void welcome()
{
     cout<<"Welcome to the Electronic Journal Program!" <<endl <<endl;
     cout<<"This program will allow you to record your" <<endl;
     cout<<"thoughts electronically instead of wasting"<<endl;
     cout<<"all of that paper!" <<endl <<endl;
}
journal::journal(int size = 100)
{
     my_journal = new entry[100];
     my_journal_size = 100;
     num_entries = 0;
}
journal::~journal()
{
     delete [] my_journal;
     my_journal = NULL;
}
void journal::menu() //menu function
{
     char choice; //variable to hold the choice
     
     do //loop that keeps prompting the user until a valid choice is inputted
     {
     cout<<"What would you like to do? " <<endl;
     cout<<"Choose Option 1 to add a new Journal Entry " <<endl;
     cout<<"Choose Option 2 to display all of your Journal Entries " <<endl;
     cout<<"Choose Option 3 to find an entry by the subject " <<endl <<endl;
     cout<<"Please enter a choice (1,2, or 3): " <<endl;
     
     cin>>choice; cin.ignore(100, '\n'); //reads in the choice and eats the rest
     if (choice == '1')
        object.new_entry(); //if 1, Create a new journal entry
     else if (choice == '2')
        object.display_all();// if 2, Display everything
     else if (choice == '3')
        object.search(); //if 3, search by subject
     else
         cout<<"Error, Please try again" <<endl <<endl; //error message, restarting the loop
     }while (choice != '1' && choice != '2' && choice != '3');
     
}
void journal::new_entry() //function for inputting a new journal entry
{
     
     char temp[MAX]; //variable to read in the journal entry temporarily
     
     //loop to read in until we reach the maximum size of the Journal
     while (num_entries > my_journal_size);
     {
     cout<<"Please enter the date for this entry (MM/DD/YYYY): ";
     cin.get(my_journal[num_entries].date, DATE, '\n'); //read in the date
     cin.ignore(100, '\n'); //eat the carriage return
     
     cout<<"Please enter the subject for this entry ";
     cin.get(my_journal[num_entries].subject, SUB, '\n'); //read in subject
     cin.ignore(100, '\n'); //eat the carriage return
     
     cout<<"Please enter your Journal entry ";
     cin.get(temp, MAX, '\n'); //read in the entry
     cin.ignore (100, '\n'); //eat the carriage return
     
     my_journal[num_entries].entry = new char[strlen(temp)+1]; 
     //creates an array of 1+the string length
     
     strcpy(my_journal[num_entries].entry, temp); //copy the temp into entry
     ++num_entries; //add 1 to the counter
     cout<<endl <<endl <<endl;
     menu();//calls the menu function to get back 
     }
}
void journal::display_all() //display everything entered
{ 
     for (int i = 0; i < num_entries; ++i) //loop that displays everything entered
     {   //output
         cout<< "Date: " << my_journal[i].date  <<endl;
         cout<< "Subject: " << my_journal[i].subject  <<endl <<endl;
         cout<< "Journal Entry: " <<endl << my_journal[i].entry <<endl <<endl;
     }
     menu();
}
void journal::search()//function to search by subject
{
     char query[100]; //Variable to hold the search query
     
     cout<< "What subject would you like to search for?: "; //Prompt user 
     cout<< endl <<endl;
     cin.get(query, 100, '\n'); cin.ignore (100, '\n'); //Read in the query
     
     for (int i = 0; i < num_entries; ++i)
     {
         if(strcmp(query, my_journal[i].subject) == 0)//Compare..MATCH!
         {    //output            
              cout<< "Date: " << my_journal[i].date  <<endl;
              cout<< "Subject: " << my_journal[i].subject  <<endl <<endl;
              cout<< "Journal Entry: " <<endl << my_journal[i].entry <<endl;
              cout<< endl;
         }
     }
     menu();
     
}
void termination()
{
     cout<< "Thank you for using the Electronic Journal!" <<endl;
     cout<< "Press ENTER to exit! ";
}
Last edited on
A linked list is where you store a pointer to the next entry in each record:

1
2
3
4
5
6
7
8
struct entry
{
       entry* next;  // link to next entry in the journal

       char date[DATE];
       char subject[SUB];
       char *entry; // same name?
};


So instead of storing your entry objects in an array like this:

 
entry* my_journal = new entry[100]; 


They want you to store them as a list of linked records.

 
entry* my_journal = 0; // initialise my_journal list  


You then need to write routines to add and delete items from that list

1
2
3
4
5
void journal::add_entry(entry* e)
{
       e->next = my_journal; // point this entry's next element to the current list
        my_journal = e; // make this entry the first in the list.
}


You need to do some reading on linked lists:

This tutorial seems quite good:

http://cslibrary.stanford.edu/103/LinkedListBasics.pdf

Last edited on
OK, we actually did learn about LLL, we just never saw a complete program and were just taught the basics and expected to cram them into the existing program posted above. The tutorial still helped though. I am getting all sorts of errors and Im also not sure if I have the List structure set up correctly in the add_entry function (the loop obviously is not going to work, its just hanging around from the last program) Where should I go from here?

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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
//Program 5

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

const int DATE = 11; //const int for the date
const int SUB = 51; //const int for the subject
const int MAX = 2000; //const int for the TEMP ENTRY

struct journal_entry  //structure for the journal entry
{
       char date[DATE]; //array for holding the date
       char subject[SUB]; //array for holding the subject
       char *entry; //dynamic array for holding the journal entry
};

struct node  //structure for the node (data and pointer)
{
       journal_entry *data;  //instance of the journal struct
       node *next; //pointer to the next node
};

class list    //list class to manage the LLL
{
  public:
         list();  //constructor
         ~list(); //destrutor
        void add_entry(); 
        void display_all(node* head);
        void search();
        void menu();  //menu function prototype
  private:
        node* head;
        int my_journal_size; //int for holding the size of the jounral
        int num_entries; //keeps track of the number of entries
};      
    
void welcome();  //welcome function prototype

list object;  //initalize the class

int main()
{
    welcome();
    cin.get();
}
void welcome()
{
     cout<<"Welcome to the Electronic Journal Program!" <<endl <<endl;
     cout<<"This program will allow you to record your" <<endl;
     cout<<"thoughts electronically instead of wasting"<<endl;
     cout<<"all of that paper!" <<endl <<endl;
}
void list::menu() //menu function
{
     char choice; //variable to hold the choice
     head = new node;
     
     do //loop that keeps prompting the user until a valid choice is inputted
     {
     cout<<"What would you like to do? " <<endl;
     cout<<"Choose Option 1 to add a new Journal Entry " <<endl;
     cout<<"Choose Option 2 to display all of your Journal Entries " <<endl;
     cout<<"Choose Option 3 to find an entry by the subject " <<endl <<endl;
     cout<<"Please enter a choice (1,2, or 3): " <<endl;
     
     cin>>choice; cin.ignore(100, '\n'); //reads in the choice and eats the rest
     if (choice == '1')
        object.add_entry(); //if 1, Create a new journal entry
     else if (choice == '2')
        object.display_all(head->data);// if 2, Display everything
     else if (choice == '3')
        object.search(); //if 3, search by subject
     else
         cout<<"Error, Please try again" <<endl <<endl; //error message, restarting the loop
     }while (choice != '1' && choice != '2' && choice != '3');
}
list::list()
{
     head = new node;
     head = NULL;
     my_journal_size = 0;
     num_entries = 0;
}

list::~list()
{    
     delete head;
}

void list::add_entry()
{
     char temp[MAX]; //variable to read in the journal entry temporarily
     
     
     //loop to read in until we reach the maximum size of the Journal
     while (num_entries > my_journal_size);
     {
     cout<<"Please enter the date for this entry (MM/DD/YYYY): ";
     cin.get(head->data.date, DATE, '\n'); //read in the date
     cin.ignore(100, '\n'); //eat the carriage return
     
     cout<<"Please enter the subject for this entry ";
     cin.get(head->data.subject, SUB, '\n'); //read in subject
     cin.ignore(100, '\n'); //eat the carriage return
     
     cout<<"Please enter your Journal entry ";
     cin.get(temp, MAX, '\n'); //read in the entry
     cin.ignore (100, '\n'); //eat the carriage return
     
     
     ++num_entries; //add 1 to the counter
     
     cout<<endl <<endl <<endl;
     
     node* current = head;
     head = new node
     head->data = new journal_entry;
     head->data->entry = new char[strlen(temp)+1];
     strcpy(head->data->title, temp);
     head->next = current;
     }                
}

void list::display_all(node* head)
{
     if(head)
     {
			cout<<head->data.datedate;
			cout<<endl <<endl;
			cout<<head->data.subject;
			cout<<endl <<endl;
			cout<<head->data->entry
			
			display_all(head->next);
      }
}

void list::search()
{
            cout<<"SEARCH!";  
}
Topic archived. No new replies allowed.