Deleting lnked list

This is a discussion on Deleting lnked list within the C++ Programming forums, part of the General Programming Boards category; Hi I am trying to write simple linkedlist application. But i take error while trying to delete it Code: #include ...

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    61

    Deleting lnked list

    Hi
    I am trying to write simple linkedlist application. But i take error while trying to delete it
    Code:
    #include "stdafx.h"
    #include <stdlib.h>
    #include <iostream>
    using namespace std;
    
    typedef struct _Listmember
    {
    	int x;
    	_Listmember *pNext;
    }Listmember;
    
    Listmember *head = NULL;
    
    
    void Add(int x)
    {
    	Listmember *List = (Listmember *) malloc(sizeof(Listmember));
    	List->x = x;
    	List->pNext = head;
    	head = List;
    	 
    };
    void Show()
    {
    	Listmember* pmember;
    	for(pmember = head; pmember != NULL; pmember = pmember->pNext)
    		cout << pmember->x << "\n";
    };
    
    void Delete()
    {
    Listmember* pmember;
    	for(pmember = head; pmember != NULL; pmember = pmember->pNext)
    	free(pmember);
    	
    }
    
    int _tmain(int argc, _TCHAR* argv[])
    {   
    
    	Add(1);
    	Add(2);
    	Add(3);
    	Show();
    	Delete();
    	return 0;
    }
    Why is that code wrong?
    How must i delete the linkedlist?
    Thanks
    Last edited by sawer; 07-05-2007 at 02:32 AM.

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,474
    if you are writing on C++ - you should use new/delete

    If you on C - do not use <iostream> - use stdio.h with printf

    Code:
    for(pmember = head; pmember != NULL; pmember = pmember->pNext)
    	free(pmember);
    You do pmember->pNext after free - it is not safe
    Do something like
    Code:
    while(head)
    {
       Listmember *temp = head->next;
       free(head);
       head = temp;
    }
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    Registered User
    Join Date
    Jan 2006
    Posts
    61
    Hmm ok.
    Thank you vart.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. circular doubly linked list help
    By gunnerz in forum C++ Programming
    Replies: 5
    Last Post: 04-28-2007, 08:38 PM
  2. Anyone good with linked list.....I am not....
    By chadsxe in forum C++ Programming
    Replies: 11
    Last Post: 11-10-2005, 01:48 PM
  3. instantiated from here: errors...
    By advocation in forum C++ Programming
    Replies: 5
    Last Post: 03-27-2005, 08:01 AM
  4. Linked list with two class types within template.
    By SilasP in forum C++ Programming
    Replies: 3
    Last Post: 02-09-2002, 05:13 AM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21