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.
here is the program output---
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
~~~~~~~~~~~~~~~ ~~~~
the numbers shift to the left as u can see from the program output
Code:
#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
~~~~~~~~~~~~~~~ ~~~~
the numbers shift to the left as u can see from the program output
Comment