Linked List with Filing

this is my linked list code.
i want to use this with filing.
how can i use linked list with filings.
plz help soon.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#include<iostream>
using namespace std;

class List
{
	int data;
	int size;
	List *next;
	List *head;

public:

	List():next(NULL),head(NULL),size(NULL){}

	bool IsEmpty();
	int Size();
	void Insert();
	void Remove();
	void Display();
};

bool List::IsEmpty()
{
	if(head==NULL)
		return true;
	return false;
}

int List::Size()
{
	if(List::IsEmpty())
	return NULL;
	return size;
}

void List::Insert()
{
	List *nn = new List();
	int num;
	List *temp;

	cout<<"Enter Number = ";
	cin>>num;

	if(List::IsEmpty())
	{
		nn->data = num;
		nn->next = NULL;
		head = nn;
		size++;
		return;
	}

	temp = head;

	while(temp->next!=NULL)
	{
		temp = temp->next;
	}

	nn->data = num;
	nn->next = NULL;
	temp->next = nn;
	size++;
}

void List::Remove()
{
	if(List::IsEmpty())
	{
		cout<<"Linked List is Empty..."<<endl;
		return;
	}

	List *pre,*cur;
	pre = head;
	cur = head->next;

	if(pre->next==NULL)
	{
		head = NULL;
		return;
	}

	while(cur->next!=NULL)
	{
		pre = cur;
		cur = cur->next;
	}

	delete cur;
	pre->next = NULL;
}

void List::Display()
{
	if(List::IsEmpty())
	{
		cout<<"Linked List is Empty..."<<endl;
		return;
	}

	List *temp;
	temp = head;

	while(temp!=NULL)
	{
		cout<<temp->data<<endl;
		temp = temp->next;
	}
}

void main()
{
	List l;
	l.Insert();
	cout<<endl;
	l.Insert();
	cout<<endl;
	l.Display();
	cout<<endl;
	l.Remove();
	l.Display();
	system("pause");
}

I'm not sure what you mean, can you explain further?

Do you realise that your Insert() gets the size wrong on the first call?
i mean that when i insert number,the number save in a file.
You need to open an file for output (an ofstream).

Then you traverse the linked list, writing each node to the file. You already do this in Display(), but write to stdout. Just replace std:cout with the instance of ofstream.
i don't understand.kindly write some portion of code.
1
2
3
4
5
6
7
8
9
void List::Save(string filename)
{
	ofstream os(filename.c_str());

	for (List *temp = head; temp; temp = temp->next)
	{
		os << temp->data << endl;
	}
}
void List::Save(string filename)
{
ofstream os(filename.c_str());

for (List *temp = head; temp; temp = temp->next)
{
os << temp->data << endl;
}
}


this code is for writing in the file how to read file.???
tell me how to read from file using linked list.give me code for this.
Topic archived. No new replies allowed.