the way find and delete item from link List

November 10, 2010 at 22:01:40
Specs: Macintosh
if any body can help me, i am trying to write tow functions to delete all items in the list and remove them from the memory and the second to search for current data in the list and delete it
////////////////////////
if(you help me)
thank you;
else
thank you, too;
////////////////////////

#include <iostream.h>

struct Link
{
int data;
Link* next;
};
//////////////////////////////////////////
class Link_List
{
private:
Link* first;
public:
Link_List() {
first = NULL;
}

void add_item(int d);

void display();

void distroy_link();

void distroy_all_links(); // doesn't work

void remove_item(int a); // doesn't work
};

void Link_List::add_item(int d) {
Link* newlink = new Link;
newlink->data =d;
newlink->next =first;
first = newlink;
}

void Link_List::display() {
Link* current = first;
while (current !=NULL) {
cout << endl << current->data;
current = current->next;
}
}

void Link_List::distroy_link() {
Link* newlink = first;
delete newlink;
first = first->next;
}
///////////////////////////////////////////////////////////////////////////////////////////

/*void Link_List::remove_item(int a) { // doesn't work
Link* newlink = first;

while (newlink != NULL) {
if (newlink->data == a) {
newlink = newlink->next;
first = newlink;
}
else
newlink = newlink->next;
}
}*/


/*void Link_List::distroy_all_links() { // doesn't work
while (first == NULL) {
distroy_link();
}
}*/



////////////////////////////////////////////////////////////////////////////////////////////
int main (int argc, char * const argv[]) {

Link_List l1;
l1.add_item(22);
l1.add_item(33);
l1.add_item(44);
l1.add_item(55);
l1.display();

cout << "\nNow distroy_link called";
l1.distroy_link();

l1.display(); cout << endl;
return 0;
}


See More: the way find and delete item from link List

Report •

#1
November 11, 2010 at 22:38:25
try this
............................................................................................

void remove(int d)
{
Link* Current = first;

if (Current == NULL)
return;

while (Current != NULL))
{
if (Current->data == d)
{
Link* del = Current;
Current = Current->Link;
delete del;
return;
}

Current = Current->Link;
}
}

void Destroy()
{
Link* Current;

if (first == NULL)
return;

while ( first != NULL)
{
Current = first;
first = first->Link;
delete Current;
}
}

..........................................................................

_Adnan_


Report •

#2
November 12, 2010 at 23:26:21

thank you



Report •

#3
November 13, 2010 at 04:40:09
Try this...............


void Link_List::remove_item(int d)
{
Link* corrent = first;
Link* delet = NULL;

if (corrent ==NULL)
return;
while(corrent != NULL) {
if (corrent->data = d) {
delet = corrent;
corrent = corrent->next;
delete delet;
}
}
}

.................................................

_Adnan_


Report •

Related Solutions

#4
November 13, 2010 at 14:50:33
first thank you so much because you gave me a good point to think about it next time
for both of the functions, but the first one there something wrong maybe with my compiler if you can help , thank you again(o_o)
void Link_List::remove_item(int d)
{
Link* corrent = first;

if (corrent ==NULL)
return;
while(corrent != NULL) {
if (corrent->data = d) {
link* delet = corrent; // here it tells me that delet not declared in this scope
corrent = corrent->next;
delete delet; // error: type '<type error>' argument given to 'delete', expected pointer
}
}
}


Report •

#5
November 13, 2010 at 22:10:34
you're welcome......

.........................................................

_Adnan_


Report •

Ask Question