Thread: deleting a node in a linked list

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    83

    deleting a node in a linked list

    hi,

    in my code at the bottom, everything works perfectly except for the function to delete the pointer. i've been working on it for hours and understand the concept that the node is not "deleted" rather it's skipped. the node before the current node is linked to the next node and then the current node is deleted to save memory.

    i just can't seem to get it on code. can someone help me with the delete function? the main problem i have is that...let's say i have the current node to be deleted, how do i point to the previous node and the next node?

    i thought about searching for the node and then having a separate function to delete the node. or iterate through the node and do the linking in there and that's where i get lost with the previous/next node.

    thanks,
    barneygumble742

    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    using namespace std;
    
    struct car_node
    {
    	string make;
    	int year;
    	car_node *next;
    };
    
    typedef  car_node* car_node_pointer;
    
    void head_insert(car_node_pointer &head, string temp_make, int temp_year);
    void add(car_node_pointer after_me, string temp_make, int temp_year);
    void print(car_node_pointer &temp);
    void print_all(car_node_pointer &head);
    car_node_pointer search(car_node_pointer &head, string temp_make);
    void askuser(string &make, int &year);
    void delete_node(car_node_pointer &head, string temp_make);
    
    int main()
    {
    string make, space; int year=0; ifstream fin("cars1.txt"); ofstream fout("cars2.txt");
    car_node_pointer head = new car_node;
    car_node_pointer after_me = new car_node;
    car_node_pointer temp1 = new car_node;
    
    	after_me->next = head;
    
    	head->next=NULL;
    
    	while(!fin.eof())
    	{
    		getline(fin, make);
    		fin >> year;
    		getline(fin, space);
    
    		add(head, make, year);
    	}
    	print_all(head);
    
    	askuser(make, year);
    	delete_node(head, make);
    	print_all(head);
    
    return 0;
    }
    
    car_node_pointer search(car_node_pointer &head, string temp_make)
    {
    	car_node_pointer here = head;
    
    	if(here == NULL)
    	{
    		return NULL;
    	}
    	else
    	{
    		while(here->make != temp_make && here->next != NULL)
    			here = here->next;
    
    		if(here->make == temp_make)
    			return here;
    		else
    			return NULL;
    	}
    }
    
    void delete_node(car_node_pointer &head, string temp_make)
    {
    	car_node_pointer current = new car_node;
    
    	if(head->make == temp_make)
    	{
    		current = head;
    		head = head->next;
    	}
    	else
    	{
    		current = head;
    		while(current->make = temp_make)
    		{
    			current->next = head;
    		}
    		if(current->make = temp_make)
    		{
    			car_node_pointer discard = current;
    			current->next = discard->next;
    			delete discard;
    		}
    	}
    }

  2. #2
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    maybe I am overlooking something.. but this kinda bothers me:
    Code:
    if(head->make == temp_make)
    	{
    		current = head;
    		head = head->next;
    	}

    you are re-assigning your head pointer.. no deletion is taking place.. it seems that with every call to this function, if head->make == temp_make.. the head pointer will continously be reassigned.. as well as the current pointer.. which I think is leaving a bunch of dangling nodes... each re-assignment of 'current' is leaving the previous current with no way to be referenced.. (if you were using a double linked list.. perhaps you could iterate back from 'head' or 'current'.. but that would seem unorthodox)

    perhaps this is a viable solution:
    Code:
    if(head->make == temp_make)
    	{
    		current = head;
    		head = head->next;
                    delete current;
    	}
    Last edited by The Brain; 08-03-2005 at 07:32 PM.
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  3. #3
    Registered User
    Join Date
    Sep 2004
    Posts
    83
    the whole delete node functions is kind of messy. i was converting pseudocode.

    how should i redo it?

    thanks

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    > while(current->make = temp_make)
    I think you want:
    while(current->make == temp_make)

    > if(current->make = temp_make)
    Same here:
    if(current->make == temp_make)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pleas take a look & give a critique
    By sh3rpa in forum C++ Programming
    Replies: 14
    Last Post: 10-19-2007, 10:01 PM
  2. Reverse function for linked list
    By Brigs76 in forum C++ Programming
    Replies: 1
    Last Post: 10-25-2006, 10:01 AM
  3. Problems with deleting a node in a singly linked list
    By omishompi in forum C++ Programming
    Replies: 7
    Last Post: 02-23-2006, 06:27 PM
  4. Problem with linked list ADT and incomplete structure
    By prawntoast in forum C Programming
    Replies: 1
    Last Post: 04-30-2005, 01:29 AM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM