Thread: Creating three nodes, deleting head

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

    Creating three nodes, deleting head

    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);
    
    
    int main(){
    	node *list = new node;
    	node *first = list;           //this pointer always points to the head node
    	node *current = list;         //current node...?
    	
    	list -> next = NULL;
    	
    	add_node(list, 1);
    	add_node(list, 2);
    	add_node(list, 3);
    	
    	//output_list(node* list);
    	return 0;
    }
    
    
    void add_node(node *list, int num){
    	node *temp;
    
    	temp -> data = num;
    	temp -> next = list -> next;
    	list -> next = temp;
    	
    	cout << list -> data << endl;
    	getch();
    } //end void add_node

    I have tried everything and nothing works. How can I make three nodes, first off? This won't work

  2. #2
    i want wookie cookies the Wookie's Avatar
    Join Date
    Oct 2002
    Posts
    455
    when you add a node, you need a while loop to find an emtpy spot.

    the way you have it now, itll go:

    head
    next = 1


    now you add 2

    head
    next = 2

    and then 3

    head
    next = 2


    instead of:

    Code:
    head
      next = 2
                 next = 3
                            next = 4
    get it?

  3. #3
    Registered User
    Join Date
    Oct 2002
    Posts
    155
    ........... what?

  4. #4
    i want wookie cookies the Wookie's Avatar
    Join Date
    Oct 2002
    Posts
    455
    the way you designed the list is flawed, heres an example (sorry if theres errors)

    Code:
    stuct node {
      int data;
      node *next;
    }
    
    void addnode(int data);
    node *first;
    
    void main (){
       //add 3 nodes
    
       first->next=NULL;
       addnode(1);
       addnode(2);
       addnode(3);
    }
    
    void addnode(int data){
       node*current;
       node *add;
       add->data = data;
       add->next = NULL;
    
       current=first;
       while(current->next!=NULL){
             current=current->next;
       }
    
       current=add;
    }

  5. #5
    Registered User
    Join Date
    Oct 2002
    Posts
    155
    no worky

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. xor linked list
    By adramalech in forum C Programming
    Replies: 23
    Last Post: 10-14-2008, 10:13 AM
  2. Profiler Valgrind
    By afflictedd2 in forum C++ Programming
    Replies: 4
    Last Post: 07-18-2008, 09:38 AM
  3. Programmatically creating nodes in a treeview problem
    By DanFraser in forum C# Programming
    Replies: 2
    Last Post: 10-12-2006, 02:41 PM
  4. Can't figure out problem with code
    By Beast() in forum C Programming
    Replies: 4
    Last Post: 04-16-2005, 05:27 PM
  5. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM