#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
typedef struct lk
{
int id;
struct lk *pnext;
int data;
}Node;
Node *CreateNode()
{
Node *node = NULL;
node = (Node *)malloc(sizeof(Node));
node->pnext = NULL;
node->data = (rand()%100);
return node;
}
void InitList(Node **head ,int count)
{
Node *temp = *head;
int i = 0;
if(temp == NULL)
{
printf("list init error !\n");
}
else
{
for(i = 0; i < count; i++)
{
temp->pnext = CreateNode();
temp->pnext->id = i;
//printf("i = %d ",i);
//printf("%d",temp->pnext->id);
temp = temp->pnext;
}
}
printf("\n");
}
void FreeList(Node *head)
{
if(head->pnext == NULL)
{
if(head != NULL)
{
printf("head free \n");
free(head);
}
printf("end free\n");
return;
}
else
{
printf("one free\n");
FreeList(head->pnext);
}
}
void DisplayList(Node * head)
{
Node *temp = head;
if(temp == NULL)
{
printf("display error \n");
}
else
{
temp = temp->pnext;
while(temp != NULL)
{
printf(" id : %d ,data : %d \n",temp->id,temp->data);
temp = temp->pnext;
}
}
}
int main()
{
Node *list= NULL;
list = (Node *)malloc(sizeof(Node));
int count = 0;
printf("input create nodes:");
scanf("%d",&count);
InitList(&list,count);
DisplayList(list);
FreeList(list);
return 0;
}
只是简单的实现链表的创建和遍历。
Linux GCC 编译器。