delete linked list

0 ragmar thomas · September 30, 2014
#include <iostream>
#include <string>
#include <fstream>

using namespace std;

struct ddpl{
string author, title, show_title, choice, user_name, borrowed_book_title, borrowed_book_author;
string isbn, shelf_no, id, borrow_date, due_date;
struct ddpl *next;
};
struct ddpl *head;
//the insert function
void insertTitle(){
ddpl *theInsert1 = new ddpl;

cout << "Title: ";
getline(cin, theInsert1->title);
cout << "Author: ";
getline(cin, theInsert1->author);
cout << "ISBN: ";
cin >> theInsert1->isbn;
cout << "Shelf_No: ";
cin >> theInsert1->shelf_no;

if(theInsert1 == NULL){
head = theInsert1;
theInsert1->next = NULL;
}
else{
theInsert1->next = head;
head = theInsert1;
}
}
//the delete function
void deleteBook(ddpl &adding){
ddpl *theDeleteTitle = head, *temp;
string lost_Book;

cout << "enter lost book to delete: ";
getline(cin, lost_Book);

while(theDeleteTitle != NULL){
if(lost_Book == adding.title){
head = theDeleteTitle->next;
delete theDeleteTitle;
return;
}
else{
theDeleteTitle = theDeleteTitle->next;
temp = theDeleteTitle->next;
theDeleteTitle->next = temp->next;
delete temp;
}
}
}
//the print function
void displayTilte(){
ddpl *theDisplay = head;

cout << "\nthe books in the list are\n" << endl;
while(theDisplay != NULL){
cout << theDisplay->title << ',';
cout << theDisplay->author << ',';
cout << theDisplay->isbn << ',';
cout << theDisplay->shelf_no << '\n';
theDisplay = theDisplay->next;
}
cout << endl;
}
//the main function
int main(){
head = NULL;

cout << "enter your choice from the menu" << endl;
cout << "1. For Adding Title\n";
cout << "2. For Searching Title\n";
cout << "3. For Delete Title\n";
int choice;

while(choice != 0){
int position;
ddpl adding;
cout << "\nEnter your choice: ";
cin >> choice;cin.ignore();

switch(choice){
case 1:insertTitle();break;
case 2:displayTilte();break;
case 3:deleteBook(adding);break;
default:cout << "Bye!!!\n";
 }
}
}

the delete part is not working and it crashing.

Post a Reply

Replies

Oldest  Newest  Rating
0 ragmar thomas · October 1, 2014
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>

using namespace std;

struct ddpl{
string author, title, show_title, choice, user_name, borrowed_book_title, borrowed_book_author;
string isbn, shelf_no, id, borrow_date, due_date;
struct ddpl *next;
};
struct ddpl *head;

//the insert function
void insertTitle(){
ddpl *theInsert1 = new ddpl;

cout << "Title: ";
getline(cin, theInsert1->title);
cout << "Author: ";
getline(cin, theInsert1->author);
cout << "ISBN: ";
cin >> theInsert1->isbn;
cout << "Shelf_No: ";
cin >> theInsert1->shelf_no;


if(theInsert1 == NULL){
head = theInsert1;
theInsert1->next = NULL;
}
else{
theInsert1->next = head;
head = theInsert1;
}
}
//the delete function
void deleteBook(ddpl &adding){
ddpl *theDeleteTitle1, *theDeleteTitle2;
string lost_Book;

cout << "enter lost book to delete: ";
getline(cin, lost_Book);

if(lost_Book == adding.title){
head = head->next;
delete theDeleteTitle1;
}
theDeleteTitle1 = theDeleteTitle2;

while(theDeleteTitle1 != NULL){

theDeleteTitle1 = theDeleteTitle1->next;
if(lost_Book == adding.title){

if(theDeleteTitle1 == 0){

theDeleteTitle2->next = 0;
delete theDeleteTitle1;
}
theDeleteTitle2->next = theDeleteTitle1->next;
delete theDeleteTitle1;
}
theDeleteTitle1 = theDeleteTitle2;
}
}
//the print function
void displayTilte(){
ddpl *theDisplay = head;

cout << "\nthe books in the list are\n" << endl;
while(theDisplay != NULL){
cout << theDisplay->title << ',';
cout << theDisplay->author << ',';
cout << theDisplay->isbn << ',';
cout << theDisplay->shelf_no << '\n';
theDisplay = theDisplay->next;
}
cout << endl;
}
//the main function
int main(){
head = NULL;

cout << "enter your choice from the menu" << endl;
cout << "1. For Adding Title\n";
cout << "2. For Searching Title\n";
cout << "3. For Delete Title\n";
int choice;

while(choice != 0){
ddpl adding;
cout << "\nEnter your choice: ";
cin >> choice;cin.ignore();

switch(choice){
case 1:insertTitle();break;
case 2:displayTilte();break;
case 3:deleteBook(adding);break;
default:cout << "Bye!!!\n";
}
}
}

what about now i make some modification but the delete part not working yet any idea.
  • 1

C++

106,979 followers
About

Used in many types of software including music players, video games, and many large scale applications.

Links
Moderators
Bucky Roberts Administrator