Hey guys .. down there is my code for a program in C++ that deals with nodes.. i
ID: 3615883 • Letter: H
Question
Hey guys .. down there is my code for a program in C++ that deals with nodes.. it works just fine ,,
but i have a problem with the function "Remove"..
instead of deleting the node I want, it deletes the one right after. What did I do wrong and how can I fix it?
Plz asnwer me as fast as possible!
#include
#include
using
struct
{
string item;
Node *link;
};
typedef
void
{
NodePtr temp =
temp->item = an_item;
temp->count = no;
temp->link = head;
head = temp;
}
void
{
NodePtr temp =
temp->item = an_item;
temp->count = no;
temp->link = after_me->link;
after_me->link = temp;
}
NodePtr search(NodePtr head, string target)
{
NodePtr here = head;
here = here->link;
}
void
{
cout << to_show->item <<
}
void
{
string to_be_removed;
cout <<
<<
cin >> ans;
{
cin >> to_be_removed;
NodePtr remove = search (head,to_be_removed) ;
NodePtr replace = remove->link;
remove ->link = replace->link;
cout <<
show_list(head);
}
}
int
{
NodePtr head =
string ITEM, TARGET;
head->link=NULL;
head_insert (head,
head_insert (head,
head_insert (head,
cout <<
show_list(head);
cout <<
cin >> ITEM;
cout <<
cin >> COUNT;
cout <<
cin >> TARGET;
NodePtr AFTER_ME = search(head,TARGET);
cout <<
{
cout <<
<< COUNT <<
Explanation / Answer
#include <iostream> #include <string> using namespace std; struct Node { string item; int count; Node *link; }; typedef Node* NodePtr; void head_insert (NodePtr& head, string an_item, intno) { NodePtr temp = new Node; temp->item = an_item; temp->count = no; temp->link = head; head = temp; } void insert (NodePtr after_me, string an_item, int no) { NodePtr temp = new Node; temp->item = an_item; temp->count = no; temp->link = after_me->link; after_me->link = temp; } NodePtr search(NodePtr head, string target) { NodePtr here = head; if (here == NULL) return NULL; while ( (here->item != target) && (here->link !=NULL) ) here = here->link; if (here->item == target) return here; else return NULL; } NodePtr search_ago(NodePtr head, stringtarget) { NodePtr here = head,prev; if (here == NULL) return NULL; prev=NULL; while ( (here->item != target)&& (here->link != NULL) ) { prev=here; here = here->link; } if (here->item ==target) return prev; else return NULL; }void show_list(NodePtr head) { for(NodePtr to_show = head; to_show->link!=NULL;to_show=to_show->link) cout << to_show->item << " " <<to_show->count << endl; } void Remove (NodePtr head) { string to_be_removed; char ans; cout <<"Do you want to remove a node from the list? " << "if so, which one? "; cin >> ans; if (ans == 'Y' || ans == 'y') { cin >> to_be_removed; NodePtr remove = search_ago(head,to_be_removed) ; NodePtr replace = remove->link; remove ->link = replace->link; cout <<"List After removing: "; show_list(head); delete remove; } } int main () { NodePtr head = new Node; string ITEM, TARGET; int COUNT; head->link=NULL; head_insert (head, "Tea", 2); head_insert (head, "Jam", 3); head_insert (head, "Rolls", 10); cout <<"List contains: "; show_list(head); cout <<"Enter the item you wish to insert (string) "; cin >> ITEM; cout <<"Enter the count of new item "; cin >> COUNT; cout <<"Enter the item after which you want to insertthe new node "; cin >> TARGET; NodePtr AFTER_ME = search(head,TARGET); if (AFTER_ME == NULL) cout << "The Item you entered was not found "; else { cout <<" will insert " <<ITEM <<" withcount " << COUNT <<" after the node with " << TARGET<< endl; insert (AFTER_ME , ITEM, COUNT); } cout <<"List now contains: "; show_list(head); Remove (head); system("pause"); return 0; }