您的位置:首页 > 其它

给定单链表的头指针和一个结点指针,定义一个函数在O(1)时间删除该节点

2016-05-25 15:14 441 查看
算法描述:

给定单链表的头指针和一个结点指针,定义一个函数在O(1)时间删除该节点

算法实现:

/*************************************************************************
> File Name: main.c
> Author: cyf
> Mail: XXX@qq.com
> Created Time: 2016年05月20日 星期五 09时57分05秒
************************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "deleteNode.h"
void Test(struct ListNode* pListHead, struct ListNode* pNode)
{
printf("\nThe original list is: \n");
PrintList(pListHead);

printf("\nThe node to be deleted is: \n");
PrintListNode(pNode);

deleteNode(&pListHead, pNode);

printf("\nThe result list is: \n");
PrintList(pListHead);
}

// 链表中有多个结点,删除中间的结点
void Test1()
{
struct ListNode* pNode1 = CreateListNode(1);
struct ListNode* pNode2 = CreateListNode(2);
struct ListNode* pNode3 = CreateListNode(3);
struct ListNode* pNode4 = CreateListNode(4);
struct ListNode* pNode5 = CreateListNode(5);

ConnectListNodes(pNode1, pNode2);
ConnectListNodes(pNode2, pNode3);
ConnectListNodes(pNode3, pNode4);
ConnectListNodes(pNode4, pNode5);

Test(pNode1, pNode3);

DestroyList(pNode1);
}
// 链表中有多个结点,删除尾结点
void Test2()
{
struct ListNode* pNode1 = CreateListNode(1);
struct ListNode* pNode2 = CreateListNode(2);
struct ListNode* pNode3 = CreateListNode(3);
struct ListNode* pNode4 = CreateListNode(4);
struct ListNode* pNode5 = CreateListNode(5);

ConnectListNodes(pNode1, pNode2);
ConnectListNodes(pNode2, pNode3);
ConnectListNodes(pNode3, pNode4);
ConnectListNodes(pNode4, pNode5);

Test(pNode1, pNode5);

DestroyList(pNode1);
}

// 链表中有多个结点,删除头结点
void Test3()
{
struct ListNode* pNode1 = CreateListNode(1);
struct ListNode* pNode2 = CreateListNode(2);
struct ListNode* pNode3 = CreateListNode(3);
struct ListNode* pNode4 = CreateListNode(4);
struct ListNode* pNode5 = CreateListNode(5);

ConnectListNodes(pNode1, pNode2);
ConnectListNodes(pNode2, pNode3);
ConnectListNodes(pNode3, pNode4);
ConnectListNodes(pNode4, pNode5);

Test(pNode1, pNode1);

DestroyList(pNode1);
}

// 链表中只有一个结点,删除头结点
void Test4()
{
struct ListNode* pNode1 = CreateListNode(1);

Test(pNode1, pNode1);
}

// 链表为空
void Test5()
{
Test(NULL, NULL);
}

int main()
{
Test1();
Test2();
Test3();
Test4();
Test5();
return 0;
}
/*************************************************************************
> File Name: deleteNode.h
> Author: cyf
> Mail: XXX@qq.com
> Created Time: 2016年05月20日 星期五 09时44分14秒
************************************************************************/

#ifndef _DELETENODE_H
#define _DELETENODE_H

#include <stdio.h>
#include <stdlib.h>
#include "List.h"

/*
* 给定单链表的头指针和一个结点指针,定义一个函数在O(1)时间删除该节点
* */
void deleteNode(struct ListNode **pListHead, struct ListNode *pToBeDeleted);

#endif
/*************************************************************************
> File Name: deleteNode.c
> Author: cyf
> Mail: XXX@qq.com
> Created Time: 2016年05月20日 星期五 09时45分37秒
************************************************************************/

#include "deleteNode.h"

void deleteNode(struct ListNode **pListHead, struct ListNode *pToBeDeleted)
{
if (!pListHead || !pToBeDeleted)
return ;
//中间节点
if (pToBeDeleted->next != NULL)
{
struct ListNode *pNext = pToBeDeleted->next;
pToBeDeleted->data = pNext->data;
pToBeDeleted->next = pNext->next;
free(pNext);
pNext = NULL;
}
//头结点
else if (*pListHead == pToBeDeleted)
{
free(pToBeDeleted);
pToBeDeleted = NULL;
*pListHead = NULL;
}
//尾结点
else
{
struct ListNode *pNode = *pListHead;
while (pNode->next != pToBeDeleted)
{
pNode = pNode->next;
}
pNode->next = NULL;
free(pToBeDeleted);
pToBeDeleted = NULL;
}
}
/************************************************************************
> File Name: List.h
> Author: cyf
> Mail: 1097189275@qq.com
> Created Time: 2016年03月23日 星期三 17时27分15秒
************************************************************************/

