Head.C
typedef struct _chain
{
int num;
struct _chain *next;
} Chain, *chain;
chain CreateList(int size);
void FreeChainList(chain L);
void FreeAimNode(chain L, int location);
void PrintChain(chain L);
void PrintAimNode(chain L, int location);
void ChangeAimNodeVal(chain L, int location, int changeval);
void FindAimVal(chain L, int aimval);
void AddNNewNode(chain L, int location, int NewNodeNum);
void AddNewChainList(chain L, int location, chain NL);
void ChangeTail(chain L, int location, chain NL);
void CutTail(chain L, int location);
Function.C
chain CreateList(int size)
{
if (size == 0)
{
return NULL; //最后到这里就终结了
}
chain node;
node = (chain)malloc(sizeof(Chain)); //创建空间
node->num = 0; //赋初值
node->next = CreateList(size - 1);
return node;
}
//清除链表
void FreeChainList(chain L)
{
if (L != NULL)
{
FreeChainList(L->next);
}
free(L);
}
//清除指定位置的节点,location从0开始
void FreeAimNode(chain L, int location)
{
if (location == 0)
{
printf("Cannot delete the head node! \n");
return;
}
chain temp = L;
int i = 0;
while (i != location - 1) //定位到被删除节点前的那个
{
temp = temp->next;
i++;
}
chain freetemp = temp->next;
temp->next = freetemp->next;
free(freetemp);
}
//打印链表
void PrintChain(chain L)
{
chain temp = L;
while (temp != NULL)
{
printf("%d ", temp->num);
temp = temp->next;
}
printf("\n");
}
//打印链表指定节点值
void PrintAimNode(chain L, int location)
{
chain temp = L;
int i = 0;
while (i != location)
{
temp = temp->next;
i++;
}
printf("%d\n", temp->num);
}
//改变链表指定节点值
void ChangeAimNodeVal(chain L, int location, int changeval) //修改目标结点的数据
{
chain temp = L;
int i = 0;
while (i != location)
{
temp = temp->next;
i++;
}
temp->num = changeval;
}
//查找目标值
void FindAimVal(chain L, int aimval)
{
chain temp = L;
int index = 0;
int findamount = 0;
while (temp != NULL)
{
if (temp->num == aimval)
{
findamount++;
printf("Find NO.%d %d . The index is %d \n", findamount, temp->num, index);
}
temp = temp->next;
index++;
}
if (findamount == 0)
{
printf("Can not find aimval: %d \m", aimval);
}
}
//在该点的后方嵌入长度为NewNodeNum的新链表(0),location从0开始
void AddNNewNode(chain L, int location, int NewNodeNum)
{
//查找旧表的末尾
chain temp_old = L;
int i;
i = 0;
while (i != location)
{
temp_old = temp_old->next;
i++;
}
//创建新表
chain newlist = CreateList(NewNodeNum);
chain temp_new = newlist;
//查找新表的末尾
i = 0;
while (i != NewNodeNum - 1)
{
temp_new = temp_new->next;
i++;
}
temp_new->next = temp_old->next;
temp_old->next = newlist;
}
//在指定位置后方嵌入指定链表
void AddNewChainList(chain L, int location, chain NL)
{
chain temp_old = L;
int i;
i = 1;
while (i != location)
{
temp_old = temp_old->next;
i++;
}
chain temp_new = NL;
while (temp_new->next != NULL)
{
temp_new = temp_new->next;
}
temp_new->next = temp_old->next;
temp_old->next = NL;
}
//该点之后截断,接上新的链表
void ChangeTail(chain L, int location, chain NL)
{
chain temp = L;
int i;
i = 1;
while (i != location)
{
temp = temp->next;
i++;
}
FreeChainList(temp->next);
temp->next = NL;
}
//剪断链表的指定位置
void CutTail(chain L, int location)
{
chain temp = L;
int i;
i = 1;
while (i != location)
{
temp = temp->next;
i++;
}
FreeChainList(temp->next);
temp->next = NULL; //free之后必须要进行这种操作
}
Main.C
void main()
{
chain L = CreateList(4); //0000
PrintChain(L);
ChangeAimNodeVal(L, 0, 1);
ChangeAimNodeVal(L, 1, 2);
ChangeAimNodeVal(L, 2, 3);
ChangeAimNodeVal(L, 3, 1); //1231
PrintChain(L);
FindAimVal(L, 1);
FreeAimNode(L, 2); //121
PrintChain(L);
AddNNewNode(L, 1, 2); //12001
PrintChain(L);
//FreeAimNode(L, 2);//1000031
//PrintChain(L);
//printf("\n");
//PrintAimNode(L, 1);
//PrintAimNode(L, 6);
//PrintAimNode(L, 7);
//chain newlist = CreateList(4);
//AddNewChainList(L, 6, newlist);
//PrintChain(L);
//printf("\n");
//chain newlist2 = CreateList(1);
//ChangeAimNodeVal(newlist2, 1, 9);
//ChangeTail(L, 6, newlist2);
//PrintChain(L);
//printf("\n");
//CutTail(L, 1);
//PrintChain(L);
//printf("\n");
printf("\n");
system("pause");
}