Thread: why can't I delete the integers in my linked list?

  1. #1
    LinkedListQ2
    Guest

    Question why can't I delete the integers in my linked list?

    Code:
    #include <iostream>
    #include <vector>
    //#include <list>
    using namespace std;
    
    struct Node
    {
    	int data; 
    
    	Node *next;
    	
    	Node(int d, Node *n) : data(d), next(n)
    	{}
    };
    
    
    class List
    {
    public:
    
    	List()
    	{
    		head = new Node(int(), NULL);
    		//cout << "int() is: " << int() << endl;
    	
    	}
    	
    	void Add(int dataX)
    	{
    
    		Node *it = head; 
    
    
    		while(it->next != 0)// && it->next->data < dataX)
    		{
    
    			it = it->next;
    			//cout << it->data << endl;
    
    		}
    
    		//cout << "it->data is: " << it->data << endl;
    
    		it->next = new Node(dataX, it->next);
    		head = it;
    
    			
    
    		
    
    
    
    	}
    
    
    	bool Count(int y)
    	{
    		//cout << vTail[-1] << endl;
    		return 1;
    	
    	}
    
    	void RemoveAll(int z)
    	{
    		Node *it = head;
    		cout << "head->next is right now: " << head->data << endl;
    		cout << "it->next is right now: " << it->data << endl;
    
    		while(it->next != NULL) // && it->next->data < z)
    		{
    		//	cout << "howdy" << endl;
    			it = it->next;
    		//	cout << "howdy " << it->next << endl;
    		}
    		//if(head->next != 0)
    		//{
    			//Node *it = head->next;
    		//	Node *hold = it->next;
    		//	it->next = hold->next;
    
    		//	delete hold;
    
    		//	while(it != 0)
    		//	{
    				//cout << "now it's: " << it->data << "->" << endl;
    
    		//		it = it->next;
    		//	}
    
    		//}
    
    		if(it->next != NULL)
    		{
    			cout << "yes this works" << endl;
    			Node *hold = it->next;
    			it->next = hold->next;
    			//cout << "hold->next is: don't expect to print->: " << hold->next << endl;
    			
    
    			delete hold;
    
    			//cout << "hold->next is now: " << hold->data << endl;
    			
    			//cout << "while remove, it->data is: " << it->data << endl;
    		}
    
    		//cout << "after remove, it->data is: " << it->data << endl;
    	
    	}
    
    	int Contains(int b)
    	{
    		return 1;
    	}
    
    
    	void walk()
    	{
    		//cout << "head->next in walk is: " << head->data << endl;
    		if(head->next != 0)
    		{
    			Node *it = head->next;
    
    			while(it != 0)
    			{
    				cout << it->data << "->";
    
    				it = it->next;
    				//cout << it->data << "->";
    			}
    
    		}
    
    	}
    
    
    
    
    
    private:
    	Node *head;
    	Node *tail;
    
    	//List *pNext;
    
    	vector<int> vHead;
    	vector<int> vTail;
    
    
    };
    
    
    
    int main()
    {
    	List list;
    	//Node *lptr;
    	
    	//int match;
    	//int search;
    	//Node *list_start = new Node = list.Add(search);
    	
    	//list.FindEntry(lptr, match);
    	
    	//cout << list.Count(9) << endl; //this should print 0
    
    	list.Add(7);
    	list.Add(8);
    	list.Add(9);
    	list.Add(10);
    	//list.walk();
    
    	list.RemoveAll(7);
    	list.RemoveAll(8);
    	list.RemoveAll(9);
    	//list.walk();
    
    	//list.Add(7)->head;
    
    
    	//lptr = list.FindEntry(list_start, match);
    
    	/*if(lptr != 0)
    		cout << "Found: " << lptr->data << endl;
    	else
    		cout << "Not found!" << endl;*/
    
    	
    
    	//cout << list.Add() << endl;
    
    	/*if(list.Contains(12))
    		cout << "Found 12" << endl; //this should print
    	else
    		cout << "Error" << endl;
    
    	if(!list.Contains(15))
    		cout << "Did not find 15" << endl; //this should print
    	else
    
    		cout << "Error" << endl;*/
    
    	//for(int dataX = 0; dataX < 10; dataX++)
    	//	list.Add(dataX);
    	
    	//cout << list.Count(7) << endl; //should print 2, since there are 2 7's in the list
    	
    	list.RemoveAll(0);
    	
    	//cout << list.Count(7) << endl; //should print 0
    
    	return 0;
    }


    in class "List", and member function "RemoveAll"- it seems that I'm Nulling out it->next for some reason, but I'm not sure exactly why.

    How could I modify this code to delete the integers in the list?

    Thanks

  2. #2
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    Your function Add() is a bit strange. You start with it pointing to head, I assume head is a pointer to the start of the list. Then you walk through the list until you find the last element. Seems fine, when the last element is found you add a new element after that one. I can understand that also, but I can't understand why you assign it to head.... Shouldn't tail point to it? You are not building a list this way.

    Example:

    1. You have an empty list.
    2. You add one element, head points to that element.
    3. You add a second element, head points to that element!

    But what about the first element? There's no pointer pointing to it. So let head keep pointing to the start of the list and use tail to point to the end of the list.

    In RemoveAll() you have this piece of code. Why?

    Code:
    while(it->next != NULL) // && it->next->data < z)
    {
    }
    Since head always points to the last element, it->next, when it is assigned to head, is always NULL.

    When cleaning up start with head and walk throught the list until head meets tail.

  3. #3
    Veni Vidi Vice
    Join Date
    Aug 2001
    Posts
    343
    Just a little tip...

    In your member-function RemoveAll I would create a memberfunction that deletes a node either from the head or tail (deleteFront() or deleteEnd() ) and also support for a check to se if the list is Empty
    e.i
    Code:
    if (!isEmpty())
    {
    deleteFront();
    //deleteEnd();
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help! Placement of nodes in a Linked List
    By lostmyshadow in forum C Programming
    Replies: 6
    Last Post: 12-17-2007, 01:21 PM
  2. Pleas take a look & give a critique
    By sh3rpa in forum C++ Programming
    Replies: 14
    Last Post: 10-19-2007, 10:01 PM
  3. singly linked circular list
    By DarkDot in forum C++ Programming
    Replies: 0
    Last Post: 04-24-2007, 08:55 PM
  4. Replies: 6
    Last Post: 03-02-2005, 02:45 AM
  5. How can I traverse a huffman tree
    By carrja99 in forum C++ Programming
    Replies: 3
    Last Post: 04-28-2003, 05:46 PM