Linked List HELP!!

Hi! I'm making a linked list where it contains 6 elements and wherein if input a number, starting from the first element, going to the element according to the input. And then I will input again but this time, I will start from the element where I stopped from the previous roll.

So if your initial state is 0->1->2->3->4->5 and input a 4, you advance to 4.
But if the input is 3, I would circle back to the 1.

I'm basically new to data structure programming :( Help is appreciated.

Sorry for my bad english! Thank you!

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
 #include <iostream>

using namespace std;

struct Node
{
	int data;
	struct Node *next;
};

//insert a new node in an empty list
struct Node *insertInEmpty(struct Node *last, int new_data)
{
   // if last is not null then list is not empty, so return
   if (last != NULL)
   return last;
 
   // allocate memory for node
   struct Node *temp = new Node;
 
   // Assign the data.
   temp -> data = new_data;
   last = temp;
 
   // Create the link.
   last->next = last;
 
   return last;
}

//insert new node at the beginning of the list
struct Node *insertAtBegin(struct Node *last, int new_data)
{
   //if list is empty then add the node by calling insertInEmpty
   if (last == NULL)
   return insertInEmpty(last, new_data);
 
   //else create a new node
   struct Node *temp = new Node;
 
   //set new data to node
   temp -> data = new_data;
   temp -> next = last -> next;
   last -> next = temp;
 
   return last;
}


//traverse the circular linked list
void traverseList(struct Node *last) 
{
   struct Node *p;
 
   // If list is empty, return.
   if (last == NULL) 
   {
      cout << "Circular linked List is empty." << endl;
      return;
    }
    
	p = last -> next; // Point to the first Node in the list.
 
	// Traverse the list starting from first node until first node is visited again
	do 
	{
      cout << p -> data << "==>";
      p = p -> next;
    } while(p != last->next);
	
	if(p == last->next)
	cout<<p->data;
	cout<<"\n\n";
}

void pointTO(struct Node *current, int count)
{
	// CODES THAT I NEED HELP
}

int main()
{	
	int count;
	struct Node *last = NULL;
	struct Node *current = NULL;
	
	last = insertInEmpty(last, 5);
	last = insertAtBegin(last, 4);
	last = insertAtBegin(last, 3);
	last = insertAtBegin(last, 2);
	last = insertAtBegin(last, 1);
	last = insertAtBegin(last, 0);
	cout<<"The circular linked list created is as follows:"<<endl;
	traverseList(last);
	
	for(int i; i < 5; i++)
	{
		cout<<"Enter number: ";
		cin>>count;
		pointTO(current,count);
		cout<<"Current number: "<<current->data;
	}
	
	

	return 0;
}
Nvm I have done it :)))

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
#include <iostream>

using namespace std;

struct Node
{
	int data;
	struct Node *next;
};

//insert a new node in an empty list
struct Node *insertInEmpty(struct Node *last, int new_data)
{
   // if last is not null then list is not empty, so return
   if (last != NULL)
   return last;
 
   // allocate memory for node
   struct Node *temp = new Node;
 
   // Assign the data.
   temp -> data = new_data;
   last = temp;
 
   // Create the link.
   last->next = last;
 
   return last;
}

//insert new node at the beginning of the list
struct Node *insertAtBegin(struct Node *last, int new_data)
{
   //if list is empty then add the node by calling insertInEmpty
   if (last == NULL)
   return insertInEmpty(last, new_data);
 
   //else create a new node
   struct Node *temp = new Node;
 
   //set new data to node
   temp -> data = new_data;
   temp -> next = last -> next;
   last -> next = temp;
 
   return last;
}


//traverse the circular linked list
void traverseList(struct Node *last) 
{
   struct Node *p;
 
   // If list is empty, return.
   if (last == NULL) 
   {
      cout << "Circular linked List is empty." << endl;
      return;
    }
    
	p = last -> next; // Point to the first Node in the list.
 
	// Traverse the list starting from first node until first node is visited again
	do 
	{
      cout << p -> data << "==>";
      p = p -> next;
    } while(p != last->next);
	
	// last node
	if(p == last->next)
	cout<<p->data;
	cout<<"\n\n";

}

void pointTO(struct Node *last, int count)
{
	// CODES THAT I NEED HELP
	struct Node *p;
	int x = 0;
	p = last -> next;
	
	do
	{
		cout<<"Enter number: ";
		cin>>count;
		x += 1;
		
		if(count == -10)
		{
			break;
		}
		if (count == 1)
		{	
			p = p -> next;
			cout<<"Current number: "<< p->data;
		}
		else if (count == 2)
		{
			p = p -> next;
			p = p -> next;
			cout<<"Current number: "<< p->data;
		}
		else if (count == 3)
		{
			p = p -> next;
			p = p -> next;
			p = p -> next;
			cout<<"Current number: "<< p->data;
		}
		else if (count == 4)
		{
			p = p -> next;
			p = p -> next;
			p = p -> next;
			p = p -> next;
			cout<<"Current number: "<< p->data;
		}
		else if (count == 5)
		{
			p = p -> next;
			p = p -> next;
			p = p -> next;
			p = p -> next;
			p = p -> next;
			cout<<"Current number: "<< p->data;
		}
		else if (count == 6)
		{
			p = p -> next;
			p = p -> next;
			p = p -> next;
			p = p -> next;
			p = p -> next;
			p = p -> next;
			cout<<"Current number: "<< p->data;
		}
		
		cout<<endl;
		
	} while(x!=999);
		
}

int main()
{	
	int count;
	struct Node *last = NULL;
	//struct Node *current = NULL;
	
	last = insertInEmpty(last, 5);
	last = insertAtBegin(last, 4);
	last = insertAtBegin(last, 3);
	last = insertAtBegin(last, 2);
	last = insertAtBegin(last, 1);
	last = insertAtBegin(last, 0);
	cout<<"The circular linked list created is as follows:"<<endl;
	traverseList(last);
	pointTO(last,count);
	

	return 0;
}
if your initial state is 0->1->2->3->4->5 and input a 4, you advance to 4.
For this reason, you'd be better off with a vector instead of a list.
Duplicate of http://www.cplusplus.com/forum/general/263954/

EDIT: and of http://www.cplusplus.com/forum/beginner/263559/

Please don't waste people's time by spamming multiple threads for the same topic.
Last edited on
Topic archived. No new replies allowed.