Double linked list in c++ implementation

Display all Nodes of the Double Linked List loaded in memory

The following function traverses all the nodes from the first node to the last node and displays the data item of each node

void display() //fn displays all the nodes in the list
{
	NODE *temp;
	temp=first;
	cout<<"\n\n\n";
	while(temp)
	{
		cout<<endl<<"\t\t\t"<<temp->data;
		temp=temp->next;
	}
	cout<<"\n\n\t\t "<<nodecount<<" records displayed ,Press a key to continue .....";getch();
}

Delete all the nodes in the double linked list

The following function traverses all nodes of the double linked list from the first node to the last node and deletes each node

void deleteall()//fn deletes all the nodes in the list,sets global ptrs to
					  // to NULL
{
	int count=nodecount;
	cout<<"\n\n\t\t"<<count<<" existing records will be deleted ! (y/n) " ;char choice=getch();
	if((choice=='Y'|| choice=='y'))
	{

        NODE *temp;
        temp=first;

        while(temp)
        {
            first=first->next;
            delete temp;nodecount--;
            temp=first;
        }
        first=last=NULL;
        nodecount=0;
        cout<<"\n\n\t\t"<<count<<" records deleted. Press a key to continue ..." ;getch();
	}



}

Write the linked list in memory to a file

The function below writes the complete double linked list in memory to a file list.dlsin the current directory from where the executable file is run.

void save() // This saves the list in memory to a file list.dls in the
				//current directory of the executable.it is to be noted that
				//at  the end of file NULL or 0 is stored,by default.
				//This had created a bug while trying to read the file
				// see loadlist().
{
	fstream listfile;
	listfile.open("list.dls",ios::out) ;
	NODE *temp;
	temp=first;
	while(temp!=NULL)
	{
		listfile.write((char*)temp,sizeof(int));
		temp=temp->next;
	}
        listfile.close();
	cout<<"\n\n\n\t\tLink list has been saved in list.dls in current folder.";
	cout<<"\n\n\t\tPress a key to continue ... ";getch();

}


Continue reading the remaining 2 pages of the article through page links below ....

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.