开发者

C Programming: Linked Lists

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

Anyway, the purpose of the program is to enter 8 characters and have the program print the characters back out to you and also print the characters back out in reverse order, using linked lists of course.

I got this so far. There's a lot wrong with it (i think).

Problems are

  • When asking for characters from the user it should read in the amount of characters automatically without having to ask for how many characters

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

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

Lots of help needed here. It would be so much appreciated!

Code of course

#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;
}


You have:

in insert:

char first[strsize];

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

strcpy(cur->fname, first);

in print

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

You should have %s instead of %c and therefore change &format to format in the argument list, as format itself represents the address of the location the string is to be stored.

So the scanf call should be like below

scanf("%s", first);

Another thing. If you have specified a return type in the print function then you should return something, or make it return nothing (declare return type as void). This will not pose any problem in this case although.

Read the warning messages which the compiler throws to you and you would see the compiler actually had answered your questions.

You need to do some redesigns i think. For example to traverse the linked list you do not need to counter 'j'. you can detect the list termination by inspecting if the next link is NULL or not.

Your question was to print the characters or strings in reverse, so you need to write some other print function than what you have wrote.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