Simple Linked List

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mircead2014
    New Member
    • Feb 2014
    • 4

    Simple Linked List

    Hi.
    I am beginner in a linked list programming and i need some advices.
    First of all, what is the diferences between :

    Code:
    typedef struct node
    {
        int data;
        struct node *next;
    };
    and

    Code:
    struct node
    {
        int data;
        struct node *next;
    }*head;
    Now, I have this problem to count the number of nodes from a linked list.
    I have passed the head of linked list every function, i dont know why but it dont works.

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<malloc.h>
    
    typedef struct node
    {
        int data;
        struct node *next;
    };
    
    int main()
    {
        struct node *head;
        Create_List(head);
        Print_List(head);
        Number_of_Key(head);
    }
    
    void Create_List(struct node *head)
    {
        int alege = 1;
        do
        {
            struct node *new_node, *current = NULL;
            head = NULL;
            new_node = (struct node *)malloc(sizeof(struct node));
            printf("\n Enter the data : ");
            scanf("%d", &new_node->data);
            if(head == NULL)
            {
                head = new_node;
                current = new_node;
            }
            else
            {
                current->next = new_node;
                current = new_node;
            }
            printf("\n Do you want to creat another element ? (Apasa \"1\" sau \"0\") : ");
            scanf("%d", &alege);
        } while(alege != 0);
    }
    
    void Print_List(struct node *head)
    {
        struct node *temp;
        temp = head;
        printf("\n The Linked List : ");
        while(temp != NULL)
        {
            printf(" %d ->", temp->data);
            temp = temp->next;
        }
        printf(" NULL");
        printf("\n");
    }
    
    void Number_of_Key(struct node *head)
    {
        struct node *current;
        int key, counter = 0;
        printf("\n Enter the key number : ");
        scanf("%d", &key);
        current = head;
        while(current != NULL)
        {
            if(current->data == key)
                counter++;
            current = current->next;
        }
        printf("\n The key number \"%d\" is in the list %d times!\n", key, counter);
    }
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    Second structure also creates a variable, head, a pointer to node.

    Your code doesn't work because in Print_List and Number_of_Key you assume that the end of the list is indicated by a NULL next pointer (which is the standard way to do it) but in Create_List you never set the next pointer to NULL for any node, and especially, not the last one.

    Comment

    • donbock
      Recognized Expert Top Contributor
      • Mar 2008
      • 2427

      #3
      Your first snippet shows an improper use of "typedef". The purpose of typedef is to define a new type that is equivalent to some old type. The syntax is
      Code:
      typedef oldtype newtype;
      In your example, everything from "struct" to "}" is the oldtype. You don't have a newtype. I'm surprised you didn't get a compiler error or warning. Your particular oldtype has the side effect of defining a struct named "node". That's why there is no difference between those first two code snippets (other than the declaration of variable "head").

      You should always check if malloc failed (return value is NULL). Line 26.

      Comment

      Working...