测试代码仅测试插入,删除请自行测试
head_list.h
typedef int ElemType;
typedef struct Node {
ElemType data;
struct Node *next;
}HeadList;
void InitHeadList(HeadList* head);
int GetLength(HeadList* head);
void ShowList(HeadList* head);
bool InsertOFPos(HeadList* head, ElemType val, int pos);
bool InsertOFHead(HeadList* head, ElemType val);
bool InsertOFTail(HeadList* head, ElemType val);
bool DeleteOFPos(HeadList* head, int pos);
bool DeleteOFHead(HeadList* head);
bool DeleteOFTail(HeadList* head);
bool DeleteOFValue(HeadList* head, ElemType val);
void ClearHeadList(HeadList* head);
void DestroyHeadList(HeadList* head);
HeadList* HeadListQueryByVal(HeadList* head, ElemType val);
head_list.c
#include "head_list.h"
#include <stdlib.h>
#include <stdio.h>
static HeadList* ApplyNode(ElemType val, HeadList* ht)
{
HeadList* s = (HeadList*)malloc(sizeof(HeadList));
if (s == NULL) return NULL;
s->data = val;
s->next = ht;
}
void InitHeadList(HeadList* head)
{
if (head == NULL) exit(0);
head->next = NULL;
}
bool InsertOFPos(HeadList* head, ElemType val, int pos)
{
if (head == NULL) exit(0);
if (pos < 0) return false;
HeadList* p = head;
while (pos && p != NULL)
{
pos--; p = p->next;
}
if (p == NULL) return false;
HeadList* newNode = ApplyNode(val, p->next);
if (newNode == NULL) return false;
p->next = newNode; return true;
}
bool InsertOFHead(HeadList* head, ElemType val)
{
if (head == NULL) exit(0);
return InsertOFPos(head, val, 0);
}
bool InsertOFTail(HeadList* head, ElemType val)
{
if (head == NULL) exit(0);
HeadList* p = head;
while (p->next != NULL) p = p->next;
p->next = ApplyNode(val, NULL);
return true;
}
int GetLength(HeadList* head)
{
if (head == NULL) exit(0);
int length = 0;
HeadList * p = head->next;
while (p != NULL)
{
length++;
p = p->next;
}
return length;
}
void ShowList(HeadList * head)
{
if (head == NULL) exit(0);
HeadList * p = head->next;
while (p != NULL)
{
printf("%d ", p->data);
p = p->next;
}
printf("\n");
}
bool DeleteOFPos(HeadList* head, int pos)
{
if (head == NULL) exit(0);
HeadList * p = head;
while (pos && p->next != NULL)
{
pos--;
p = p->next;
}
if (p->next == NULL) return false;
HeadList * q = p->next;
p->next = q->next;
free(q);
return true;
}
bool DeleteOFHead(HeadList * head)
{
if (head == NULL) exit(0);
return DeleteOFPos(head, 0);
}
bool DeleteOFTail(HeadList * head)
{
if (head == NULL) exit(0);
if (head->next == NULL) return false;
HeadList * p = head, *q = head->next;
while (q->next != NULL)
{
p = q;
q = q->next;
}
p->next = NULL;
free(q);
return true;
}
bool DeleteOFValue(HeadList* head, ElemType val)
{
if (head == NULL) exit(0);
HeadList* p = head, * q = head->next;
while (q != NULL)
{
if (q->data == val)
{
p->next = q->next;
free(q);
q = p->next;
}
else
{
p = q;
q = q->next;
}
}
return true;
}
void ClearHeadList(HeadList* head)
{
if (head == NULL) exit(0);
while (head->next != NULL)
DeleteOFHead(head);
}
void DestroyHeadList(HeadList* head)
{
if (head == NULL) exit(0);
ClearHeadList(head);
}
HeadList* HeadListQueryByVal(HeadList* head, ElemType val)
{
if (head == NULL) exit(0);
while (head->next != NULL)
{
if (head->next->data == val)
return head->next;
else head = head->next;
}
return NULL;
}
test_main.c
#include "head_list.h"
void InsertOFPosTest()
{
HeadList head;
InitHeadList(&head);
InsertOFPos(&head, 1, 0);
InsertOFPos(&head, 2, 1);
InsertOFPos(&head, 3, 2);
InsertOFPos(&head, 4, 3);
InsertOFPos(&head, 5, 4);
InsertOFPos(&head, 6, 5);
ShowList(&head);
}
void InsertOFHeadTest()
{
HeadList head;
InitHeadList(&head);
InsertOFHead(&head, 1);
InsertOFHead(&head, 2);
InsertOFHead(&head, 3);
InsertOFHead(&head, 4);
InsertOFHead(&head, 5);
InsertOFHead(&head, 6);
ShowList(&head);
}
void InsertOFTailTest()
{
HeadList head;
InitHeadList(&head);
InsertOFTail(&head, 1);
InsertOFTail(&head, 2);
InsertOFTail(&head, 3);
InsertOFTail(&head, 4);
InsertOFTail(&head, 5);
InsertOFTail(&head, 6);
ShowList(&head);
}
int main()
{
InsertOFHeadTest();
InsertOFTailTest();
InsertOFPosTest();
}
单链表中倒数第K个结点测试.c
#include "head_list.h"
#include <stdlib.h>
#include <stdio.h>
void InsertOFTailTest()
{
HeadList head;
InitHeadList(&head);
InsertOFTail(&head, 1);
InsertOFTail(&head, 2);
InsertOFTail(&head, 3);
InsertOFTail(&head, 4);
InsertOFTail(&head, 5);
InsertOFTail(&head, 6);
ShowList(&head);
HeadList* p = HeadListQueryByVal(&head, 6);
if (p)
printf("%d\n", p->data);
}
int main()
{
InsertOFTailTest();
}
链表中环的入口结点.c
#include "head_list.h"
#include <stdlib.h>
#include <stdio.h>
HeadList* MeetingNode(HeadList* head)
{
if (head == NULL) exit(0);
HeadList* pSlow = head->next;
HeadList* pFast = NULL;
if (pSlow)
pFast = head->next->next;
while (pSlow != NULL && pFast != NULL && pFast->next != NULL)
{
if (pSlow == pFast)
return pSlow;
pSlow = pSlow->next;
pFast = pFast->next->next;
}
return NULL;
}
HeadList* EntryNodeOfLoop(HeadList* head)
{
HeadList* meetingNode = MeetingNode(head);
if (meetingNode == NULL) return NULL;
int nodeInLoop = 1;
HeadList* p1 = meetingNode;
while (p1->next != meetingNode)
{
p1 = p1->next;
nodeInLoop++;
}
p1 = head;
for (int i = 0; i < nodeInLoop; ++i)
p1 = p1->next;
HeadList* p2 = head;
while (p1 != p2)
{
p1 = p1->next;
p2 = p2->next;
}
return p1;
}
void InsertOFTailTest()
{
HeadList head;
InitHeadList(&head);
InsertOFTail(&head, 1);
InsertOFTail(&head, 2);
InsertOFTail(&head, 3);
InsertOFTail(&head, 4);
InsertOFTail(&head, 5);
InsertOFTail(&head, 6);
ShowList(&head);
HeadList* p1 = HeadListQueryByVal(&head, 6);
HeadList* p2 = HeadListQueryByVal(&head, 3);
p1->next = p2;
HeadList* p3 = EntryNodeOfLoop(&head);
if (p3)
printf("%d\n", p3->data);
}
int main()
{
InsertOFTailTest();
}