#include <stdio.h>
#include <stdlib.h>
#define _DEBUG
#undef PDEBUG
#ifdef _DEBUG
#define PDEBUG(fmt, args...) printf(fmt, ##args)
#else
#define PDEBUG(fmt, args...)
#endif
typedef struct node
{
int data
;
struct node
*next
;
}Node
;
Node
*creatnode(int num
)
{
Node
*p
= (Node
*)malloc(sizeof(Node
));
p
->data
= num
;
p
->next
= NULL;
return p
;
}
Node
*creatlist(int arr
[], int length
)
{
Node
*head
, *p
, *tail
;
int len
= length
;
int i
;
for(i
=0; i
<len
; i
++)
{
p
= creatnode(arr
[i
]);
if(0 == i
)
head
= p
;
else
tail
->next
= p
;
tail
= p
;
}
tail
->next
= NULL;
return head
;
}
Node
*reverslist(Node
*head
)
{
Node
*pre
= head
;
Node
*cur
= head
->next
;
Node
*tmp
= head
->next
->next
;
while( cur
)
{
tmp
= cur
->next
;
cur
->next
= pre
;
pre
= cur
;
cur
= tmp
;
}
head
->next
= NULL;
return pre
;
}
void display(Node
*head
)
{
Node
*p
=head
;
while( p
)
{
PDEBUG("%d->", p
->data
);
p
= p
->next
;
}
PDEBUG("\n");
}
int main(void)
{
int arr
[] = {0, 1, 3, 5, 7, 9, 10, 12};
int length
= sizeof(arr
)/sizeof(arr
[0]);
Node
*s
= creatlist(arr
, length
);
display(s
);
return 0;
}
转载请注明原文地址: https://mac.8miu.com/read-1712.html