#include <stdio.h>
#include <stdlib.h>
#define MAX 20
struct node
{
char strings[MAX];
struct node *next;
};
struct node *creat(struct node *head)
{
int i;
int counter = 1;
char input[MAX];
struct node *new;
struct node *pointer;
printf("请向链表中存入第%d个字符串\n", counter);
scanf_s("%s", input);
for (i = 0; i < MAX; i++)
{
head->strings[i] = input[i];
}
head->next = NULL;
pointer = head;
counter++;
while (counter <= 10)
{
new = (struct node*)malloc(sizeof(struct node));
printf("请向链表中存入第%d个字符串\n", counter);
scanf_s("%s", input);
for (i = 0; i < MAX; i++)
{
new->strings[i] = input[i];
}
new->next = NULL;
pointer->next = new;
pointer = new;
counter++;
}
return head;
}
struct node *reverse(struct node *head)
{
struct node *tail;
struct node *next;
struct node *pointer;
tail = NULL;
next = head->next;
pointer = head; //初始化
while (head != NULL)
{
head->next = tail;
tail = head;
head = next;
next = head->next;
}
return tail;
}
void print(struct node* head)
{
struct node *pointer;
int counter = 1;
pointer = head;
while (pointer != NULL)
{
printf("第%d个节点的数据是:%s", counter, pointer->strings);
counter++;
pointer = pointer->next;
}
}
int main()
{
struct node *head;
head = (struct node*)malloc(sizeof(struct node));
head = creat(head);
head = reverse(head);
print(head);
system("PAUSE");
return 0;
}
运行时第一次输入数据后便出错弹出0xC0000005: 写入位置 0x01300000 时发生访问冲突。请指教!