Linked lists and inserting nodes

the program runs without errors, but when i try to use the changenode function, if i put anything above 2 it doesn't change the correct node. here is the source code.

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
166
#include <iostream.h>
#include <fstream.h>
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <string.h>


typedef
struct node{
	int data1;
	node *p;
} nodetype;

nodetype *p = NULL;

void deleteAll(){
p=NULL;
}

void startlist(){
	nodetype *firstone = new nodetype;
	nodetype *temp;

	for(int i=0; i<10; i++){
		temp=new nodetype;
		temp->data1=random(1000);
	if(p == NULL){
		p=temp;
		firstone=temp;
			} // if
	else{
		firstone->p=temp;
		firstone=temp;
	     } // else
				} //for
	temp->p = NULL;
} // startlist



void addNode(int pos, int value){
	nodetype *n=new nodetype;
	n->data1=value;
	nodetype *temp=p;

	if(pos==1){  n->p=p; 
			p=n;   } // if

      else{
	 for(int i=0; i<pos-2; i++) temp=temp->p;
	n->p = temp->p; temp->p=n;
	} // else
	
} // addnode

void deleteNode(int pos){
	nodetype *temp=p;
	nodetype *d = new nodetype;
	if(pos==1){ 
                p=p->p;
		delete temp;
		} //if
	else{
	for(int i=0; i<pos; i++){
		d = temp;
		temp = temp->p;
	   } //for
	  d->p=temp->p;
		} //else

} // deletenode

void changeNode(int pos, int value){
	node *c=new nodetype;
	c->data1=value;
	nodetype *temp=p;
	for(int i=0; i<pos; i++){
		p = temp; temp=temp->p;            
		p->p=c; 
		c->p = temp;
			}

}
	
	
void printall(){
	nodetype *temp = p;

	if(temp==NULL){
		cout << "\n\n\tError";
			} // if
	else{
		while(temp!=NULL){
			cout << "  " << temp->data1;
			temp=temp->p;
	  } // while
			} // else

} // printall

int listsize(){
	nodetype *temp = p;
	int size=0;
	for(;;){      
		if(temp==NULL) break;
		temp=temp->p;
		size++;
		}
	cout << "\n\n\tList size = " << size;

return size;
} 

int main(){
   clrscr();
	int pos, value;
	char c1;
	cout << "\n\n\tstartlist() ";
	startlist();
	printall();
	getch();
	cout << "\n\n\t Insert Node (Y/N)? ";
	c1=getche();
	if((c1=='Y')||(c1=='y')){
	cout << "\n\tNode# = ";
	cin >> pos;
	cout << "\n\tValue = ";
	cin >> value;
        addNode(pos,value);
		}
	cout << "\n\n\t";
	printall();
	listsize();
	cout << "\n\n\t Change Value (Y/N)? ";
	c1=getche();
	if((c1=='Y')||(c1=='y')){
	cout << "\n\tNode# = ";
	cin >> pos;
	cout << "\n\tValue = ";
	cin >> value;
        changeNode(pos,value);
		}
	cout << "\n\n\t";
	printall();
	listsize();
	cout << "\n\n\t Delete Node (Y/N/ALL)? ";
	c1=getche();
	if((c1=='Y')||(c1=='y')){
	cout << "\n\tNode# = ";
	cin >> pos;
	deleteNode(pos);
		}
	else{
	((c1=='A')||(c1=='a'));
	cout << "\n\n\tDeleting Nodes";
	deleteAll();
		}
	printall();
	listsize();
	

   cout << "\n\n\tComplete";

       return 0;
} //MAIN 


startlist()   549  604  986  944  712  283  556  734  286  861

         Insert Node (Y/N)? y
        Node# = 9

        Value = 333


          549  604  986  944  712  283  556  734  333  286  861

        List size = 11

         Change Value (Y/N)? y
        Node# = 3

        Value = 333


          986  333  944  712  283  556  734  333  286  861

        List size = 10

         Delete Node (Y/N/ALL)?


the numbers shift to the left as u can see from the program output
if for your changevalue(pos, value) function, all you want to do is change the data1 value of the node at position=pos your code should look something like this:

1
2
3
4
5
6
7
8
9
10
11
void changeNode(int pos, int value)
{
	nodetype * temp;                     //declare a local nodetype pointer
	temp = p;                                 //set temp to the first node

	int i;
	for( i = 1; i < pos; i++ )            //for loop starting from 1 
             temp = temp->p;                //iterate through list

        temp->data1 = value;             //temp now points to node at pos in list, change the data
}


if you are still having trouble, try working it out on paper. It is very difficult to use pointers without some careful planning. (at least in my experience) The easiest way to do this is to just draw your list and the pointers and actually do the entire changeNode(pos, value) function by hand to see EXACTLY what needs to be done.
good luck!
Actually with Standard C++ STL, I doubt we need to code our own linked list in the real business world application anymore (unless we work for employers that sell STL for a living).

For school work then it has to be done but as backprop says, pointer manipulation and algorithm implementation are best drafted on paper for easy visualization and understanding before you transform them into C++ code.

Good luck :)
Additionally it is very unlikely that a linked list implementation that is user defined will be useful for many cases. You'll end up having to copy and paste to create specific implementations for each and every use where the STL provides something that is generic. It is also very unlikely that the creators of these implementations will have the wherewithal to have thought through all of the issues in order to provide an efficient and useful interface and implementation.
Topic archived. No new replies allowed.