Linked list, will compile but crashes?

I have a program that is a linked list of books. It compiles fine when I had all the code outside of a class. I was trying to put a class in it, and make a spec and imp file, as I had everything in one file. I got it, to compile but something is causing it to crash. Anyone have an idea what?

Also, I need the program to maintain the list in alphabetical order for three StrType keys: Subject, Author, and Title. Im not really sure what the best way to go about this would be, can someone help me? I think I could do 3, 2dim arrays...but would their be a better way to do it? Anyways heres the code:

Header
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
#include <iostream>
#include <string>

using namespace std;

class Library
{
      public:
         Library(); //Creates an empty library without books and without authors.
         ~Library(); //Destructor
      
         void add_node_at_end();
		 void display_list();
		 void delete_start_node();
		 void delete_end_node();
		 void move_current_on();
		 void move_current_back();
		 int option;
      
      private:
		  struct node
		  {  
			  string title; // Title of book
			  string author;          // Author of book
			  string subject;     // Subject of book
			  node *nxt;// Pointer to next node
		  };
		  
		  node *start_ptr;
		  node *current;		 // Used to move along the list
		  

          
      
};  


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
#include "library.h"

Library::Library()
{
	node *start_ptr = NULL;
	int option = 0;
}

Library::~Library()
{
}

void Library::add_node_at_end()
{  node *temp, *temp2;   // Temporary pointers

     // Reserve space for new node and fill it with data
     temp = new node;
	 cin.ignore();
     cout << "Please enter the name of the person: ";
	 getline(cin, temp->title);
     cout << "Please enter the age of the person : ";
     getline(cin, temp->author);
     cout << "Please enter the height of the person : ";
     getline(cin, temp->subject);
     temp->nxt = NULL;

     // Set up link to this node
     if (start_ptr == NULL)
       { start_ptr = temp;
	 current = start_ptr;
       }
     else
       { temp2 = start_ptr;
         // We know this is not NULL - list not empty!
         while (temp2->nxt != NULL)
           {  temp2 = temp2->nxt;
              // Move to next link in chain
           }
         temp2->nxt = temp;
       }
  }

void Library::display_list()
  {  node *temp;
     temp = start_ptr;
     cout << endl;
     if (temp == NULL)
       cout << "The list is empty!" << endl;
     else
       { while (temp != NULL)
	   {  // Display details for what temp points to
              cout << "Title : " << temp->title << " ";
              cout << "Author : " << temp->author << " ";
	      cout << "Subject : " << temp->subject;
	      if (temp == current)
		cout << " <-- Current node";
              cout << endl;
	      temp = temp->nxt;

	   }
	 cout << "End of list!" << endl;
       }
  }

void Library::delete_start_node()
   { node *temp;
     temp = start_ptr;
     start_ptr = start_ptr->nxt;
     delete temp;
   }

void Library::delete_end_node()
   { node *temp1, *temp2;
     if (start_ptr == NULL)
          cout << "The list is empty!" << endl;
     else
        { temp1 = start_ptr;
          if (temp1->nxt == NULL)
             { delete temp1;
               start_ptr = NULL;
             }
          else
             { while (temp1->nxt != NULL)
                { temp2 = temp1;
                  temp1 = temp1->nxt;
                }
               delete temp1;
               temp2->nxt = NULL;


Main
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
#include "library.h"

void main()
{  
	Library lib;

     do
	{
	  lib.display_list();
	  cout << endl;
	  cout << "Please select an option : " << endl;
	  cout << "0. Exit the program." << endl;
	  cout << "1. Add a node to the end of the list." << endl;
	  cout << "2. Delete the start node from the list." << endl;
	  cout << "3. Delete the end node from the list." << endl;
	  cout << "4. Move the current pointer on one node." << endl;
          cout << "5. Move the current pointer back one node." << endl;
          cout << endl << " >> ";
	  cin >> lib.option;

	  switch (lib.option)
	    {
	      case 1 : lib.add_node_at_end(); break;
	      case 2 : lib.delete_start_node(); break;
	      case 3 : lib.delete_end_node(); break;
	      case 4 : lib.move_current_on(); break;
          case 5 : lib.move_current_back();
	    }
	}
     while (lib.option != 0);
  }
Is there a reason you write your own list implementation? Why don't you use std::list or std::multiset (multiset comes with auto-sorting)?
I haven't learned it yet...it does sound much easier, assuming it writes the implementation file for me? Ill have to do some research. Those type of things haven't been taught in any of my textbooks yet. I got almost everything done without them though. Anyone figure out what was making this crash?
Why don't you just use the debugger and look at the call stack after the crash occurs. What IDE are you using? It would also help if you posted one complete compilable unit. What you have posted isn't even complete. Even if someone wanted to help you they could not easily compile what you have posted and execute the program. Where is the rest of the delete_end_node() function?
@imi,
OP is likely learning how to write a linked list implementation, probably part of a college/university course.

@kempofighter,
I don't know about the VC++ debugger, but gdb often helpfully displays "??()" for function names; so even a debugger might not be much use here. Certainly I've found commenting out different lines of code, using assert() and printf()/std::cout as well as my own brain far more useful than gdb. I don't know why the FSF doesn't just scrap that project and start again. gdb is awful.

@RLB31384,
std::list and std::multiset are just implementations of a linked list so you don't have to try. If I'm right in assuming you're doing this because you've been told to do it for homework or something, then you should do it yourself. But as kempofighter says, you gotta post the whole class (or better yet, the whole program) so we can actually help.
I think every programmer should do a linked list as part of their pointer education.

There is a problem right at the start with the Library constructor;

1
2
3
4
5
6
Library::Library()
{
	node *start_ptr = NULL; //This is wrong - this creates a local variable which is destroyed when the constructor completes!!!!!!!!
	int option = 0; //Same problem as above
}
Last edited on
I agree. Linked lists aren't that difficult a concept to grasp, and they're very useful. For example, Bash uses a linked list to store the arguments to a program (argv[]).

I did doubly linked lists (I partly copied an example for a singly-linked list, and then did the rest with trial-and-error) but I did that with C, purely with structs and a main() function. It was very simplistic.
Last edited on
Im not sure why it didnt post all the code, anyways. I think I tried to post the rest in a post under it..and it must not have went through.

Anyways..I worked on the program not in a class...and now im attempting to get it back in a class
..heres the new code that I am trying to get in a class:

http://www.cplusplus.com/forum/general/20340/

I tried earlier and it wouldn't work, if anyone has any help post it on that topic. Im going to try again here in a second to get it in a class. Ill post my results there.

Thanks!
okay my new code is doing the same thing, ill post all of it at the link above. It compiles, but crashes on startup

Unhandled exception at 0x00415baa in CS 310 - Project 2.exe: 0xC0000005: Access violation reading location 0xcccccd2c.

Maybe its the reason guestgulkan said? I thought I had to use a constructor to assign something. And i thought the constructor assigns the value, then its destroyed but the value stays there with the variable which is in the class?

Topic archived. No new replies allowed.