Free Web Hosting by Netfirms
Web Hosting by Netfirms | Free Domain Names by Netfirms

Eighth Quiz




Test 1
Name: LinkyTime: 1 hr
Create a linked list using classes, and save the data you put into the lists to a text file.
Hint: Create a linked list exactly as before, then swap the struct for the class, put data into the private part of the class, create the next and prev nodes in the public part of the class.



Answer
Answer below - drag the cursor over the white space to highlight the answer
Test 1Linky Program , Source code
 
#include <conio.h>
#include <iostream.h>
#include <fstream.h>

class My_class
{
private:
	int data;
public:
	void Set_data(int value)
	{
		data=value;
	}
	int Get_data()
	{
		return data;
	}
	My_class *next,*prev;
};

void main()
{
	My_class *Pointer;
	Pointer = new My_class;		//1st list
	Pointer->prev=0;
	Pointer->Set_data(5);
	Pointer->next=new My_class;	//2nd list
	Pointer->next->prev=Pointer;
	Pointer=Pointer->next;
	Pointer->Set_data(10);
	Pointer->next=new My_class;	//3rd list
	Pointer->next->prev=Pointer;
	Pointer=Pointer->next;
	Pointer->Set_data(15);
	Pointer->next=new My_class;	//4th list
	Pointer->next->prev=Pointer;
	Pointer=Pointer->next;
	Pointer->Set_data(20);
	Pointer->next=0;
	ofstream Data("Data.txt");
	while(Pointer->prev!=0)
	{
		Data << Pointer->Get_data() << endl;
		Pointer=Pointer->prev;
		delete Pointer->next;
	}
	Data << Pointer->Get_data();
	delete Pointer;
	Data.close();
	cout << "Press any key to continue.";
	getch();
}


Back Home