#include<stdio.h>
#include<malloc.h>
struct Linknode
{
int num;
struct Linknode *next;
};
//靜态連結清單
void test1()
{
struct Linknode node1 = { 10 , NULL};
struct Linknode node2 = { 20 , NULL};
struct Linknode node3 = { 30 , NULL};
struct Linknode node4 = { 40 , NULL};
struct Linknode node5 = { 50 , NULL};
node1.next = &node2;
node2.next = &node3;
node3.next = &node4;
node4.next = &node5;
struct Linknode *p = &node1;
while( p != NULL)
{
printf("%d\n",p->num);
p = p->next;
}
}
//動态連結清單
void test2()
{
struct Linknode *node1= malloc(sizeof(struct Linknode));
struct Linknode *node2= malloc(sizeof(struct Linknode));
struct Linknode *node3= malloc(sizeof(struct Linknode));
struct Linknode *node4= malloc(sizeof(struct Linknode));
struct Linknode *node5= malloc(sizeof(struct Linknode));
node1->num = 10;
node2->num = 20;
node3->num = 30;
node4->num = 40;
node5->num = 50;
node1->next = node2;
node2->next = node3;
node3->next = node4;
node4->next = node5;
node5->next = NULL;
struct Linknode *p = node1;
while( p != NULL)
{
printf(":::%d\n",p->num);
p = p->next;
}
free(node1);
free(node2);
free(node3);
free(node4);
free(node5);
}
int main()
{
//test1();
test2();
}