Circular Linked List

Pages: 12
Would anybody know why this isnt working?
Only error I keep getting is LNK2019
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
void print(circularLinkedList &head){
		if(head = NULL){
			return;
		};
		Node<T>* current = head;
		current = current->next;
		while(current != head);
		
		cout<<current->data<<endl;
	}
    
};
template<class T>
void insert(T &item);


template<class T>
int main(){
	int number = 20;
	insert(T number);
	
	


	system("pause");
};
LNK is for linking error
Where is your void insert(T& item); implementation and where is the decleration?
If they are in 2 different files you have to include the second file in the first file after the function is declared. You don't have to include the *.h file in a *.tpp file if only *.tpp functions are in the .tpp file

so you should have something like this:

insert
header.h
1
2
3
template<class T>
void insert(T &item);
#include "template.tpp" // after decleration 

template.tpp
1
2
template<class T>
void insert(T &item) {}


Also, your print function just prints the data of the head.
And a reference cannot be 0
1
2
3
4
5
6
7
8
9
void print(circularLinkedList &head){

// a reference cannot be 0	
//if(head = NULL){
//  return;
//};

    std::cout << head.data << std::endl;
}
Last edited on
Here is the whole file. Everything is on just one file.

What do you mean by the print function.
1
2
3
4
5
6
#include <iostream>
using namespace std;

template <class T>

Last edited on
plus how would I test run both using the two functions. thanks!!
before main there is this:

1
2
template<class T>
void insert(T item);

That thing is causing the problem.
Where is the implementation?

furthermore, int main() is not a template function ... so you should not declare it like that.
int main has 2 commonly used declerations, use one of them please.
int main(int argc, char** argv) { ... } int main() { ... }

and why is there a T in your call: insert(T number);
Last edited on
The implementation is in the public circular class list. Should I defined it as a friend?
Nah, you shouldn't define it outside of the class :)
just delete that ;)

try that:
1
2
3
4
5
6
7
8
9
10
11
12
13
int main(void)
{
  circularLinkedList<int> list;
  list.insert(5);
  list.insert(4);
  list.insert(3);
  list.insert(2);
  list.insert(1);
  list.insert(0);
  list.print();

  return 0;
}

Last edited on
I keep getting error 2440 where it says '=':cannot convert from 'Node<T>' to 'Node*'
Never mind I'm just having problems with the print functions. I forgot to add<T> to my some of my pointers so thats why it was giving me those errors
So ... does it work now?
What did you change?

your print should looks something like this:
1
2
3
4
5
6
7
8
9
10
11
12
void print()
{
    Node<T>* temp = head;
    
    if(temp != nullptr)
    {
        do {
        std::cout << temp->data << ' '; // print
        } while(temp=temp->next != head);
        std::cout << std::endl;
    }
}
here is my print function. It builds but doesnt run properly.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void print(){
		
		Node<T>* current = head;
		if(head = NULL){
			return;
		};
		
		
		while(current != head);
		
		cout<<current->data<<endl;
		current = current->next;
	}

let me guess: it just prints the data of the head?
Doesnt print anything actually.
what do you have in main?

At the moment it should print the data of head
I just have this so far.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int main(){
	circularLinkedList<int> list;
	list.insert(20);
	list.insert(15);
	list.insert(17);
	list.insert(16);
	list.insert(18);

	list.print();
	


	system("pause");
};
then it seems like your insert method doesn't work properly.

Also, your print method does Node<T>* current = head;
and imediately after that you have while(current != head); which is false in the first iteration

furthermore: this while loop does nothing because of the semicolon after it
while(current != head);

1
2
3
4
5
6
7
8
9
10
11
12
void print()
{
  if(head == NULL)
    return;
  
  Node<T>* current = head;
  do
  {
    cout<<current->data<<endl;
    current = current->next;
  } while(current != head);
}
Last edited on
In print, one should remember that == is comparison and = is assignment.
In print, one should remember that == is comparison and = is assignment.
thanks for that

yeah, one should remember that...
copy/pasting other code that may not work is not a good idea ...
I didnt copy and paste this code. I had someone help me with it. Took me about 4 days and counting to understand it. I still have to do the delete function and search function so I dont want to hear anymore accusations
Anyways I fixed the minor errors such as the comparisons and the brackets and added one more line. The only problem Im having now is not getting the first the number in the list.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void print(){
		
		Node<T>* current = head;
		if(head == NULL){
			return;
		};
		
		current = current->next;
		while(current != head){
		
		cout<<current->data<<endl;
		current = current->next;
		};
	}



the main

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int main(){
	circularLinkedList<int> list;
	list.insert(20);
	list.insert(15);
	list.insert(17);
	list.insert(16);
	list.insert(18);

	list.print();
	


	system("pause");
};
Pages: 12