Need help in "Linked list"

Hi

I don't know what's wrong with my code

my program is about linked list that take a string type and integer.
I insert some of names and numbers then i call print function but i didn't get any thing

This is my code - i hope some one can help me
regards


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
#include<iostream>
#include<string>
using namespace std;

struct  NodeType
{
	NodeType *Link;
	string Name;
	int Grade;

};

class TotalLinkedList
{
	NodeType *Pass;
	NodeType *Fail;
	int CountPass;
	int CountFail;

public:
	TotalLinkedList();
	void insert(string, int);
	void print();
};


TotalLinkedList::TotalLinkedList()
{
	CountPass=CountFail=0;
	Pass=Fail=NULL;
}


void TotalLinkedList::insert(string Na,int Gr)
{
	NodeType *Pa,*Fa;
	Pa=new NodeType;
	Fa=new NodeType;

	if(Gr>=50 && Gr<=100)
	{
		Pa->Name=Na;
		Pa->Grade=Gr;
		CountPass++;
		Pa->Link=Pass;
	}

	else
		if(Gr<50 && Gr>=0)
		{
			Fa->Grade=Gr;
			Fa->Name=Na;
			CountFail++;
			Fa->Link=Fail;
		}
	else
		cout<<"Grade is invalid ,Ican not insert"<<endl;
}

void TotalLinkedList::print()
{
	NodeType *P=Pass;
	NodeType *F=Fail;

	while(P!=0)
	{
		cout<<"Student: "<<P->Name<<"  "<<P->Grade<<endl; 
		P=P->Link;

	}

	while(F!=0)
	{
		cout<<"Student: "<<F->Name<<"  "<<F->Grade<<endl; 
		F=F->Link;

	}

	cout<<endl;
}


void main()
{	
	TotalLinkedList obj1;
	obj1.insert("Jake",90);
	obj1.insert("John",30);
	obj1.insert("TG",-5);
	obj1.insert("Kath",68);
	obj1.insert("JaJa",25);

	obj1.print();
}
I don't see what keeping two lists (pass/fail) will buy you other than added complexity.

Even if insert worked the way you wanted it to, you would still be leaking memory like a sieve. As it is, every allocation you make is leaked. If it worked correctly only half the allocations you make would be leaked. Well, that is until we come to the non-existent destructor that doesn't free any memory. I guess it all gets leaked anyway!

You never modify either the Pass or Fail members of your class so, of course, your print function reflects that.
Topic archived. No new replies allowed.