This problem required to achieve a function, a list in reverse order of the input data.
Function interface definition:
struct ListNode createList * ();
Createlist function scanf acquired using input from a series of positive integers, it indicates the end of input when read -1. Establishing a list in reverse order of the input data, and return the list head pointer. List node structure is defined as follows:
struct ListNode {
int data;
struct ListNode *next;
};
Referee test sample program:
#include <stdio.h>
#include <stdlib.h>
struct ListNode {
int data;
struct ListNode *next;
};
struct ListNode *createlist();
int main()
{
struct ListNode *p, *head = NULL;
head = createlist();
for ( p = head; p != NULL; p = p->next )
printf("%d ", p->data);
printf("\n");
return 0;
}
/ * Your code will be embedded here * /
Sample input:
1234567-1
Output Sample:
7654321
struct ListNode *createlist()
{
struct ListNode *head, *p;
int temp;
head = (struct ListNode*)malloc(sizeof(struct ListNode));
head->next = NULL; // 如果不初始化指向任意地方
scanf("%d", &temp);
while (temp != -1)
{
p = (struct ListNode*)malloc(sizeof(struct ListNode));
p->data = temp;
p->next = head->next;
head->next = p;
scanf("%d", &temp);
}
return head->next;
}