By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,197 Members | 1,207 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,197 IT Pros & Developers. It's quick & easy.

Linked lists and inserting nodes (c++)

P: 25
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.

Expand|Select|Wrap|Line Numbers
  1. #include <iostream.h>
  2. #include <fstream.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <conio.h>
  6. #include <string.h>
  7.  
  8.  
  9. typedef
  10. struct node{
  11.     int data1;
  12.     node *p;
  13. } nodetype;
  14.  
  15. nodetype *p = NULL;
  16.  
  17. void deleteAll(){
  18. p=NULL;
  19. }
  20.  
  21. void startlist(){
  22.     nodetype *firstone = new nodetype;
  23.     nodetype *temp;
  24.  
  25.     for(int i=0; i<10; i++){
  26.         temp=new nodetype;
  27.         temp->data1=random(1000);
  28.     if(p == NULL){
  29.         p=temp;
  30.         firstone=temp;
  31.             } // if
  32.     else{
  33.         firstone->p=temp;
  34.         firstone=temp;
  35.          } // else
  36.                 } //for
  37.     temp->p = NULL;
  38. } // startlist
  39.  
  40.  
  41.  
  42. void addNode(int pos, int value){
  43.     nodetype *n=new nodetype;
  44.     n->data1=value;
  45.     nodetype *temp=p;
  46.  
  47.     if(pos==1){  n->p=p; 
  48.             p=n;   } // if
  49.  
  50.       else{
  51.      for(int i=0; i<pos-2; i++) temp=temp->p;
  52.     n->p = temp->p; temp->p=n;
  53.     } // else
  54.  
  55. } // addnode
  56.  
  57. void deleteNode(int pos){
  58.     nodetype *temp=p;
  59.     nodetype *d = new nodetype;
  60.     if(pos==1){ 
  61.                 p=p->p;
  62.         delete temp;
  63.         } //if
  64.     else{
  65.     for(int i=0; i<pos; i++){
  66.         d = temp;
  67.         temp = temp->p;
  68.        } //for
  69.       d->p=temp->p;
  70.         } //else
  71.  
  72. } // deletenode
  73.  
  74. void changeNode(int pos, int value){
  75.     node *c=new nodetype;
  76.     c->data1=value;
  77.     nodetype *temp=p;
  78.     for(int i=0; i<pos; i++){
  79.         p = temp; temp=temp->p;            
  80.         p->p=c; 
  81.         c->p = temp;
  82.             }
  83.  
  84. }
  85.  
  86.  
  87. void printall(){
  88.     nodetype *temp = p;
  89.  
  90.     if(temp==NULL){
  91.         cout << "\n\n\tError";
  92.             } // if
  93.     else{
  94.         while(temp!=NULL){
  95.             cout << "  " << temp->data1;
  96.             temp=temp->p;
  97.       } // while
  98.             } // else
  99.  
  100. } // printall
  101.  
  102. int listsize(){
  103.     nodetype *temp = p;
  104.     int size=0;
  105.     for(;;){      
  106.         if(temp==NULL) break;
  107.         temp=temp->p;
  108.         size++;
  109.         }
  110.     cout << "\n\n\tList size = " << size;
  111.  
  112. return size;
  113.  
  114. int main(){
  115.    clrscr();
  116.     int pos, value;
  117.     char c1;
  118.     cout << "\n\n\tstartlist() ";
  119.     startlist();
  120.     printall();
  121.     getch();
  122.     cout << "\n\n\t Insert Node (Y/N)? ";
  123.     c1=getche();
  124.     if((c1=='Y')||(c1=='y')){
  125.     cout << "\n\tNode# = ";
  126.     cin >> pos;
  127.     cout << "\n\tValue = ";
  128.     cin >> value;
  129.         addNode(pos,value);
  130.         }
  131.     cout << "\n\n\t";
  132.     printall();
  133.     listsize();
  134.     cout << "\n\n\t Change Value (Y/N)? ";
  135.     c1=getche();
  136.     if((c1=='Y')||(c1=='y')){
  137.     cout << "\n\tNode# = ";
  138.     cin >> pos;
  139.     cout << "\n\tValue = ";
  140.     cin >> value;
  141.         changeNode(pos,value);
  142.         }
  143.     cout << "\n\n\t";
  144.     printall();
  145.     listsize();
  146.     cout << "\n\n\t Delete Node (Y/N/ALL)? ";
  147.     c1=getche();
  148.     if((c1=='Y')||(c1=='y')){
  149.     cout << "\n\tNode# = ";
  150.     cin >> pos;
  151.     deleteNode(pos);
  152.         }
  153.     else{
  154.     ((c1=='A')||(c1=='a'));
  155.     cout << "\n\n\tDeleting Nodes";
  156.     deleteAll();
  157.         }
  158.     printall();
  159.     listsize();
  160.  
  161.  
  162.    cout << "\n\n\tComplete";
  163.  
  164.        return 0;
  165. } //MAIN
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
Sep 23 '10 #1
Share this Question
Share on Google+
2 Replies


weaknessforcats
Expert Mod 5K+
P: 9,197
Have you stepped through this using your debugger?
Sep 23 '10 #2

P: 25
i figured it out, i didn't need nodetype *c because it would be adding node.
Sep 23 '10 #3

Post your reply

Sign in to post your reply or Sign up for a free account.