线性链表的c语言实现代码具体如下
#include <malloc.h>
#include <stdio.h>
#include <stdlib.h>
//函数结果状态码
#define OK 1
#define ERROR 0
#define false 0
#define true 1
#define INFEASIBLE -3
struct LNode
{
int data;
struct LNode *next;
} * LinkList;
//初始化链表
LNode *InitList(int n)
{
LNode *p, *head;
LinkList = head = (LNode *)malloc(sizeof(LNode));
for (int i = 0; i < n; i++)
{
p = (LNode *)malloc(sizeof(LNode));
printf("please input %dth number:", i + 1);
scanf("%d", &p->data);
LinkList->next = p;
LinkList = p;
}
LinkList->next = NULL;
return head;
}
//销毁一个已经存在的链表
int DestroyList(LNode *L)
{
LNode *q;
while (q)
{
q = L->next;
free(L);
L = q;
}
return 100;
}
//置空一个链表
void ClearList(LNode *L)
{
LNode *p, *q;
p = L->next;
while (p)
{
q = p