首先准备如下3个文件:
nodelist.h:链表结构,函数声明
nodelist.c:函数定义
test.c:主函数 / 测试
nodelist.h
#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
typedef int datatype;
typedef struct listnode
{
datatype a;
struct listnode* next;
}listnode;
typedef listnode* nodep;
void printlist(nodep lty);
nodep supply(datatype a);
void addback(nodep* lty,datatype a);
void addfront(nodep* lty, datatype a);
void deleteback(nodep* lty);
void deletehead(nodep* lty);
nodep finddata(nodep lty, datatype a);
void addanypos(nodep* lty,int pos, datatype a);
void deleteanypos(nodep* lty, int pos);
void destory(nodep* lty);
nodelist.c
#define _CRT_SECURE_NO_WARNINGS 1
#include"nodelist.h"
void printlist(nodep lty)
{
if (lty == NULL)
{
printf("无数据");
return;
}
nodep temp = lty;
while (temp)
{
printf("%d ", temp->a);
temp = temp->next;
}
printf("\n");
}
nodep supply(datatype a)
{
nodep lty = (nodep)malloc(sizeof(listnode));
if (lty == NULL)
{
perror("mallocfail");
return;
}
lty->a = a;
lty->next = NULL;
return lty;
}
void addback(nodep* lty, datatype a)
{
assert(lty);
nodep temp = supply(a);
if (*lty == NULL)
{
*lty = temp;
return;
}
if (*lty != NULL)
{
nodep tem = *lty;
while (tem->next)
{
tem = tem->next;
}
tem->next = temp;
}
}
void addfront(nodep* lty, datatype a)
{
assert(lty);
nodep temp = supply(a);
if (*lty == NULL)
{
*lty = temp;
return;
}
if (*lty != NULL)
{
nodep tem = *lty;
*lty= temp;
(*lty)->next = tem;
}
}
void deleteback(nodep* lty)
{
assert(lty);
if (*lty == NULL)
{
printf("无数据,删除失败");
return;
}
if ((*lty)->next == NULL)
{
free(*lty);
*lty = NULL;
return;
}
nodep temp = *lty;
nodep front_1 = NULL;
while (temp->next)
{
front_1 = temp;
temp = temp->next;
}
front_1->next = NULL;
free(temp);
temp = NULL;
}
void deletehead(nodep* lty)
{
assert(lty);
if (*lty == NULL)
{
printf("无数据,删除失败");
return;
}
nodep tempnext = (*lty)->next;
nodep first = *lty;
free(first);
first = NULL;
*lty = tempnext;
}
nodep finddata(nodep lty, datatype a)
{
if (lty == NULL)
{
printf("无数据");
return NULL;
}
nodep temp = lty;
while (temp)
{
if (temp->a == a)
{
printf("找到了\n");
return temp;
}
temp = temp->next;
}
printf("找不到数据");
return NULL;
}
void addanypos(nodep* lty,int pos,datatype a)
{
assert(lty);
nodep temp = supply(a);
if (*lty == NULL)
{
*lty = temp;
return;
}
if (pos <= 1)
{
addfront(lty, a);
return;
}
if (*lty != NULL)
{
nodep tempp = *lty;
int y = pos - 2;
while(y--)
{
tempp = tempp->next;
if (tempp->next == NULL)
{
addback(lty, a);
return;
}
}
nodep temppp = tempp->next;
tempp->next = temp;
temp->next = temppp;
}
}
void deleteanypos(nodep* lty,int pos)
{
assert(lty);
if (*lty == NULL)
{
printf("无数据");
return;
}
if ((*lty)->next == NULL)
{
free(*lty);
*lty = NULL;
return;
}
nodep temp = *lty;
int t = pos - 2;
while (t--)
{
temp = temp->next;
if (temp->next == NULL)
{
deletepop(lty);
}
}
nodep newnext = temp->next->next;
nodep deletenode = temp->next;
free(deletenode);
deletenode = NULL;
temp->next = newnext;
}
void destory(nodep* lty)
{
assert(lty);
if ((*lty) == NULL)
{
printf("无数据");
return;
}
if ((*lty)->next==NULL)
{
free(*lty);
*lty = NULL;
}
else
{
nodep temp = *lty;
while (temp)
{
nodep nextnode = temp->next;
free(temp);
temp = nextnode;
}
}
*lty = NULL;
}
test.c
#define _CRT_SECURE_NO_WARNINGS 1
#include"nodelist.h"
void test()
{
}
int main()
{
test();
return 0;
}