A simple linked list program

This is a discussion on A simple linked list program within the C Programming forums, part of the General Programming Boards category; Hi, I'm new to programming with linked lists. I'm writing a program which takes in 10 characters and integers, stores ...

  1. #1
    Registered User glanzvoll's Avatar
    Join Date
    Feb 2010
    Posts
    5

    A simple linked list program

    Hi, I'm new to programming with linked lists. I'm writing a program which takes in 10 characters and integers, stores them into a linked list then prints them.

    For some reason it's not taking all the characters and integers. Only half the linked list is printed out. Please help out, thanks!

    #include <stdio.h>
    #include <stdlib.h>
    #define N 10

    struct Node {
    char c;
    int i;
    struct Node *next;
    };

    struct Node *initNode (char c, int i);

    int main () {

    int j = 0;
    char c;
    int i;

    struct Node *head = NULL;
    struct Node *curr = NULL;

    head = initNode ('\0', 0);
    curr = head;

    for (j = 0; j < N; j++) {
    printf("Please enter a character and an integer: ");
    scanf("%c\n%i", &c, &i);
    (*curr).next = initNode(c, i);
    curr = (*curr).next;
    }

    curr = head;
    printf("\n\nChar\tInt\n");

    while (curr) {
    printf("%c\t%d\n", (*curr).c, (*curr).i);
    curr = (*curr).next;
    }

    return 0;
    }

    struct Node *initNode (char c, int i) {

    struct Node *temp;
    temp = (struct Node*)malloc(sizeof(struct Node));
    (*temp).next = NULL;
    (*temp).c = c;
    (*temp).i = i;

    return temp;

    }
    Last edited by glanzvoll; 02-28-2010 at 07:12 AM. Reason: Wrong information provided.

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Your node only has room for one char.

  3. #3
    Registered User glanzvoll's Avatar
    Join Date
    Feb 2010
    Posts
    5
    I provided the wrong code, sorry. I replaced it already. I'm still facing the same problem.

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Add this line, right after the scanf() line, in your code.

    getchar();

    That will pull off the newline that is left behind after the integer is input, and stop scanf() from acting badly.

    Print out looks OK, but you're showing one "node" too many.

  5. #5
    Registered User glanzvoll's Avatar
    Join Date
    Feb 2010
    Posts
    5
    Thanks! That worked nicely. My textbook displays the 0 before the linked list in one of its examples, but how do I get rid of it?

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You'd need a counter set to zero, and incrementing inside your while loop where you print the nodes, and then:

    if(counter++)
    print the node.

    So it prints all the nodes when counter != 0.

  7. #7
    Registered User
    Join Date
    Jan 2010
    Posts
    104
    hmmm i had a similar problem in one of my linked list assignments what i did was have a pointer hold onto the second node, then free the first one :P i ran a method to do this.

  8. #8
    Registered User glanzvoll's Avatar
    Join Date
    Feb 2010
    Posts
    5
    It was just what I needed. Thank you so much for everything!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. One more linked list implementation
    By BlackOps in forum C Programming
    Replies: 17
    Last Post: 07-16-2009, 09:34 PM
  2. Linked List Not Saving Value as Int
    By bar338 in forum C Programming
    Replies: 4
    Last Post: 05-04-2009, 07:53 PM
  3. problem with structures and linked list
    By Gkitty in forum C Programming
    Replies: 6
    Last Post: 12-12-2002, 05:40 PM
  4. Linked list with two class types within template.
    By SilasP in forum C++ Programming
    Replies: 3
    Last Post: 02-09-2002, 05:13 AM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM

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