打印此鏈接列表時遇到了一些問題。C:打印鏈接列表問題
該程序應該從用戶列表中取出10個字符,並按照它的順序打印出來,然後以相反的順序打印出來(還沒有那麼遠)。但是,它不讀第一個字符。
對於實施例
「請輸入字符」 用戶類型 一個(程序不讀取一個) b Ç d Ë ˚F 克 ħ 我 Ĵ ķ
然後它打印 b c d e ˚F 克 ħ 我 Ĵ ķ
試圖使此儘可能詳細。
謝謝!
#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;
double result;
RECORD *head, *p;
head=NULL;
result=10;
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", &junk);
scanf("%s", &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;
}
哎呀!您需要處理您的縮進, – pmg
我知道,我通常會在最後回退並縮進:-X – Erica
@Erica縮進的原因只有大約20%,所以其他人可以閱讀您的代碼。另外80%是這樣的_you_可以讀你的代碼。 – Daniel