由于刚刚才开始学习链表,自己写了一段尾插法的代码,但是最后老是说Run-Time Check Failure #2 - Stack around the variable 'L' was corrupted.用的是c语言
代码如下
#include<stdio.h>
#include<stdlib.h>
typedef struct Node
{
int data;
struct Node* next;
}Node;
Node *getheadLink(struct Node* L)
{
L = (struct Node*)malloc(sizeof(struct Node));
if (L == NULL)
{
exit(0);
}
L->next = NULL;
L->data = 0;
return L;
}
void getLink(struct Node* L,int a)
{
struct Node* p, *r;
int b,i;
r = L;
for (i = 0; i < a; i++)
{
p = (struct Node*)malloc(sizeof(struct Node));
if (p == NULL)
{
exit(0);
}
scanf_s("%d", &b);
p->data = b;
r->next = p;
r = p;
}
r->next = NULL;
}
void printfLink(struct Node* L)
{
struct Node* q;
q = L;
while (L->next != NULL)
{
printf("\n%d\n", L->next->data);
L = L->next;
}
printf("ok!");
}
int main()
{
struct Node* L;
int a;
L=getheadLink(&L);
scanf_s("%d", &a);
getLink(&L, a);
printfLink(&L);
return 0;
}