Im having real trouble seeing this, how do I delete something out of a linked list if it is already there? My current delete function just deletes everything out of the list.

the text file contains numbers like this:
75
85
95
25
35
75
85
95
25

#include <iostream>
#include <fstream>

using namespace std;

class List
{
public:
	void Insert(int);
	void Print();
	//
	void Delete(int);
	int Length(int);
};

struct node
{
	int item;
	node *nxt;
}; 
node *start_ptr = 0;
node *current;


void List::Delete(int item)
{
	if(start_ptr==NULL)
		cout<<"NULL";

	node* temp=start_ptr;

	if(temp->item==item)
	{
		start_ptr=temp->nxt;

		delete temp;
	}
}
void List::Insert(int item)
  {  
	  node *temp, *temp2;   // Temporary pointers

     // Reserve space for new node and fill it with data
     temp = new node; 
	 temp->item=item;
    
     temp->nxt = NULL;

     // Set up link to this node
     if (start_ptr == NULL)
       { 
		   start_ptr = temp;
		   current = start_ptr;
       }
     else
       { 
		   temp2 = start_ptr;
       
         while (temp2->nxt != NULL)
           {  
			   temp2 = temp2->nxt;
              // Move to next link in chain
           }
         temp2->nxt = temp;
       }
  }
void List::Print()
  {  
	 node *temp;
     temp = start_ptr;
     
     if (temp == NULL)
       cout << "The list is empty" << endl;
     else
       { while (temp != NULL)
	   {  // Display details for what temp points to
              cout  << temp->item << " ";
	   
	      if (temp == current)
		cout << " <--";
              cout << endl;
	      temp = temp->nxt;

	   }
	 cout << "End of list" << endl;
       }
  }
int List::Length(int item)
{
	node *temp;
	temp=start_ptr;
	temp->item=item;
   
   int count=0;

   while(temp != 0)
   {
	   temp=temp->nxt;
	   count++;
   }
   return count;
}
void main()
  {  
	  List display;
	  fstream inFile;
	  node number;
	  inFile.open("numbers.txt");

while(inFile>>number.item)
{
	display.Insert(number.item);
	display.Delete(number.item);
}
	display.Print();
	//cout<<display.Length(number.item);
}

Recommended Answers

All 8 Replies

Its done something like this: Note: not compiled or tested.

node* temp=start_ptr;
node* prev = NULL;
while( temp )
{
   if( temp->item == item) 
   {
          // delete this node
         if( prev == NULL)
         {
            // we are at the head of the linked list, for remove the head
            start_ptr = start_ptr->nxt;
         }
         else
         {
               // we're somewhere in the middle of the linked list
               prev->nxt = temp->nxt;
        }
        delete temp;
        break; // all done
   }
   prev = hold;
    hold = hold->next;
}

This just seems to do the samething, I get an empty list by the end.

post new code.

#include <iostream>
#include <fstream>

using namespace std;

class List
{
public:
	void Insert(int);
	void Print();
	//
	void Delete(int);
	int Length(int);
};

struct node
{
	int item;
	node *nxt;
}; 
node *start_ptr = 0;
node *current;


void List::Delete(int item)
{
	node* temp=start_ptr;
    node* prev = NULL;
	node* hold;

while( temp )
{
   if( temp->item == item) 
   {
          // delete this node
         if( prev == NULL)
         {
            // we are at the head of the linked list, for remove the head
            start_ptr = start_ptr->nxt;
         }
         else
         {
               // we're somewhere in the middle of the linked list
             prev->nxt = temp->nxt;
        }
        delete temp;
        break; // all done
   }
    prev = hold;
    hold = hold->nxt;
}
}
void List::Insert(int item)
  {  
	  node *temp, *temp2;   // Temporary pointers

     // Reserve space for new node and fill it with data
     temp = new node; 
	 temp->item=item;
    
     temp->nxt = NULL;

     // Set up link to this node
     if (start_ptr == NULL)
       { 
		   start_ptr = temp;
		   current = start_ptr;
       }
     else
       { 
		   temp2 = start_ptr;
       
         while (temp2->nxt != NULL)
           {  
			   temp2 = temp2->nxt;
              // Move to next link in chain
           }
         temp2->nxt = temp;
       }
  }
void List::Print()
  {  
	 node *temp;
     temp = start_ptr;
     
     if (temp == NULL)
       cout << "The list is empty" << endl;
     else
       { while (temp != NULL)
	   {  // Display details for what temp points to
              cout  << temp->item << " ";
	   
	      if (temp == current)
		cout << " <--";
              cout << endl;
	      temp = temp->nxt;

	   }
	 cout << "End of list" << endl;
       }
  }
int List::Length(int item)
{
	node *temp;
	temp=start_ptr;
	temp->item=item;
   
   int count=0;

   while(temp != 0)
   {
	   temp=temp->nxt;
	   count++;
   }
   return count;
}
void main()
  {  
	  List display;
	  fstream inFile;
	  node number;
	  inFile.open("numbers.txt");

while(inFile>>number.item)
{
	display.Insert(number.item);
	display.Delete(number.item);
}
	
	display.Print();
	//cout<<display.Length(number.item);
}

The problem is in main(), not that delete function. First it calls insert to insert a new number then turns right around and deletes it. Delete that line

while(inFile>>number.item)
{
	display.Insert(number.item);
	//display.Delete(number.item);  <<<<< delete this line
}

But I want the delete function to delete any duplicates in the linked list. If I just delete that function then I still have the whole Linked list, duplicates included.
the text file contains numbers like this:
75
85
95
25
35
75
85
95
25

I want it to display these numbers only out of that list:
75
85
95
25
35

Check to see if the number is already in the list before adding it. The delete() function is not the place to do that. Write another class method that does nothing but search the list for a number, return true if the number already exists in the list or false if it doesn't. Then if it doesn't call that Insert() function.

Ok thanks for the help, I thought I had to delete from the list already created...I didnt think to just not include them!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.