#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef int elementtype;
struct node{
elementtype element;
struct node *next;
};
typedef struct node* list;
list cons(elementtype e, list l){//リストlの先頭にeを追加したリストを返す
list n = (struct node *)malloc(sizeof(struct node *));
n->element = e;
n->next = l;
return n;
}
void print_int_list(list l){//リストlに含まれる要素を先頭から出力
while(l != NULL){printf("[%d]",l->element);l = l->next;}
printf("\n");
}
int main(){
int i;
char buf[128];
list l = NULL;
list head;
while(fgets(buf,sizeof(buf),stdin) != NULL){
sscanf(buf,"%d",&i);
l = cons(i, l);
}
head = l;
while(l->next != NULL){//リストを循環リストにする
l = l->next;
}
l->next = head;
print_int_list(l);
return 0;
}
1
2
3
4
5
【出力(現在)】
[1][5][4][3][2][1][5][4][3][2][1]......
【出力(理想)】
[1][2][3][4][5][1][2][3][4][5].....