Proper use of linked list

I am trying to grasp the concept of linked list and how they connect by reading in a word 'bob' ...Is this correct or do i need to create another node?:
struct nodeIt {
char letter;
int occurences;
nodeIt * next;( this points to the next pointer correct?)
};
nodeIt * fromString(string word)//<----function
{
int i = 0;

list1 = new NodeIt;//gives first list1 address?
// Does the line above create a new node for me to store each letter
iter = list1;
while (iter != NULL) {
iter->letter = word[i];//storing 1st letter

iter->next->letter = word[i + 1];//not sure why this will not work
}
return list1;
Last edited on
the struc node should contain a template T value, and a Node* next; for the list class you have two nodes head and tail. in the constructor link them : head->next = tail; make sure that tail is always null.

create two method insert and remove that take as parameter a templated T value. make another method call find(value) . that would be a good start . the concpt is the link or see it like a chain or a necklace. you dont want it broken, make sure all node ar attached, at any time , mostly when you remove an element.
sorry for the confusion the syntax is confusing me:
there are no classes here just a struct and a function..or are you saying i need to make a class?
<would you have a link that i could read, im having a hard time grasping when to use the syntax proper and what it means>

node *list1 is head? or do i need to make a node* head
list1 = new Node = middle/iterator
what is
iter=list1 than?
start with something like this :

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
template<typename T>
class List
{
	template<typename TT>
	struct Node
	{
		TT value;
		Node<TT>* next;
	};
	Node<T>* head;
	Node<T>* tail;
public:
	List();
	~List();
	void insert(T value);
	void remove(T value);
	signed int indexOf(T value);
};

template<typename T>
List<T>::List()
: head(new Node<T>)
, tail(nullptr)
{
	head->next = tail;
}

template<typename T>
List<T>::~List()
{
	if (head)
	delete head;
	if (tail)//not supposed to
		delete tail;
}

template<typename T>
void List<T>::insert(T value)
{
}

template<typename T>
void List<T>::remove(T value)
{
}
template<typename T>
signed int List<T>::indexOf(T value)
{
	auto current = head;
	auto index = 0;
	while (current->next != head)
	{
		if (current->value == value)
			return index;
		else
			current = current->next;

		index++;
	}
	return -1;
}


template class List<int>;

int main()
{
	List<int> myList;
       //do stuff here

    return 0;
}
Thanks allot,
i have a much better idea of what is going on now.
Or so i thought:\; I am getting out of range error after type in the word, compiler prompts me to abort:
#include<iostream>
#include<string>
#include<cmath>
using namespace std;

struct orderNode {
char letter;
int occurence;
orderNode* next;
};

orderNode *fromString(string word);

int main() {
orderNode*list1;
string word1, word2;
cout << " Enter first word :" << word1;
cin >> word1;
list1 = fromString(word1);
system("pause");
return 0;
}

orderNode *fromString(string word) {
int i = 0;
orderNode *list1;//head
orderNode *n;//iter
orderNode *t;//tail
n = new orderNode;
list1 = n;
t = n;
n->letter = word[0];
while (n != NULL) {
n->letter = word[i];
n=new orderNode;
t->next = n;
t = t->next;

if (list1->letter < n->letter) {
t->letter = list1->letter;
list1->letter = n->letter;
n->letter = t->letter;
}
else if (list1->letter == n->letter)
{
list1->occurence++;
n->letter = NULL;
}
list1->occurence++; i++;
}

return list1;
}
Last edited on
can you show the errors I dont have a compiler with me right now ?
error:

Debug Assertion Failed!
file names
Line: 1681

Expression: string subscript out of range

For information on how your program can cause an assertion failure, see the Visual C++ documentation on asserts
Topic archived. No new replies allowed.