traversing a linked list


I'm trying to loop through a linked list with my for loop but it wont terminate: any help?
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
// linked list gba.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
enum EKEYSTATE
{
	a,
	b,
	c,
	d,
	e

};
//BUFFER FOR CURRENT KEYS
//Purpose: to store information about the keys in the current buffer
struct SKey_buffer
{
	volatile unsigned char vuc_KEYSTATE;
	SKey_buffer * next;
	bool bHead;
};

    SKey_buffer _buff6;  
	SKey_buffer _buff5;  
	SKey_buffer _buff4;  
	SKey_buffer _buff3;  
	SKey_buffer _buff2;  
	SKey_buffer _buff1;  

	SKey_buffer* iter;//iter head
	SKey_buffer * iter_Head;//temp storage for iter head
	SKey_buffer* iter_newNULL; //initialised to current null	

	void BUFFER_keysinit();
	void  BUFFER_newKeys(volatile unsigned char a);


int _tmain(int argc, _TCHAR* argv[])
{
	BUFFER_keysinit();

	volatile unsigned char a = 'A';
	
	int i = 0;
	while (i < 200)
	{
		BUFFER_newKeys(a);
		a++;

		std::cout<<iter->vuc_KEYSTATE<<std::endl;
		i++;
	}
	return 0;
}

//initialise key input buffer
void BUFFER_keysinit()
{
	 _buff6.next = NULL;    _buff6.bHead = false; 
	 _buff5.next = &_buff6; _buff5.bHead = false; 
	 _buff4.next = &_buff5; _buff4.bHead = false; 
	 _buff3.next = &_buff4; _buff3.bHead = false; 
	 _buff2.next = &_buff3; _buff2.bHead = false; 
	 _buff1.next = &_buff2; _buff1.bHead = true; 

	
	iter_Head = &_buff1;//head at initialisation
	iter = iter_Head; //head at start of traversing
	iter_newNULL = &_buff6; //initialised to current null

}

void BUFFER_newKeys(volatile unsigned char a)
{
	///////////////////////////////////////////////////////////////////////////////
//section continuously executed
	//Locate current NULL
	for (iter;iter->next != NULL;iter = iter->next)
	{
		//This will be the last iterrator prior the last,
		//NULL with wont be executed
		iter_newNULL->next = iter;
	}

	//iter == null
	if (iter->next == NULL)
	{
		//current NULL, = current state
		iter->vuc_KEYSTATE = a;

		//current NULL iter = New head
		//point to old head: set old head to false
		iter_Head->bHead = false;
		iter->next = iter_Head;
		iter->bHead = true;

		//NEW iter_head = "current NULL"
		iter_Head = iter;

		//active new null to null
		iter_newNULL->next = NULL;
	}
///////////////////////////////////////////////////////////////////////////////

}
Last edited on
Instead of a for loop I usually use a while loop, always works for me

1
2
3
4
5
instanceofiter = iter;

while(instanceofiter->next != NULL){
    instanceofiter = instanceofiter ->next;
}
Last edited on
after the while loop adjustments, its still doesn't work, now the new while loop wont terminate

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
// linked list gba.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
enum EKEYSTATE
{
	a,
	b,
	c,
	d,
	e

};
//BUFFER FOR CURRENT KEYS
//Purpose: to store information about the keys in the current buffer
struct SKey_buffer
{
	volatile unsigned char vuc_KEYSTATE;
	SKey_buffer * next;
	bool bHead;
};

    SKey_buffer _buff6;  
	SKey_buffer _buff5;  
	SKey_buffer _buff4;  
	SKey_buffer _buff3;  
	SKey_buffer _buff2;  
	SKey_buffer _buff1;  

	SKey_buffer* iter;//iter head
	SKey_buffer * iter_Head;//temp storage for iter head
	SKey_buffer* iter_newNULL; //initialised to current null	

	void BUFFER_keysinit();
	void  BUFFER_newKeys(volatile unsigned char a);


int _tmain(int argc, _TCHAR* argv[])
{
	BUFFER_keysinit();

	volatile unsigned char a = 'A';
	
	int i = 0;
	while (i < 200)
	{
		BUFFER_newKeys(a);
		a++;

		std::cout<<iter->vuc_KEYSTATE<<std::endl;
		i++;
	}
	return 0;
}

//initialise key input buffer
void BUFFER_keysinit()
{
	 _buff6.next = NULL;    _buff6.bHead = false; 
	 _buff5.next = &_buff6; _buff5.bHead = false; 
	 _buff4.next = &_buff5; _buff4.bHead = false; 
	 _buff3.next = &_buff4; _buff3.bHead = false; 
	 _buff2.next = &_buff3; _buff2.bHead = false; 
	 _buff1.next = &_buff2; _buff1.bHead = true; 

	
	iter_Head = &_buff1;//head at initialisation
	iter = iter_Head; //head at start of traversing
	iter_newNULL = &_buff6; //initialised to current null

}

void BUFFER_newKeys(volatile unsigned char a)
{
	///////////////////////////////////////////////////////////////////////////////
//section continuously executed
	//Locate current NULL
	while (iter->next != NULL)
	{
		//This will be the last iterrator prior the last,
		//NULL with wont be executed
		iter_newNULL->next = iter;
		iter = iter->next;
	}

	//iter == null
	if (iter->next == NULL)
	{
		//current NULL, = current state
		iter->vuc_KEYSTATE = a;

		//current NULL iter = New head
		//point to old head: set old head to false
		iter_Head->bHead = false;
		iter->next = iter_Head;
		iter->bHead = true;

		//NEW iter_head = "current NULL"
		iter_Head = iter;

		//active new null to null
		iter_newNULL->next = NULL;
	}
///////////////////////////////////////////////////////////////////////////////

}


 
Last edited on
Topic archived. No new replies allowed.