Linklist Update

This is a discussion on Linklist Update within the C++ Programming forums, part of the General Programming Boards category; Code: #include <iostream> #include <conio> using namespace std; struct node{ int data; node *next; }; void add_node(node *list, int num); ...

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    155

    Linklist Update

    Code:
    #include <iostream>
    #include <conio>
    using namespace std;
    
    struct node{
    	int data;
    	node *next;
    };
    
    
    void add_node(node *list, int num);
    void output_list(node *list);
    void delete_head(node *list);
    
    
    int main(){
    	node *list = new node;
    
    	list -> data = 3;
    	list -> next = NULL;
    	
    	add_node(list, 22);
    	add_node(list, 4);
    	output_list(list);
    	delete_head(list);
    	output_list(list);
    	return 0;
    }
    
    
    void add_node(node *list, int num){           
    	node *temp = new node;
    	
    	temp -> data = num;
    	temp -> next = NULL;         
    	
    	while (list -> next != NULL)
    		list = list -> next;
    	
    	list -> next = temp;
    } //end void add_node
    
    
    void output_list(node *list){
    	node *temp;
    	int i = 1;
    		
    	temp = list;
    	
    	while (temp != NULL){
    		cout << "Node " << i++ << " contains data value " << temp -> data << endl;
    		temp = temp -> next;
    	}
    
    } //end void output_list
    
    
    void delete_head(node *list){
    	node *current;
    	current = list;
    	current = current -> next;
    	delete list;
    	list = current;
    } //end void delete_head

    It created the three nodes /values alright, but when outputting the linklist contents again after deleting the head causes problems and an infinite loop. Help?

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,655
    *Sometimes* you will have problems incrementing a parameterized pointer, as if it were const...remote, but possible. Try:




    Code:
    #include <iostream>
    #include <conio>
    using namespace std;
    
    struct node{
    	int data;
    	node *next;
    };
    
    
    void add_node(node *list, int num);
    void output_list(node *list);
    void delete_head(node *list);
    
    
    int main(){
    	node *list = NULL;
    
    	add_node(list, 3);
    	add_node(list, 22);
    	add_node(list, 4);
    	output_list(list);
    	delete_head(list);
    	output_list(list);
                    delete_list(list);
    	return 0;
    }
    
    
    void add_node(node *list, int num){           
     node *iter = list, 
           *temp = new node;
    	
     temp -> data = num;
     temp -> next = NULL;         
    	
      if(list == NULL){
       list = temp;
       return;
       }
      
      while (iter -> next != NULL)
         iter = iter -> next;
    	
     iter -> next = temp;
    } //end void add_node
    
    
    void output_list(node *list){
     node *temp = list;
     int i = 0;
     
     while (temp != NULL){
      cout << "Node " << ++i << " contains data value " << temp ->    data << endl;
     temp = temp -> next;
     }
     
     cout << i << "nodes total." << endl;
    
    } //end void output_list
    
    void delete_head(node * list){
      if(list == NULL) return;
      node * goner = list;
      list = list->next;
      delete goner;
     }
    
    
    /* edit*///...function added 
    
    void delete_list(node *list){
     node *current, *next; 
      next = current = list;
        
      while(next){
        next = current->next;
        delete current;
        current = next;
        }
    
    } //end void delete_list
    Last edited by Sebastiani; 11-16-2002 at 01:06 AM.

  3. #3
    Registered User
    Join Date
    Oct 2002
    Posts
    155
    This doesn't work either! Ahh...!

  4. #4
    Registered User
    Join Date
    Oct 2002
    Posts
    155
    Thank you very much! I now know why it wasn't working before

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Polymorphism; Update functions and accessibility
    By CaptainMaxxPow in forum C# Programming
    Replies: 2
    Last Post: 04-23-2009, 08:48 AM
  2. SQLite not performing update
    By OnionKnight in forum C Programming
    Replies: 0
    Last Post: 01-21-2009, 03:21 PM
  3. July 9 2008 MS XP Update
    By VirtualAce in forum A Brief History of Cprogramming.com
    Replies: 12
    Last Post: 07-18-2008, 05:14 AM
  4. LinkList Sorting in C
    By simly01 in forum C Programming
    Replies: 3
    Last Post: 11-25-2002, 12:21 PM
  5. Quick LinkList Help!!adding at the End
    By simly01 in forum C++ Programming
    Replies: 13
    Last Post: 07-28-2002, 07:19 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21