Hello
I tried to delete all the Linked List
But I can not so
Is there a way to delete the all Linked List?

struct input
{
    int id; 
    struct input * next;
};
typedef struct input ie;
class input_events 
{
    typedef struct input ie;
    private:
        ie *l;
    public:
        ~input_events();
        input_events();
        void add_ie(ie *p); 
        void gener_ie();
};
input_events :: ~input_events()
{
    typedef struct input ie;
    ie * ptr;
    ie * qtr;
    ptr = l;
    qtr = l->next;
    while (ptr)
    {
        delete ptr;
        ptr = qtr;
        if (! qtr)
        {
            qtr = qtr->next;
        }
    }
}
ie * ine;

    ine = new ie;    
        ine->id = 1;
        ptr_ie->add_ie(ine);

        ine->id = 2;
        ptr_ie->add_ie(ine);

        ine->id = 3;
        ptr_ie->add_ie(ine);

        ine->id = 4;
        ptr_ie->add_ie(ine);

        ine->id = 5;
        ptr_ie->add_ie(ine);

    cout << "*************** \n";
    cout << "Input is : \n";
    cout << "*************** \n";
    ptr_ie->gener_ie();



ptr_ie->~input_events();  // Dellet All Linked list


    cout << "*************** \n";
    cout << "Input is : \n";
    cout << "*************** \n";
    ptr_ie->gener_ie();

Thank you....

I hope to give me the code to delete all the Linked List

Your code to delete linked list should look like this :

linkedList::~linkedList{
 Node* current = head;
 while(current != NULL){
   move(current,1); //move current to next node
   destroy(head); //delete head 
   head = current;
 }
}

Here's an example of working linked list. Basic functions such as add to list, clear list and print list elements are provided. Hope this helps you to understand better.

#include <iostream>

using namespace std;

struct input
{    
	int id;			// current id
	input *next;	// pointer to next input
} ;

class input_events
{
    private:
        input *head;	// pointer to head
		input *curr;	// pointer to current

    public:
        input_events();				// constructor
		void ClearLinkedList();		// clear list
        void add_ie(input *p);		// add input
        void print_ie();			// print all in list
};

// constructor
input_events::input_events()
{
	head = NULL;	// head points to NULL
	curr = NULL;	// current points to NULL
}

// add input
void input_events::add_ie(input *p)
{
	if(head == NULL)	// first in list
	{
		head = p;			// head
		curr = head;		// current points to head
	}
	else
	{
		curr->next = p;		// add to list
		curr = p;			// update current pointer
	}
}

// clear list
void input_events::ClearLinkedList()
{
    input *ptr = head;			// points to head
    input *qtr = head->next;	// points to second

	// loop through list and delete all
    while (ptr != NULL)
    {
        delete ptr;
        ptr = qtr;

		if(ptr != NULL)
        {
            qtr = ptr->next;
        }
		else
		{
			qtr = NULL;
		}
    }

	head = NULL;	// reassign head to NULL
	curr = NULL;	// reassign current to NULL
}

// print all in list
void input_events::print_ie()
{
	input *ptr = head;

	cout << "*********************" << endl;
	cout << "Input is: " << endl;

	// for each in list, print out all
	while(ptr != NULL)
	{
		cout << ptr->id << endl;
		ptr = ptr->next;			// iterate to next input in list
	}
	
	cout << "*********************" << endl;
}

int main()
{
	input_events *ptr_ie = new input_events();

	// define input 1
	input *ine = new input;
	ine->id = 1;
	ine->next = NULL;
	ptr_ie->add_ie(ine);

	// define input 2
	ine = new input;
	ine->id = 2;
	ine->next = NULL;
	ptr_ie->add_ie(ine);

	// define input 3
	ine = new input;
	ine->id = 3;
	ine->next = NULL;
	ptr_ie->add_ie(ine);

	// print out list
	ptr_ie->print_ie();

	// clear list
	ptr_ie->ClearLinkedList();

	// print out list
	ptr_ie->print_ie();

	return 0;
}

Thank you very much for your efforts

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.