- 链表节点的删除与插入;
- 链表节点的查询;
- 链表的快慢指针用法;
- 链表的逆置(两种方法);(暂不考虑递归实现)
- 有序链表和合并;
头文件"Slist.h"
#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
typedef int DateType;
typedef struct PlistNode
{
DateType _date;
struct PlistNode* _next;
} PlistNode;
PlistNode* GetNode(DateType x);
void Print(PlistNode* plist);
void PushBack(PlistNode** pplist, DateType x);
void PopBack(PlistNode** pplist);
void PushFront(PlistNode** pplist, DateType x);
void PopFront(PlistNode** pplist);
PlistNode* ListFind(PlistNode* plist, DateType x);
void ListInsertAfter(PlistNode** pos, DateType x);
void SListEraseAfter(PlistNode** pos);
PlistNode* middleNode(PlistNode** head);
PlistNode* reverseList1(PlistNode** head);
PlistNode* reverseList2(PlistNode** head);
PlistNode* FindKNode(PlistNode** head, int k);
源文件"Slist.c"
#include "Slist.h"
void Print(PlistNode* plist)
{
PlistNode* cur = plist;
while (cur != NULL)
{
printf("%d\n", cur->_date);
cur = cur->_next;
}
printf("\n");
}
PlistNode* GetNode(DateType x)
{
PlistNode* NewNode = (PlistNode*)malloc(sizeof(PlistNode));
NewNode->_date = x;
NewNode->_next = NULL;
return NewNode;
}
void PushBack(PlistNode** plist, DateType x)
{
PlistNode* NewNode = GetNode(x);
if (*plist == NULL)
*plist = NewNode;
else
{
PlistNode* tail = *plist;
while (tail->_next != NULL)
tail = tail->_next;
tail->_next = NewNode;
}
}
void PopBack(PlistNode** plist)
{
if (*plist == NULL || (*plist)->_next == NULL)
{
free(*plist);
*plist = NULL;
}
else
{
PlistNode* cur = *plist;
PlistNode* prv = NULL;
while (cur->_next != NULL)
{
prv = cur;
cur = cur->_next;
}
free(cur);
cur = NULL;
prv->_next = NULL;
}
}
void PushFront(PlistNode** plist, DateType x)
{
PlistNode* NewNode = GetNode(x);
if (*plist == NULL)
*plist = NewNode;
else
{
NewNode->_next = *plist;
*plist = NewNode;
}
}
void PopFront(PlistNode** plist)
{
if (*plist == NULL)
return;
else if ((*plist)->_next == NULL)
{
free(*plist);
*plist = NULL;
}
else
{
PlistNode* cur = (*plist)->_next;
free(*plist);
*plist = cur;
}
}
PlistNode* ListFind(PlistNode* pslist, DateType x)
{
assert(pslist);
PlistNode* cur = pslist;
while (cur != NULL)
{
if (x == cur->_date)
return cur;
cur = cur->_next;
}
return NULL;
}
void ListInsertAfter(PlistNode** pos, DateType x)
{
PlistNode* cur = GetNode(x);
PlistNode* tmp = NULL;
tmp = (*pos)->_next;
(*pos)->_next = cur;
cur->_next = tmp;
}
void SListEraseAfter(PlistNode** pos)
{
PlistNode* tmp = NULL;
PlistNode* cur = NULL;
tmp = (*pos)->_next;
cur = tmp->_next;
(*pos)->_next = cur;
free(tmp);
tmp = NULL;
}
PlistNode* middleNode(PlistNode** head)
{
PlistNode* fast = *head;
PlistNode* slow = *head;
while (fast != NULL && fast->_next != NULL)
{
fast = fast->_next->_next;
slow = slow->_next;
}
return slow;
}
PlistNode* reverseList1(PlistNode** head)
{
if ((*head == NULL) || ((*head)->_next == NULL))
return *head;
PlistNode* n1 = (*head);
PlistNode* n2 = (*head)->_next;
PlistNode* n3 = n2->_next;
n1->_next = NULL;
while (n2 != NULL)
{
n2->_next = n1;
n1 = n2;
n2 = n3;
if (n3 != NULL)
n3 = n3->_next;
}
return n1;
}
PlistNode* reverseList2(PlistNode** head)
{
if ((*head) == NULL || (*head)->_next == NULL)
return (*head);
PlistNode* cur = *head;
PlistNode* newhead = GetNode(cur->_date);
PlistNode* tmp = NULL;
while (cur->_next != NULL)
{
cur = cur->_next;
tmp = GetNode(cur->_date);
tmp->_next = newhead;
newhead = tmp;
}
return newhead;
}
PlistNode* FindKNode(PlistNode** head, int k)
{
if ((*head) == NULL)
return NULL;
PlistNode* fast = *head;
PlistNode* slow = *head;
while (k--)
{
if (fast == NULL)
return NULL;
fast = fast->_next;
}
while (fast != NULL)
{
fast = fast->_next;
slow = slow->_next;
}
return slow;
}
主函数"main.c"
#include "Slist.c"
void text()
{
PlistNode* head = NULL;
PlistNode* cur = NULL;
PlistNode* res1 = NULL;
PlistNode* res2 = NULL;
PlistNode* res3 = NULL;
PushBack(&head, 1);
PushBack(&head, 2);
PushBack(&head, 3);
PushBack(&head, 4);
PushBack(&head, 5);
Print(head);
PopBack(&head);
Print(head);
PushFront(&head, 0);
Print(head);
cur = ListFind(head, 4);
Print(cur);
ListInsertAfter(&cur, 100);
Print(head);
SListEraseAfter(&cur);
Print(head);
res1 = middleNode(&head);
printf("%d\n\n", res1->_date);
res1 = reverseList1(&head);
Print(res1);
res2 = reverseList2(&res1);
Print(res2);
res3 = FindKNode(&res2, 3);
printf("%d\n\n", res3->_date);
Print(res3);
}
int main()
{
text();
system("pause");
return 0;
}