Queue Linked List

Hi I am doing this project working with linked lists ,however everything works fine until I try my Clear() function which is suppose to clear the queue and de allocate the pointers. However when I do that I get access violation error. Keep in mind that my test harness is a simple test to see if Clear() function works
here is my code in the header file
Take a look at my Clear() function
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#ifndef QUEUE_H
#define QUEUE_H

#include<iostream>
#include<cstdlib>
#include<iomanip>



template< typename T >
class Queue
{
public:
	~Queue();
	Queue();
	 Queue(const Queue&);

	void Push(const T& t);
	T Pop();
	bool Empty() const;
	
	T& Front() ;
	size_t Size() const;
	void Clear();
	void Display(std::ostream& os ,char ofc = '\0')const;
	Queue& operator = (const Queue&);
private:
	class Link
	{
		Link ( const T& t) :element_(t),nextLink_(0){}
		T element_ ;
		Link * nextLink_ ;
		friend class Queue<T> ;
	
	};
	Link * firstLink_ ;
	Link * lastLink_ ;
	void         Copy   (const Queue<T>& q);

};
template< typename T >                                                               
Queue<T>::~Queue()                                                                   
{                                                                                    
                                                                                     
  if(!Empty())                                                                       
  {                                                                                  
    Link * currentLink = firstLink_;                                                 
    Link * tempLink;                                                                 
    while(currentLink != 0)                                                          
    {                                                                                
      tempLink = currentLink;                                                        
    currentLink = currentLink ->nextLink_;                                           
    delete tempLink;                                                                 
    }                                                                                
  }                                                                                  
                                                                                     
                                                                                     
}
template<typename T>
Queue<T>::Queue(const Queue& q)                                                                                                                                                   
	:firstLink_(0),                                                                                                                                                      
	lastLink_(0)                                                                                                                                                         
{                                                                                                                                                                                 
                                                                                                                                                                                  
Copy(q);                                                                                                                                                                      
} 
template<typename T>
Queue<T>::Queue()
	:firstLink_(0),
	lastLink_(0)
{}

template<typename T>
void Queue<T>::Push(const T& t)
{
	Link * newLink = new Link(t);
	if(firstLink_ == 0 && lastLink_ ==0)
	{
		firstLink_ = lastLink_ = newLink;
	}
	else
lastLink_ -> nextLink_= newLink;
 lastLink_ = newLink;
	
}

template<typename T>
void Queue<T>::Display(std::ostream& os, char ofc ) const
{

Link * currentLink = firstLink_;
os<<"Queue size: " <<Size();


os<<"Queue content: ";
while(currentLink != 0)
{

	os<<currentLink ->element_;

	currentLink = currentLink ->nextLink_;
}
}
template< typename T >
T Queue<T>::Pop()
{
	

	T removedElement;

Link * temp = firstLink_;


if(firstLink_ == lastLink_)
{
	firstLink_ = lastLink_ = 0;
}

firstLink_ = firstLink_ -> nextLink_;

removedElement = temp -> element_;
delete temp;
return removedElement;

}
template< typename T >
T& Queue<T>::Front() 
{
	return firstLink_ ->element_;
}


template< typename T >
size_t Queue<T>::Size() const
{
T size = 0;

Link * currentLink = firstLink_;

while(currentLink != 0)
{
	
	currentLink = currentLink->nextLink_;
	size++;
}
return size;
}
template<typename T>
bool Queue<T>::Empty() const
{
	return firstLink_ == 0;
}

template< typename T >
void Queue<T>::Clear()
{

	Link * currentLink = firstLink_ ;                                              
    Link * tempLink;                                                                 
         
	while(currentLink)
	{

	tempLink = currentLink;
	currentLink = currentLink ->nextLink_;
    delete tempLink; 

	}
	
}

template<typename T >
Queue<T>& Queue<T>::operator = (const Queue& q)
	
{
 if(this != &q)
 {
	Clear();
Copy(q);

 }
	return *this;
	

}

template < typename T > 
  void Queue<T>::Copy (const Queue<T>& q)
  {

    if (firstLink_ != 0)
    {
      std::cerr << "** Error: Queue::Copy called by non-empty object\n";
        exit(EXIT_FAILURE);
    }

    if (q.firstLink_ == 0)
      return;

    Link* qlink = q.firstLink_;
    while (qlink != 0)
    {

      Push(qlink -> element_);
      qlink = qlink -> nextLink_;

    }
  }
#endif 


here is my main file which has a simple test
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


#include"queue.h"



int main() 
{

	Queue<int> obj;
	Queue<int>obj2;

	obj.Push(5);
	obj.Push(6);
	obj.Push(32);
	obj.Clear();
	obj.Display(std::cout);
	std::cout<<std::endl;
        obj2 = obj;
	obj.Display(std::cout);


	
}

What may be Causing the error ? Help please
I believe the problem is that you need to set firstLink_ equal to zero after you clear everything. Probably should do the same with lastLink_ as well.
Wow that sovled it ! THANK U! u are a genius !
I have a problem when I got the task from my lecture

task: Made a program about queue in Bank teller system. Teller A if deposit <=10million, teller B > 10million
the count of customer(queue) is random and the price if deposit is random too.

output: how much customers,time and total transaction at teller A, also at teller B.

I hope someone can solve my probelm, Thx
Topic archived. No new replies allowed.