#ifndef ALGRITHMN_LIST_H
#define ALGRITHMN_LIST_H

struct ListNode
{
int data;
struct ListNode *next;
};
struct ListNode *CreateListNode(int value);
void ConnectListNodes(struct ListNode *pCurrent, struct ListNode *pNext);
void PrintListNode(struct ListNode *pNode);
void PrintList(struct ListNode *pHead);
void AddToTail(struct ListNode **pHead, int value);
void RemoveNode(struct ListNode **pHead,int value);
unsigned int GetListLength(struct ListNode *pHead);
void DestroyNode(struct ListNode *pNode);
void DestroyList(struct ListNode *pHead);

#endif
/*************************************************************************
> File Name: List.cpp
> Author: cyf
> Mail: 1097189275@qq.com
> Created Time: 2016年03月23日 星期三 17时32分11秒
************************************************************************/

#include "List.h"
#include <stdio.h>
#include <stdlib.h>
/*
* 创建一个链表的结点
* */
struct ListNode *CreateListNode(int value)
{
struct ListNode *pNode =(struct ListNode *)malloc(sizeof(struct ListNode));
pNode->data = value;
pNode->next = NULL;

return pNode;
}
/*
* 将两个结点连接起来
* */
void ConnectListNodes(struct ListNode *pCurrent, struct ListNode *pNext)
{
if(!pCurrent)
{
printf("ERROR!connect two ListNode.\n");
exit(1);
}
pCurrent->next = pNext;
}
/*
* 打印结点
* */
void PrintListNode(struct ListNode *pNode)
{
if(!pNode)
{
printf("the node is null!\n");
exit(1);
}
printf("The value of the node is %d\n",pNode->data);
}
/*
* 打印链表
* */
void PrintList(struct ListNode *pHead)
{
printf("print the list:\n");
struct ListNode *pNode = pHead;
if(!pNode)
{
printf("the list is null\n");
exit(1);
}
while(pNode != NULL)
{
printf("%d ",pNode->data);
pNode = pNode->next;
}
}
/*
* 在链表结尾添加结点
* */
void AddToTail(struct ListNode **pHead, int value)
{
struct ListNode *pNew = (struct ListNode*)malloc(sizeof(struct ListNode));
pNew->data = value;
pNew->next = NULL;

if(!pHead)
*pHead = pNew;
else
{
struct ListNode *pNode = *pHead;
while(pNode != NULL)
{
pNode = pNode->next;
}
pNode->next = pNew;
}

}
/*
* 删除链表中结点值为value的结点
* */
void RemoveNode(struct ListNode **pHead, int value)
{
if(pHead == NULL||*pHead == NULL)
{
printf("the list is null");
return;
}
struct ListNode *ToBeDel = NULL;

if((*pHead)->data == value)
{
ToBeDel = *pHead;
*pHead = (*pHead)->next;
}
else
{
struct ListNode *pNode = *pHead;

while(pNode->next != NULL && pNode->next->data != value)
pNode = pNode->next;
if(pNode->next != NULL && pNode->next->data == value)
{
ToBeDel = pNode->next;
pNode->next = pNode->next->next;
}
}

if(ToBeDel != NULL)
{
free(ToBeDel);
ToBeDel = NULL;
}
}
/*
* 计算链表的长度
* */
unsigned int GetListLength(struct ListNode *pHead)
{
unsigned int length = 0;
struct ListNode *pNode = pHead;

while(pNode != NULL)
{
++length;
pNode = pNode->next;
}

return length;
}
/*
* 销毁一个结点
* */
void DestroyNode(struct ListNode *pNode)
{
free(pNode);
pNode = NULL;
}
void DestroyList(struct ListNode* pHead)
{
struct ListNode *pNode = pHead;
while (pNode != NULL)
{
pHead = pHead->next;
free(pNode);
pNode = pHead;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: