Help with Link List

closed account (jihpDjzh)
Hello posting here again and I need help.

Here are the instructions if curious, what I need help with is reading from a file and assigning to the right places so I can display it. When I run it, it comes out blank.

(You will need to use a linked list)


You would like to write a program that stores and manipulates TV program information. Your program should read TV program information from a file called TVData.dat. The file contains the name of the show, Day show is on, time shown, and approximate number of viewers


Your program should allow a user to do several things:

Print the complete list of TV shows and all related data.
Type in a TV show title and retrieve the day and time the show is on.
Find TV shows with number of viewers greater than 20000
Add a show and all related data to the list
Print the movie TV shows in order of popularity, greatest number of viewers to least.

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
126
127
128
129
130
131
132
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>

using namespace std;

class TvShow
{
private: 
	struct Show{
		string name;
		string day;
		string time;
		string views;
		Show *next;
	};
	Show *head;
public:
/*	TvShow Show(string n, string d, string t, double v)
	{
		name = n;
		day = d;
		time = t;
		views = v;
	}*/
	TvShow()
	{
		head = NULL;
	}
	void Prepend(string n, string d, string t, string v)
	{
		Show *newNode;
		Show *curr;

		newNode = new Show;
		newNode->name = n;
		newNode->day = d;
		newNode->time = t;
		newNode->views = v;
		newNode->next = NULL;

		newNode->next = head;
		head = newNode;
	}
	void Display()
	{
		Show *curr;
		curr = head;
		while (curr != NULL){
			cout << "Show: " << curr->name << endl;
			cout << "Air date: " << curr->day << endl;
			cout << "Time: " << curr->time << endl;
			cout << "Views: " << curr->views << endl;
			curr = curr->next;}
}
	void Find(string n)
	{
		Show *curr;
		bool found = false;

		curr = head;
		while (curr != NULL && (found == false)){
			if (n == curr->name){
				found = true;
				cout << "Show: " << curr->name << endl;
				cout << "Air Date: " << curr->day << endl;
				cout << "Time: " << curr->time << endl;}
			curr = curr->next;
}
		if (found == false){
			cout << "Not Found" << endl;}
	}
	void Add(string n, string d, string t, string v){
		Show *temp = new Show;

		if (!head){
			head = temp;
			return;}
		else{
			Show *last = head;
			while (temp->next != NULL)
			{
				last= last->next;
				last->next = temp;
			}
			Show *newNode = new Show;
			newNode->name = n;
			newNode->day = d;
			newNode->time = t;
			newNode->views = v;
			newNode->next = NULL;

		}
	}

};



int main()
{


	ifstream Tvfile;
	Tvfile.open("TvShow.dat");
	int size = 0;
	Tvfile >> size;

		TvShow *myshow;
		myshow = new TvShow[size];
		string tempname;
		string tempday;
		string temptime;
		string tempviews;

		for (int i = 0; i < size; i++){
			getline(Tvfile, tempname);
			Tvfile >> tempname;
			Tvfile >> temptime;
			Tvfile >> tempviews;
			myshow[i].Prepend(tempname, tempday, temptime, tempviews);
		}
		Tvfile.close();
		for (int i = 0; i < size; i++)
		{
			myshow[i].Display();
		}

	return 0;
}


File has this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
House of Cards
Wednesday
8:00pm
20278
Big Bang Theory
Tuesday
8:00pm
23574
Modern Family
Thursday
9:00pm
15546
The 100
Monday
10:00pm
13245
Scandal
Tuesday
9:00pm
16678
Last edited on
closed account (SECMoG1T)
Hi am worried this isn't a linked list it's a dynamic array you might need to change it since you already have a linked list interface implemented in your class

1
2
TvShow *myshow;
		myshow = new TvShow[size];



you need something like this
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
TvShow myshow;

std::ifstream Tvfile("TvShow.dat");
if(!Tvfile){std::cout<<"couldn't open your file\n "; return 1;}


		string tempname;
		string tempday;
		string temptime;
		string tempviews;

		while(Tvfile)
               {
			getline(Tvfile, tempname);
			getline(Tvfile,tempname);
			getline(Tvfile,temptime);
			getline(Tvfile,tempviews);
			myshow.Prepend(tempname, tempday, temptime, tempviews);
		}

that's better now however i have a strong feeling that you should delegate that work to your constructor


Note: you'll also need a destructor.
Last edited on
you can use <list>
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
// 
#include <iostream>   
#include <algorithm> 
#include <fstream>   
#include <list> 
using namespace std;

struct Show{
	string name, day, time;
	unsigned views;
	Show(string n, string d, string t, unsigned v){
		name= n; 
		day= d; 
		time= t; 
		views= v;
	}
	bool operator< (const Show &b){
		return (views < b.views);
	}
};

bool S_sorter (const Show &ob1, const Show &ob2){
	return ob1.views > ob2.views;  // descending
}

int main () {
	string n, d, t;
	unsigned v;	
	list<Show> li;
	ifstream in("input.txt");
	if(!in)	{ 
		cout << "file error!";	 
		exit(1); 
	}	

	while(getline(in, n)){		
		in >> d;
		in >> t; 
		in >> v;
		in.ignore();		
		li.emplace_back(Show(n,d,t,v));
	}	

	li.sort();  // uses "operator<" function written inside Show
	for(auto x:li) cout << x.views << " ";	
	
	cout << "\n\n";
	
	li.sort(S_sorter);  // uses S_sorter function, descending
	//std::sort(li.begin(), li.end(), &S_sorter);  
	// ^ error for list, worked for vector		
	for(auto x:li) cout << x.views << " ";	

return 0;
}


/*//input.txt: 
House of Cards
Wednesday
8:00pm
20278
Big Bang Theory
Tuesday
8:00pm
23574
Modern Family
Thursday
9:00pm
15546
The 100
Monday
10:00pm
13245
Scandal
Tuesday
9:00pm
16678
*/
closed account (jihpDjzh)
When I use the while loop, and run it. It works perfect, but now it it starts from the list bottom to top and puts the show "Scandal" with info twice. Any way you could direct to solve that? I have not changed anything to my code besides putting

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
ifstream Tvfile;
	Tvfile.open("TvShow.dat");
	

		string tempname;
		string tempday;
		string temptime;
		string tempviews;

		while(Tvfile)
		{
			getline(Tvfile, tempname);
			getline(Tvfile, tempday);
			getline(Tvfile, temptime);
			getline(Tvfile, tempviews);
			myshow.Prepend(tempname, tempday, temptime, tempviews);
		}
		Tvfile.close();
		
		myshow.Display();
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
#include <iostream>
#include <string>
#include <fstream>
using namespace std;

class TvShow{
private: 
	struct Show{
		string name, day, time, views;
		Show *next;
		Show(string n="", string d="", string t="", string v="", Show* h=NULL)
			: name(n), day(d), time(t), views(v), next(h) {}
	};
	Show *head;
	
public:

	TvShow() : head(NULL) {}

	void Prepend(string n, string d, string t, string v){
		if(!head) { 
			head = new Show(n,d,t,v,NULL);
		}
		else{
			Show *newNode = new Show(n,d,t,v, head);
			head = newNode;			
		}

	}
	
	void Display(){		
		Show *curr = head->next; ///
		while (curr){
			cout<< "Show: " << curr->name
			 	<< "\nAir date: " << curr->day
			 	<< "\nTime: " << curr->time
			 	<< "\nViews: " << curr->views;			 	
			cout<< "\n\n";
			curr = curr->next;
		}
		
	}
	
	//~TvShow();
	
};


int main(){	

	ifstream Tvfile("TvShow.txt");
	string tempname;
	string tempday;
	string temptime;
	string tempviews;	
	TvShow myshow;
	
	while(Tvfile){
		getline(Tvfile, tempname);
		getline(Tvfile, tempday);
		getline(Tvfile, temptime);
		getline(Tvfile, tempviews);
		myshow.Prepend(tempname, tempday, temptime, tempviews);
	}
	
	Tvfile.close();	
	myshow.Display();
		
return 0;
}
You should store "views" as a number.

You're getting scandal twice because it's the last one. You read it, then the while loop checks if the file still okay (it is). Then it tries to read another record and fails, leaving all the variables unchanged. You call Prepend() which inserts the unchanged variables and voila! Scandal is in the list twice.

Your while loop needs to check the state of the stream after reading everything:
1
2
3
4
5
6
7
	while(Tvfile){
		getline(Tvfile, tempname);
		getline(Tvfile, tempday);
		getline(Tvfile, temptime);
		getline(Tvfile, tempviews);
		if (Tvfile) myshow.Prepend(tempname, tempday, temptime, tempviews);
	}
while(!Tvfile.eof()) is better than while(Tvfile)
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
#include <iostream>
#include <string>
#include <fstream>
using namespace std;

struct Show{
	string name, day, time;
	int views;
	Show *next;
	Show(string n="", string d="", string t="", int v=0, Show* h=NULL)
		: name(n), day(d), time(t), views(v), next(h) {}
};
	
class TvShow{
private: 
	Show *head;
	
public:
	TvShow() : head(NULL) {}

	void Prepend(string n, string d, string t, int v){
		if(!head) { 
			head = new Show(n,d,t,v,NULL);
		}
		else{
			Show *newNode = new Show(n,d,t,v, head);
			head = newNode;			
		}

	}
	
	void Display(){		
		Show *curr = head;
		while (curr){
			cout<< "Show: " << curr->name
			 	<< "\nAir date: " << curr->day
			 	<< "\nTime: " << curr->time
			 	<< "\nViews: " << curr->views;			 	
			cout<< "\n\n";
			curr = curr->next;
		}
		
	}
	
	//~TvShow();
	
};


int main(){	

	ifstream Tvfile("TvShow.txt");
	string tempname;
	string tempday;
	string temptime;
	int tempviews;	 //
	TvShow myshow;
	
	while(!Tvfile.eof()){  ///
		getline(Tvfile, tempname);
		Tvfile>>tempday;
		Tvfile>>temptime;
		Tvfile>>tempviews;
		Tvfile.ignore(); ///
		myshow.Prepend(tempname, tempday, temptime, tempviews);		
	}
	
	Tvfile.close();	
	myshow.Display();
		
return 0;
}
Topic archived. No new replies allowed.