Problem with nodes

This is a discussion on Problem with nodes within the C Programming forums, part of the General Programming Boards category; Good day guys! I am an IT student currently learning linked list. I have a problem with my code here. ...

  1. #1
    Registered User
    Join Date
    Feb 2013
    Posts
    2

    Question Problem with nodes

    Good day guys!
    I am an IT student currently learning linked list. I have a problem with my code here. After I call addFront() my list doesn't change when I display it. How do I somewhat change/fill my list without changing the function type? I know it works on pointers still messed up with linked list.




    list.h

    Code:
    typedef struct node *nodeptr; 
    
    struct node{
    int item; nodeptr next;
    }; typedef nodeptr List; void addFront(List head, int item); void display(List list);


    list.c
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include"list.h"
    
    void addFront(List head, int item){
    List temp; temp = (List)malloc(sizeof(List)); temp->item = item; temp->next=head; head=temp;
    } void display(List list){
    List ptr=list; while(ptr!=NULL){
    printf("%d ", ptr->item); ptr=ptr->next;
    }
    }
    main.c
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include"list.h"
    
    int main(){
    List head=NULL; addFront(head, 5); display(head);
    }
    Last edited by Clyde Sanchez; 02-13-2013 at 04:08 AM.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,590
    > head=temp;
    You're modifying head, but main doesn't see that happen.

    Try
    List addFront(List head, int item)
    where you return the (possibly new) head each time you change a list in some way.

    And you do this in main
    head = addFront(head, 5);
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Feb 2013
    Posts
    2
    actually this problem was our seatwork earlier. Our teacher gave us void addFront(List head, int item) and said not to change it. I actually did that too. Wonder if it's even possible with void.
    Last edited by Clyde Sanchez; 02-13-2013 at 04:42 AM.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,590
    Your only other option in C is make head a pointer,

    void addFront(List *head, int item)

    Which is called with
    addFront(&head, 5);

    If you can't do either of those things, you're stuck (without using f-ugly globals).
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem transferring nodes between linked lists
    By NotAProgrammer in forum C Programming
    Replies: 1
    Last Post: 04-07-2012, 03:38 PM
  2. Problem with Nodes, Ancestry!
    By Ervilha in forum C Programming
    Replies: 1
    Last Post: 04-14-2010, 10:05 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. Logic problem adding nodes to linked list
    By SeanMSimonsen in forum C++ Programming
    Replies: 0
    Last Post: 04-02-2003, 06:04 PM
  5. Help w/nodes
    By Unregistered in forum C++ Programming
    Replies: 4
    Last Post: 04-23-2002, 08:09 PM

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