我用malloc申请空间,编译不过去
/写出将带头结点的线性单链表L就地逆置的算法#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#define LEN sizeof(struct student)
struct student{
int num;
struct student *next;
}*L;
struct student *L()
{
int *L=NULL;
L =(int *)malloc(sizeof(int)*LEN);
if(NULL!=L)
exit(1);
else { L->next=LEN;
L->num =0};
}; 本帖最后由 jackz007 于 2021-3-15 18:07 编辑
#include <stdio.h>
#include <stdlib.h>
struct student{
int num ;
struct student * next ;
} ;
struct student * foo(struct student * head , int d)
{
struct student * p1 , * p2 ;
if((p1 = (struct student *) malloc(sizeof(struct student)))) {
p1 -> next = NULL ;
p1 -> num = d ;
for(p2 = head ; p2 && p2 -> next ; p2 = p2 -> next) ;
if(p2) p2 -> next = p1 ;
else head = p1 ;
} else {
fprintf(stderr , "\n") ;
fprintf(stderr , "Error : malloc()\n") ;
fprintf(stderr , "\n") ;
}
return head ;
}
int main(void)
{
struct student * L , * P ;
int i , m ;
for(L = NULL , i = 0 ; i < 10 ; i ++) L = foo(L , 100 + i) ;
printf("%d" , L -> num) ;
for(P = L -> next ; P ; P = P -> next) printf(" , %d" , P -> num) ;
printf("\n") ;
for(P = L ; P ; L = P) {
P = L -> next ;
free(L) ;
}
}
编译、运行实况:
D:\0002.Exercise\C>g++ -o x x.c
D:\0002.Exercise\C>x
100 , 101 , 102 , 103 , 104 , 105 , 106 , 107 , 108 , 109
D:\0002.Exercise\C>
楼主似乎格外偏爱 L,定义个结构名叫 L,定义个函数,名字还叫 L,这是为什么呢?
页:
[1]