Converting from java code to c++

I'm not sure if anyone knows how to code in java. But I wanted to write a c++ version of this SortedLinkList class.

I just don't know how to point values to a variable in c++. I know in java I would use a.getNext() if I wanted to set 'a' to the next value.

java SortedLinkList
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

public class SortedLinkedList<T extends Comparable<T>> {
	/** Singly linked list . */
	private Node<T> head; // head node of the list
	private long size; // number of nodes in the list

	/** Default constructor that creates an empty list */
	public SortedLinkedList() {
		head = new Node<T>(null , null);;
		size = 0;
	}

	// ... update and search methods would go here ...
	/**
	 * Adds an element to the list in the proper place as defined by 
	 * the element's compareTo
	 * @param newElement element to add to list
	 */
	public void add(T newElement) {
		Node<T> n;
		for (n = head; n.getNext() != null; n = n.getNext())
			if (newElement.compareTo(n.getNext().getElement()) < 0)
				break;
		Node<T> node = new Node<T>(newElement, n.getNext());
		n.setNext(node);

		++size;
	}
	/**
	 * Searches for element in list
	 * @param element element to search for
	 * @return true if element exists, false otherwise
	 */
	public boolean exists(T element) {
		for (Node<T> n = head.getNext(); n != null; n = n.getNext())
			if(n.getElement().compareTo(element) == 0)
				return true;
		return false;
	}

	/**
	 * Renders list as string
	 * @return string that represent the list
	 */
	public String toString() {
		String retStr = "";
		for (Node<T> n = head.getNext(); n != null; n = n.getNext())
			retStr += n.getElement() + "\n";
		return retStr;
	}

}


This is what I have so far. I have errors on line 30 and 38.

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
#include <iostream>
#include "SortedLinkedListInt.h"
using namespace std;

class SortedLinkedListInt{

private:
	struct Node
	{
		Node *next;
		int data;
	};
	Node *head;

public:
	SortedLinkedListInt() {head = NULL;}
	~SortedLinkedListInt(){}

	void add(const int value)
	{
		Node *n = new Node;
		n -> data = value;
		n -> next = head;

		head = n;
	}
	
	bool exists (int element){
		for (Node *n = head; n != NULL; n = n -> next) // how to write n.getElement() in c++
			if(strcmp(n, element) == 0) //analogous to compareTo (java)
				return true;
		return false;
	}

	string toString(){
		string retStr = "";
		for (Node *n = head; n != NULL; n = n -> next)
			retStr = retStr + n;
		return retStr;
	}
}
Extend Node like so:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
struct Node
	{
		Node *next;
		int data;
		bool compareTo(int x)
		{
		  return (data == x);
		}
		std::string toString() const
		{
		  return ...;
		}
	};
....
Then line 30: if(n->compareTo(element))
Then line 38: retStr += n->toString();

There's no secret about C++.
Topic archived. No new replies allowed.