使用单链表实现如下功能
单链表反转
链表中环的检测
两个有序的链表合并
删除链表倒数第 n 个结点
求链表的中间结点
#include <iostream>
#include <stdio.h>
//单链表反转
//链表中环的检测
//两个有序的链表合并
//删除链表倒数第 n 个结点
//求链表的中间结点
typedef struct _Node
{
int value;
struct _Node *next;
}LinkList;
void InitCircleLinkList(LinkList *p)
{
int x[9] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
int i = 0;
LinkList *c = p;
LinkList *midPointer;
while (i < 9)
{
LinkList *tmp = (LinkList*)malloc(sizeof(LinkList));
tmp->value = x[i];
tmp->next = c->next;
c->next = tmp;
c = tmp;
if (i == 3) {
midPointer = tmp;
}
if (i == 8) {
c->next = midPointer;
}
i++;
}
}
void InitTwoLinkList(LinkList *L1, LinkList *L2) {
int arr1[5] = { 1,2,6,8,10 };
int arr2[5] = { 2,3,4,9,10 };
int i1 = 0;
int i2 = 0;
LinkList *c1 = L1;
LinkList *c2 = L2;
while (i1 < 5)
{
LinkList *tmp = (LinkList*)malloc(sizeof(LinkList));
tmp->value = arr1[i1];
tmp->next = c1->next;
c1->next = tmp;
c1 = tmp;
i1++;
}
while (i2 < 5)
{
LinkList *tmp = (LinkList*)malloc(sizeof(LinkList));
tmp->value = arr2[i2];
tmp->next = c2->next;
c2->next = tmp;
c2 = tmp;
i2++;
}
}
//单链表反转:就是申请一个新的链表然后头部插入就可以
void LinkLintReverse(LinkList *c1, LinkList *copyData) {
while (c1->next) {
LinkList *tmp = (LinkList*)malloc(sizeof(LinkList));
tmp->value = c1->next->value;
tmp->next = copyData->next;
copyData->next = tmp;
c1 = c1->next;
}
}
int CircleDetect(LinkList *p, int marker) {
//初始化一个环
InitCircleLinkList(p);
if (p->next == NULL || p->next->next == NULL) {
marker = 0;
return marker;
}
LinkList *f = p->next->next;
LinkList *s = p->next;
while (f->next && f->next->next) {
if (f == s) {
marker = 1;
break;
}
else {
f = f->next->next;
s = s->next;
}
}
return marker;
}
void CombineTwoLinkList(LinkList *p1, LinkList *p2, LinkList *LCombine) {
LinkList *LCombineTmp = LCombine;
while (p1->next && p2->next) {
LinkList *midTmp = (LinkList*)malloc(sizeof(LinkList));
if (p1->next->value >= p2->next->value) {
midTmp->value = p2->next->value;
p2 = p2->next;
}
else {
midTmp->value = p1->next->value;
p1 = p1->next;
}
midTmp->next = LCombineTmp->next;
LCombineTmp->next = midTmp;
LCombineTmp = midTmp;
}
}
void DeleSpecialNode(LinkList*p, int n) {
int LinkLen = 0;
LinkList *c = p;
while (c->next) {
c = c->next;
LinkLen++;
}
if (LinkLen<n) {
printf("删除的位置大于链表长度,删除失败");
return;
}
int target = LinkLen - n;
int tmpi = 0;
c = p;
while (tmpi<target) {
c = c->next;
tmpi++;
}
c->next = c->next->next;
}
void FindmidNode(LinkList*per, LinkList*&midtarget) {
int LinkLen = 0;
LinkList *c = per;
while (c->next) {
c = c->next;
LinkLen++;
}
int target = LinkLen / 2 + LinkLen % 2;
int tmpi = 0;
c = per;
while (tmpi<target) {
c = c->next;
tmpi++;
}
//midNode->value = c->value;
//midNode->next = c->next;
midtarget = c;
}
int main() {
LinkList *p;
p = (LinkList*)malloc(sizeof(LinkList));
p->next = NULL;
//在链表中插入数据
int x[9] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
int i = 0;
LinkList *c = p;
while (i < 9)
{
LinkList *tmp = (LinkList*)malloc(sizeof(LinkList));
tmp->value = x[i];
tmp->next = c->next;
c->next = tmp;
c = tmp;
i++;
}
//单链表反转
LinkList *copyData = (LinkList*)malloc(sizeof(LinkList));
copyData->next = NULL;
LinkLintReverse(p, copyData);
//链表中环的检测
int marker = 0; //0表示无环,1表示有环
LinkList *p2;
p2 = (LinkList*)malloc(sizeof(LinkList));
p2->next = NULL;
marker = CircleDetect(p2, marker);
//两个有序的链表合并
LinkList *L1 = (LinkList*)malloc(sizeof(LinkList));
LinkList *L2 = (LinkList*)malloc(sizeof(LinkList));
L1->next = NULL;
L2->next = NULL;
InitTwoLinkList(L1, L2);
LinkList *LCombine = (LinkList*)malloc(sizeof(LinkList));
LCombine->next = NULL;
CombineTwoLinkList(L1, L2, LCombine);
//删除链表倒数第 n 个结点
int n = 9;
DeleSpecialNode(p, n);
//求链表的中间结点
LinkList *midNode = (LinkList*)malloc(sizeof(LinkList));
midNode->next = NULL;
FindmidNode(p, midNode);
return 0;
}