C Programming: Related Lists

I am writing a program using a linked list (such a nightmare).

In any case, the goal of the program is to enter 8 characters, and the program will print the characters back to you, and also print the characters in reverse order using linked lists, of course.

I still have it. There are a lot of things wrong (I think).

Problems

  • When requesting characters from the user, he should automatically read the number of characters without having to ask how many characters

  • Also, when it compiles, it prints gibberish on the screen, for example, I just ran it and printed

    ¿r
      (àõ($ê¿¿  
    a¿r
    (àõ($ê¿¿  
    
    ¿r
      (àõ($ê¿¿  
    b¿r
       (àõ($ê¿¿  
    

Great help is needed here. It would be so grateful!

Course code

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

#define strsize 30

typedef struct member
{
    int number;
    char fname[strsize];
    struct member *next;
}
RECORD;

RECORD* insert (RECORD *it);
RECORD* print(RECORD *it, int j);

int main (void)
{
    int i, result;
    RECORD *head, *p;
    head=NULL;
    printf("Enter the number of characters: ");
    scanf("%d", &result);

    for (i=1; i<=result; i++)
        head=insert (head);
    print (head, result);

    return 0;
}

RECORD* insert (RECORD *it)
{
    RECORD *cur, *q;
    int num;
    char junk;
    char first[strsize];
    printf("Enter a character:");
    scanf("%c", &first);

    cur=(RECORD *) malloc(sizeof(RECORD));

    strcpy(cur->fname, first);
    cur->next=NULL;

    if (it==NULL)
        it=cur;
    else
    {
        q=it;
        while (q->next!=NULL)
            q=q->next;
        q->next=cur;
    }
    return (it);
}

RECORD* print(RECORD *it, int j)
{
    RECORD *cur;
    cur=it;
    int i;  
    for(i=1;i<=j;i++)
    {
        printf("%s  \n", cur->fname);
        cur=cur->next;
    }
    return;
}
+3
source share
1 answer

You have:

insert:

char first[strsize];

scanf("%c", &first);  /* note the %c */

strcpy(cur->fname, first);

in the press

printf("%s  \n", cur->fname);

%s %c , , &format format , format , .

, scanf

scanf("%s", first);

. print, - ( void). .

, , , .

, . , , "j". , , next NULL .

, , , , .

+3

All Articles