delete at the nth position in linked list

0 ragmar thomas · October 11, 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 *theDeleteTitle1 = head, *theDeleteTitle2;
string lost_Book;

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

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

while(theDeleteTitle1 != NULL){

theDeleteTitle1 = theDeleteTitle1->next;
if(lost_Book == theDeleteTitle1->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 = -1;

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

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

i don't know why the delete part iof the function  is not working 
	while(theDeleteTitle1 != NULL && lost_Book != theDeleteTitle2->title){
theDeleteTitle1 = theDeleteTitle1->next;
if(lost_Book == theDeleteTitle1->title){

if(theDeleteTitle1 == 0){

theDeleteTitle2->next = 0;
delete theDeleteTitle1;
}
theDeleteTitle2->next = theDeleteTitle1->next;
delete theDeleteTitle1;
}
theDeleteTitle1 = theDeleteTitle2;
}
}


any suggestions to fix this issue

Post a Reply

Replies

Oldest  Newest  Rating
0 ragmar thomas · October 13, 2014
i did this i end up with the same problem only deleteing the first node
//the delete function
void deleteBook(){
    ddpl *theDeleteTitle = head, *temp = theDeleteTitle;
    string lost_Book;

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

    while(theDeleteTitle != 0){
        if(theDeleteTitle->title == lost_Book){
            if(theDeleteTitle == head){
                head = theDeleteTitle->next;
                delete theDeleteTitle;
                return;
            }
            else{
                temp->next = temp->next;
                delete theDeleteTitle;
                return;
            }
        }
            else
                temp = theDeleteTitle->next;
    }

}
0 ragmar thomas · October 13, 2014
You mean third pointer variable, i have already temp and theDeleteTitle.
And i have done it like this i don't really know what is left for this code to work properly


#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;
    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 == 0){
       head = theInsert1;
       theInsert1->next = 0;
   }
   else{
        theInsert1->next = head;
        head = theInsert1;
    }
}
//the delete function
void deleteBook(){
    ddpl *theDeleteTitle = head;
    string lost_Book;

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

        if(lost_Book == theDeleteTitle->title){
            head = theDeleteTitle->next;
            delete theDeleteTitle;
            return;
        }
            ddpl *temp = head;
            while(temp->next != 0 && temp->next->title != lost_Book){
                    temp = temp->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 = -1;

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

        switch(choice){
    case 1:insertTitle();break;
    case 2:displayTilte();break;
    case 3:deleteBook();break;
    default:cout << "Bye!!!\n";
        }
    }
}
0 ragmar thomas · October 13, 2014
void deleteBook(){
    ddpl *theDeleteTitle = head;
    string lost_Book;

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

        if(lost_Book == theDeleteTitle->title){
            head = theDeleteTitle->next;
            delete theDeleteTitle;
            return;
        }
            ddpl *temp = head;
            while(theDeleteTitle->next != 0){
                    if(lost_Book == theDeleteTitle->title){
                        theDeleteTitle->title;
                    theDeleteTitle->next = temp->next;
                    delete temp;
            }
        }

the deletion of the first node works but  for the nth wont work logically looks fine but technically is not working tips to improve this
  • 1

C++

107,247 followers
About

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

Links
Moderators
Bucky Roberts Administrator