#include <stdlib.h> typedef int elementtype; struct node { elementtype element; struct node* next; }; typedef struct node* list; list cons(elementtype e, list l) { list n; n = (list)malloc(sizeof(struct node)); n->element = e; n->next =l; return n; } int length(list l) { int c = 0; while(l!=NULL){ c++; l = l->next; } return c; } void print_int_list(list l) { while (l != NULL) { printf("[%d]", l->element); l = l->next; } } int main() { int i; wchar_t buf[128]; list l = NULL; while(fgetws(buf, sizeof(buf), stdin) !=NULL) { sscanf(buf, "%d", &i); l = cons(i, l); } printf("length=%d\n", length(l)); print_int_list(l); return 0; } コード ```### 前提・実現したいこと ここに質問の内容を詳しく書いてください。 (例)PHP(CakePHP)で●●なシステムを作っています。 ■■な機能を実装中に以下のエラーメッセージが発生しました。 ### 発生している問題・エラーメッセージ Linux上で 1 2 3 ctrl+x と入力すると [3][2][1] length=3 正しく出力されるのに 別のパソコンのwindow10 vscode上では 1 2 3 ctrl+z と入力すると length=0 としか出力されず困っています。 エラーメッセージ
該当のソースコード
#include <stdio.h>
#include <stdlib.h>
typedef int elementtype;
struct node {
elementtype element;
struct node* next;
};
typedef struct node* list;
list cons(elementtype e, list l) {
list n;
n = (list)malloc(sizeof(struct node));
n->element = e;
n->next =l;
return n;
}
int length(list l) {
int c = 0;
while(l!=NULL){
c++;
l = l->next;
}
return c;
}
void print_int_list(list l) {
while (l != NULL) {
printf("[%d]", l->element);
l = l->next;
}
}
int main() {
int i;
wchar_t buf[128];
list l = NULL;
while(fgetws(buf, sizeof(buf), stdin) !=NULL) {
sscanf(buf, "%d", &i);
l = cons(i, l);
}
printf("length=%d\n", length(l)); print_int_list(l); return 0;
}
試したこと
ここに問題に対して試したことを記載してください。
補足情報(FW/ツールのバージョンなど)
ここにより詳細な情報を記載してください。
あなたの回答
tips
プレビュー