2 Replies - 709 Views - Last Post: 01 May 2013 - 08:55 PM Rate Topic: -----

#1 Jonathan Voy   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 01-May 13

Problem: how to read file from linked list

Posted 01 May 2013 - 08:35 AM

// in this program, i want to read file and delete certain node, but until this part, i don't know how to do
void deletenode (int date)
{
        ifstream fin;
        ofstream fout;
        fin.open("book");
        fout.open("new");
	ListNode * temphead = head;
	ListNode * previous = NULL;
	ListNode * tempnext = NULL;
	while (temphead->date != date && temphead != NULL)
	{
		previous = temphead;
		temphead = temphead->next;
	}
	if (temphead == NULL)
	{
		cout<<"Sorry no data found!!!"<<endl;
	}
	else if (previous == NULL)
	{
		tempnext = temphead->next;
		head = tempnext;
		delete temphead;
	}
	else
	{
		tempnext = temphead->next;
		previous->next = tempnext;
		delete temphead;
	}
}

This post has been edited by jimblumberg: 01 May 2013 - 08:42 AM
Reason for edit:: Added missing code tags. Please learn to use them properly.


Is This A Good Question/Topic? 0
  • +

Replies To: Problem: how to read file from linked list

#2 Jingle   User is offline

  • D.I.C Regular

Reputation: 9
  • View blog
  • Posts: 323
  • Joined: 20-October 07

Re: Problem: how to read file from linked list

Posted 01 May 2013 - 12:51 PM

[quote name='Jonathan Voy' date='01 May 2013 - 08:35 AM' timestamp='1367422515' post='1844960']
// in this program, i want to read file and delete certain node, but until this part, i don't know how to do

I am a little confused at what it is your asking for. Am I right in thinking that this code is good and you are asking for help with another piece that uses or sets up this code?
Was This Post Helpful? 0
  • +
  • -

#3 David W   User is offline

  • DIC supporter
  • member icon

Reputation: 298
  • View blog
  • Posts: 1,839
  • Joined: 20-September 08

Re: Problem: how to read file from linked list

Posted 01 May 2013 - 08:55 PM

View PostJonathan Voy, on 01 May 2013 - 11:35 AM, said:

// in this program, i want to read file and delete certain node, but until this part, i don't know how to do
void deletenode (int date)
{
        ifstream fin;
        ofstream fout;
        fin.open("book");
        fout.open("new");
	ListNode * temphead = head;
	ListNode * previous = NULL;
	ListNode * tempnext = NULL;
	while (temphead->date != date && temphead != NULL)
	{
		previous = temphead;
		temphead = temphead->next;
	}
	if (temphead == NULL)
	{
		cout<<"Sorry no data found!!!"<<endl;
	}
	else if (previous == NULL)
	{
		tempnext = temphead->next;
		head = tempnext;
		delete temphead;
	}
	else
	{
		tempnext = temphead->next;
		previous->next = tempnext;
		delete temphead;
	}
}


You may like to see this ...

bool deletenode( yourType val )
{
    // not used here ...

    //ifstream fin;
    //ofstream fout;
    //fin.open("book");
    //fout.open("new");

    ListNode* cur = head;
    ListNode* prev = NULL;
    bool found = false;

    while( cur ) // NOTE FIXED TEST ORDER !!! NEED TO TEST cur != NULL FIRSTLY !!!
    {
        if( cur->val == val )
        {
            found = true;
            break;
        }

        prev = cur;
        cur = cur->next;
    }


    if( found )
    {
        if( prev == NULL ) // was first node ... so
        {
            cur = cur->next;
            delete head;
            head = cur; // update head pointer
        }
        else // was NOT first node so HAS BOTH prev node and cur node
        {
            prev->next = cur->next; // skip over cur that gets deleted on next line
            delete cur;
        }
        // -- size; // if have a size member //
    }

    return found;
}

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1