Thread: Problem with nodes

  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 05:08 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > 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.

  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 05:42 AM.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    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.

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, 07:04 PM
  5. Help w/nodes
    By Unregistered in forum C++ Programming
    Replies: 4
    Last Post: 04-23-2002, 08:09 PM