Hi, I am having an issue while creating the del() function(for deleting node) for linked list in C++. I have tried several methods to create the function but none of them is working, I have given the code below along with its output, let me know if you can help me.
class Node {
public:
string NAME;
string MEANING;
char INITIAL;
Node* link;
Node() {
}
};
class LinkedList {
private:
Node* head, * tail, * current, * temp;
public:
LinkedList() {
head = NULL;
}
void InsertAtTail(char initial, string name, string meaning) {
if (head == NULL) {
head = new Node;
head->INITIAL = initial;
head->NAME = name;
head->MEANING = meaning;
head->link = NULL;
tail = head;
}
else {
temp = new Node;
temp->INITIAL = initial;
temp->NAME = name;
temp->MEANING = meaning;
temp->link = NULL;
tail->link = temp;
tail = temp;
}
}
void display() {
Node* current = head;
while (current != NULL) {
cout << "Name: " << current->NAME;
cout << "\tMeaning: " << current->MEANING << endl;
current = current->link;
}
}
void search(string n) {
Node* current = head;
while (current != NULL) {
if(n == current->NAME){
cout << "Name found in database!"<<endl;
cout << "Name: " << current->NAME;
cout << "\tMeaning: " << current->MEANING << endl;
current = current->link;
}
else
current = current->link;
}
}
void add(char i, string n, string m) {
Node* current = head;
while (current != NULL) {
if(i == current->INITIAL){
temp = new Node;
temp->INITIAL = i;
temp->NAME = n;
temp->MEANING = m;
current->link = temp;
temp->link = current->link;
break;
}
else
current = current->link;
}
}
void del(string n) {
Node* current = head;
Node* temp = NULL;
if (current != NULL && current->NAME == n){
temp->link = current->link;
delete(current);
return;
}
else{
while (current != NULL && current->NAME != n) {
temp = current;
current = current->link;
/*if(current->NAME == n){
Node* temp = current;
current = current->link;
delete(temp);
//current->link = current;
//delete(temp);
//break;
}
else
current = current->link;*/
}
}
}
};
int main() {
LinkedList list;
fstream boys("boys.txt", ios::in);
string name, meaning;
char initial;
while (boys >> name >> meaning >> initial)
{
list.InsertAtTail(initial, name, meaning);
}
list.del("Ali");
list.display();
boys.close();
return 0;
}
The output is
Name: Aaidan Meaning: Young
Name: Aamir Meaning: Civilized
Name: Aaron Meaning: Enlightened
Name: Abbas Meaning: Lion
Name: Abeer Meaning: Scent
Name: AbuZar Meaning: Rich
Name: Adeel Meaning: Moderate
Name: Afan Meaning: To_Forgive
Name: Ahmad Meaning: Noble
Name: Ahnaf Meaning: Narrator
Name: Ahsan Meaning: Superior
Name: Ali Meaning: Lion
Name: Amir Meaning: Prince
Name: Ammar Meaning: Pious
Name: Ansar Meaning: Helpful
Name: Aqib Meaning: Successor
Name: Arafat Meaning: Mount_Of_Recognition
Name: Arbaaz Meaning: Eagle
Name: Arham Meaning: Mercy
Name: Arman Meaning: Wish
Name: Arsal Meaning: Sent_One
Name: Arsalan Meaning: Lion
Name: Arshman Meaning: Pride_Of_Sky
Name: Aryan Meaning: Warrior
Name: Asad Meaning: Fortunate
Name: Asal Meaning: Honey
Name: Asghar Meaning: Smaller
Name: Ashar Meaning: Wise
Name: Ashir Meaning: Wealthy
Name: Askari Meaning: Legions
Name: Athar Meaning: Pure
Name: Atif Meaning: Compassionate
Name: Aun Meaning: Helper
Name: Baqir Meaning: Genius
Name: Barir Meaning: Faithful
Name: Basil Meaning: Brave
Name: Basim Meaning: The_smiling_one
Name: Basit Meaning: One_Who_Enlarges
Name: Bilal Meaning: Moistening
Name: Burhan Meaning: Proof
Name: Danish Meaning: Intellect
Name: Dayyan Meaning: Revenger
Name: Faheem Meaning: Judicious
Name: Faiz Meaning: Triumphant
Name: Faraj Meaning: Cure
Name: Farhan Meaning: Laughter
Name: Faris Meaning: Perspicacity
Name: Farjad Meaning: Excellent
Name: Faseeh Meaning: Ample
Name: Fawad Meaning: Heart
Name: Fida Meaning: Sacrifice
Name: Furqan Meaning: Evidence
Name: Ghazanfar Meaning: Brave
Name: Ghazi Meaning: Conqueror
Name: Habib Meaning: Loved
Name: Hadi Meaning: Leader
Name: Haider Meaning: Virtuous
Name: Hammad Meaning: Admirable
Name: Hamza Meaning: Competent
Name: Hanif Meaning: True_Believer
Name: Haris Meaning: Vigilant
Name: Hasan Meaning: Gentle
Name: Haseeb Meaning: Elderly
Name: Hashir Meaning: Collector
Name: Hayyan Meaning: Lively
Name: Hesham Meaning: Brave
Name: Hunain Meaning: Valley
Name: Hur Meaning: Fair
Name: Husnain Meaning: Elegant
Name: Hussain Meaning: Good
Name: Huzaifa Meaning: Wise
Name: I�bad Meaning: Servants
Name: Idris Meaning: Instructor
Name: Imad Meaning: Faith
Name: Imran Meaning: Happiness
Name: Irfan Meaning: Thankfulness
Name: Ismail Meaning: Heard_by_God
Name: Jafar Meaning: Rivulet
Name: Jawad Meaning: Generous
Name: Jazib Meaning: Absorber
Name: Jibran Meaning: Changer
Name: Junaid Meaning: Warrior
Name: Kaab Meaning: Honorable
Name: Kabeer Meaning: Elderly
Name: Kafeel Meaning: Sponsor
Name: Kaif Meaning: Pleasure
Name: Kashan Meaning: Lucky
Name: Kashif Meaning: Reveler
Name: Khabeeb Meaning: Smart
Name: Khizar Meaning: Green
Name: Kiyan Meaning: Existence
Name: Maaz Meaning: Refuge
Name: Maheer Meaning: Bold
Name: Mahir Meaning: Skilled
Name: Manaf Meaning: Negator
Name: Masab Meaning: Rewarded
Name: Mohsin Meaning: Humanitarian
Name: Moosa Meaning: Moses
Name: Mujtaba Meaning: Selected
Name: Murtaza Meaning: Favorite
Name: Muzzammil Meaning: Wrapped
Name: Naif Meaning: Stable
Name: Naqi Meaning: Pure
Name: Owais Meaning: Fearless
Name: Paras Meaning: Touchstone
Name: Qamber Meaning: Salve
Name: Qasim Meaning: Divider
Name: Raza Meaning: Contentment
Name: Rehan Meaning: Scented
Name: Roomi Meaning: Citizen
Name: Saad Meaning: Felicity
Name: Sahil Meaning: Riverbank
Name: Sahir Meaning: Wakeful
Name: Saif Meaning: Sword
Name: Sajid Meaning: One_Who_Prostrates
Name: Salah Meaning: Goodness
Name: Salar Meaning: Leader
Name: Salman Meaning: Companion
Name: Sameer Meaning: Jovial
Name: Shabbeer Meaning: Social
Name: Shahan Meaning: Profit_maker
Name: Shahroz Meaning: Lavish
Name: Shahrukh Meaning: Monarchy
Name: Shahzad Meaning: Prince
Name: Shahzain Meaning: Skillful
Name: Shayan Meaning: Worthy
Name: Shehryar Meaning: King
Name: Sheraz Meaning: Loving
Name: Sherjeel Meaning: Spark
Name: Shuja� Meaning: Courageous
Name: Sohaib Meaning: Sandy
Name: Sohail Meaning: Ease
Name: Suleman Meaning: Security
Name: Tabish Meaning: Warmth
Name: Taha Meaning: Pure
Name: Tajammul Meaning: Dignity
Name: Talha Meaning: Fruitful
Name: Taqi Meaning: God_Fearing
Name: Turab Meaning: Ground
Name: Ubaid Meaning: Faithful
Name: Umair Meaning: Life
Name: Usama Meaning: Lion
Name: Uzair Meaning: Helper
Name: Wajeeh Meaning: Eminent
Name: Wali Meaning: Governor
Name: Waqas Meaning: Solider
Name: Wasif Meaning: Definition
Name: Yasir Meaning: Wealthy
Name: Yawar Meaning: Adjutant
Name: Yazdan Meaning: Merciful
Name: Yureed Meaning: Optation
Name: Yusuf Meaning: Power
Name: Zafer Meaning: Help
Name: Zahid Meaning: Devout
Name: Zaid Meaning: Growth
Name: Zain Meaning: Adornment
Name: Zargham Meaning: Lion
Name: Zarrar Meaning: Strict
Name: Zawar Meaning: Pilgrimages
Name: Zayan Meaning: Graceful
Name: Zimar Meaning: Nice
Name: Zohaib Meaning: Leader
Name: Zubair Meaning: Firm
Name: Zuhayr Meaning: Sparkling
Name: Zulfiqar Meaning: Cleaver
Name: Zunair Meaning: Shine
The Name Ali is still appearing the list. I don't know why delete function isn't working...
Kindly help me getting the function work...
Top comments (2)
It seems to be a forward-only linked list. Boy, I haven't done C++ in a decade.
By examining the
IF
before thewhile
I see you usetemp
to save the next link after the one about to be deleted. It is good that you do that, but it isn't good that you do it withtemp
becausetemp
is NULL.Yes, you need 2 pointers, but you're doing it incorrectly. To make the surgery, you need a pointer pointing to the one that is previous to the one being deleted, so you can do
previous->link = current->link;
. Apply this logic inside thewhile
loop. As you currently have the loop, it does nothing. It just stops when the name is found and that's it.Welcome to DEV!
You could get syntax highlighting for your code-snippet and make it more readable by adding the language after the opening triple backticks. In this case it would be
cpp
:See the markdown cheatsheet