-1

I am unable to get my linked list to print out after finishing the code. I am just learning pointers so that is where the problem is most likely. I am also not sure if I used the correct type of loop to do this.

#include <stdio.h>
#include <stdlib.h>

typedef struct node {
    int fld1;
    char fld2;
    struct node *next;
} node;

int main(void) {
    struct head {
        struct node *first;
        int count;
    } head;

    int x;
    char y;
    node *newptr;
    node *currentptr;
    node *previousptr;

    head.first = NULL;
    head.count = 0;
    int zz = 1;
    int i;

    do {
        printf("Enter a value between 1 and 10: ");
        scanf(" %i", &x);
        printf("Enter a letter: ");
        scanf(" %c", &y);
        printf("in");

        newptr = (node*)malloc(sizeof(node));
        newptr->fld1=x;
        newptr->fld2=y;

        if (head.first == NULL) {
            head.first = newptr;
            head.count++;
            newptr-> next == NULL;
        } else {
            currentptr = head.first;
            if (currentptr->next == NULL) {
                head.count++;
                currentptr->next = newptr;
                newptr->next = NULL;
            } else {
                currentptr = currentptr->next;
                int i;
                for (i = 1; i < head.count; i++){
                    if (currentptr->next == NULL) {
                        currentptr->next = newptr;
                    } else {
                        currentptr = currentptr->next;
                    }
                    head.count++;
                }
            }
            printf("%d\n %c\n", head.count, newptr);
        }
    } while (x != 99);
}
Jens
  • 69,818
  • 15
  • 125
  • 179

2 Answers2

0

looks like you need to debug your code. Couple things I found so far.

line 39 newptr-> next == NULL should be newptr->next = NULL
Using double equals compares the two values, it looks like you need to assign Null to newptr->next

line 64 printf("%d\n %c\n", head.count, newptr); you cant just print newptr because it is just the location of your "node". What you want is to print is the node data members.

newptr->fld1 (your int value)

or

newptr->fld2 (your char value)

also what is the purpose of line 30 printf("in"); That just prints "in" after you accept your input. If you keep line 30, i recommend adding a newline characterprintf("in\n"); because it messes up your output format when you print your node later on.

After making the fixes I mentioned above, your code does actually print "something" as you make new nodes. I think you're struggling on line 64 when you try to print your node. Fix that and I think you'll be doing better.

Brian W
  • 153
  • 1
  • 10
  • After doing all the changes it prints once for the last thing I entered. I am going to try to debug the rest of the code. – Noah Scharmer Oct 12 '17 at 18:33
  • yes, your code just prints the most newly created node. If you want to print the entire list every time you add a new node, you'll need a loop to iterate through. Try something like this `Node *temp = head; while(temp != NULL){ printf("%d %c",temp->fld1,temp->fld2); temp = temp->next; }` – Brian W Oct 12 '17 at 18:38
0

Please see your mistakes in below code under comment section. Use below corrected code and enjoy:-

#include <stdlib.h>

typedef struct node {
int fld1;
char fld2;
struct node *next;

}node;

int main(void) {
struct head {
    struct node *first;
    int count;
}   head;
int x;
char y;
node *newptr = NULL;
node *currentptr = NULL;
/* node *previousptr; *//* this pointer is not required*/
head.first = NULL;
head.count = 0;
int zz = 1;
do {
    printf("Enter a value between 1 and 10: ");
    scanf(" %d", &x);
    printf("Enter a letter: ");
    scanf(" %c", &y);
    printf("in");

    newptr = (node*)malloc(sizeof(node));
    newptr->fld1 = x;
    newptr->fld2 = y;
    newptr->next = NULL;/* assign it only in one place*/

    if (head.first == NULL) {
        head.first = newptr;
        head.count++;
        currentptr = head.first;
        /* newptr->next = NULL; */ /* it should be single assignment */
    }
    else {
        /* currentptr = head.first; */ /* you need this statement inside above if condition because currentptr should alwasy pointing to the latest node*/
        if (currentptr->next == NULL) {
            head.count++;
            currentptr->next = newptr;
            /* newptr->next = NULL; */
        }
        else {
            currentptr = currentptr->next; /* every iteration this pointer will pointing to the last node*/
            /* int i; */
            /* for (i = 1; i<head.count; i++) {*/ /* why you need this for loop here??? */
                if (currentptr->next == NULL) {/* is last node next is NULL - yes true, it will always true*/
                    currentptr->next = newptr;
                }
                else {
                    currentptr = currentptr->next;/* this statement will never execute why because currentptr alwasy pointing to the last node */
                }
                head.count++;
            }
        }
        /* printf("%d\n %c\n", head.count, newptr); */ /* what you want to print here just think */
} while (x != 99); 
/* your list has been created */
/* now you need to print your list here */
currentptr = head.first; /* first pointing to the first node*/
while (currentptr != NULL) {/*iterate the list till last node and print value of each node */
    printf("fdl1 =%d  and fdl2 =%c\n", currentptr->fld1, currentptr->fld2);
    currentptr = currentptr->next;
}

}
Abhijit Pritam Dutta
  • 5,521
  • 2
  • 11
  • 17