Linked List search and management troubles

Hello there, I was hoping you all could help me with some issues I'm having. It is a program that creates a linked list of Books and their prices. It's a very simple program, but I'm still really new with c++. The program successfully creates the list and is able to add nodes. It can display them and it can delete them.

I have noticed that it does have some issues with deleting nodes. It can't delete the first node without crashing the program. I'm not too sure how to use the debugging in MS Visual Studio so I'm not sure what causes the problem. However if you delete another node first then you can delete other nodes without any issues. I've tried a few rewrites to the code, but I can't seem to quite fix the problem.

The program also needs to be able to search for a book by the title. My search function seems to work fine for since word titles but if there are spaces it doesn't find the book. I've tried using different comparison commands, but I wasn't able to find one that worked.

Any help you could give me would be awesome, also if you have any pointers on how to make the code better written would be even better. Thanks in advance for any help.

Here is the 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
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#include <iostream>
#include <string.h>

using namespace std;
int i = 0;

struct Book { 
	char title[1024];		// Book name
	int id;					// Book id or ISBN number
	float price;			// Book price
	struct Book* next;
} *COLLECTION = NULL;

//-- Forward Declaration --// 
void menu(); 
void branching(char option);
void insertion();
void printall();
struct Book* search(); 
void deletion();
void quit(struct Book* HEAD);


int main()
{
	char ch; 

	cout << "\n\nWelcome to CSE240: Bookstore\n";

	do {
		 menu();
		 ch = tolower(getchar()); // read a char, convert to lower case
		 cin.ignore();

		 branching(ch);
	} while (ch != 'q');
	
	return 0; 
}

void menu()
{
	cout << "\nMenu Options\n";
	cout << "------------------------------------------------------\n";
	cout << "i: Insert a book\n";
	cout << "d: Delete a book\n";
	cout << "s: Search a book\n";
	cout << "p: Review your list\n"; 
	cout << "q: Quit\n";
	cout << "\n\nPlease enter a choice (i, d, s, p, or q) ---> "; 
}

void branching(char option)
{
	switch(option)
	{
		case 'i':
			insertion();
		break;

		case 'd':
			deletion();
		break;

		case 's':
			search();
		break;

		case 'p':
			printall();
		break;

		case 'q':
			quit(COLLECTION);
			COLLECTION = NULL;
		break;

		default:
			cout << "\nError: Invalid Input.  Please try again..."; 
		break;
	}
}


void insertion()				//Creates a link and adds it to the beginning of linked list
{
	struct Book *p;
	p = (struct Book *) malloc(sizeof(struct Book));	//Checks memory usage
	if( p == 0)
		cout <<"out of memory\n";

	cout <<"\nWhat is the name of book?\n";
	
	cin.getline(p->title, 1024);

	cout <<"\nWhat is the price?\n";
	cin >>p->price;
	cin.ignore();

	p->id = i;					//Sets id to unique incremented value
	i++;

	p->next = COLLECTION;		//Adds to beginning of list
	COLLECTION = p;


	// add code to insert a new book into the COLLECTION linked list.   
	// HINT: You can insert a new book at the beginning of the linked list


}

struct Book* search()			//Searches linked list for book based on title
{
	
	char sname[30];
	struct Book *p = COLLECTION, *b = p;
	cout <<"\nPlease enter the name to be searched for:\n";
	cin >> sname;
	cin.ignore();
	while(p != 0)
		if((strcmpi(sname, p->title)) == 0){		//Compares string and returns previous reference
			cout <<"ID:" <<p->id;
			cout <<"\nPrice:" <<p->price;
			return b->id;
		}
		else{
			b = p; 
			p = p->next;
		}
		cout <<"\nThe name does not exist.\n";		//Returns 0 if not found
		return 0;

	// add code to search for an existing book in the COLLECTION linked-list
	// HINT: If no book matches the tile return a error messag
}

void deletion()				//Deletes link based on entered ID
{
	int sID;
	struct Book *p = COLLECTION, *b = p;
	cout <<"\nPlease enter the ID to be deleted:\n";
	cin >> sID;
	cin.ignore();
	while(p != 0)						//Loops through list to compare ID's
		if(b->next == NULL){			//Case: If link is the first and only link deletes and sets head to null
			free(b);
			b = NULL;
			COLLECTION = NULL;
			return;
		}
		
		else if(sID == b->next->id){	//Case: Link found sets previous link to point to the link after the found link, then deletes
			b = b->next;
			p->next = p->next->next;
			delete b;
			return;
		}
		else{							//Case: not found moves to next link
			b = p; 
			p = p->next;
		}
		cout <<"\nThat ID does not exitst.\n";
}

void printall()							//Prints all entered links, with fixed output format of 2 decimal places
{
	struct Book *p = COLLECTION;
	cout.setf(std::ios::fixed);
	cout.precision(2);

	cout <<"\n{Book Id} : {Book Title} for {Book Price}.";

	while(p != 0){						//Moves through list and prints info
		cout <<"\n" <<p->id <<": " <<p->title <<" for $" <<p->price <<".";
		p = p->next;
	}
	// Add code to print the book collection.  (HINT: You will need to use a loop.)
}

void quit(struct Book* HEAD)			//Deletes linked list before closing
{
	if(HEAD == NULL)
		return;
	else{
		quit(HEAD->next);
		delete HEAD;
	}


	// Add code to delete the objects/books from the lniked-list.
	// HINT: Refer to the slides in the homework assignmetn
}
Topic archived. No new replies allowed.