给定单链表的头指针和一个结点指针,定义一个函数在O(1)时间删除该节点
来源:互联网 发布:出身不好知乎 编辑:程序博客网 时间:2024/05/16 08:55
算法描述:
给定单链表的头指针和一个结点指针,定义一个函数在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_Hstruct 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;}}
0 0
- 给定单链表的头指针和一个结点指针,定义一个函数在O(1)时间删除该节点
- 题目:给定单向链表的头指针和一个结点指针,定义一个函数在O(1)时间删除该结点。
- 给定单向链表的头指针和一个结点指针,定义一个函数在O(1)时间删除该结点
- 13\给定单向链表的头指针和一个结点指针,定义一个函数在O(1)时间删除该结点,
- 给定链表的头指针和一个结点指针,在O(1) 时间删除该结点
- 给定链表的头指针和一个结点指针,在O(1) 时间删除该结点
- 给定链表的头指针和一个结点指针,在O(1)时间删除该结点
- 给定链表的头指针和一个结点指针,在O(1)时间删除该结点。
- 给定链表的头指针和一个结点指针,在O(1)时间删除该结点
- 设一个没有头结点指针的单链表。一个指针指向此单链表中间的一个结点(不是第一个,也不是最后一个结点),将该结点从单链表中删除,要求时间复杂度O(1)。
- 给定一个节点指针 删除单链中当前结点
- 有一个单项的链表,在没有头结点的情况下,只知道有一个指向结点B的指针p,假设这个结点B不是尾结点,删除该节点B。
- 8、一个单链表,不给头指针,只给出当前结点,删除该结点。
- 在一个无头指针的单链表中,删除指针p所指向的结点
- 给定一单链表的表头指针和指向其中一个节点的指针,要求以该指针为头将原链表逆序
- (转)问题:假设一个没有头指针的单链表。一个指针指向此单链表中间的一个节点(既不是第一个,也不是最后一个节点),请将该节点从单链表中删除。
- (转)问题:假设一个没有头指针的单链表。一个指针指向此单链表中间的一个节点(既不是第一个,也不是最后一个节点),请将该节点从单链表中删除。
- 13 在O1的时间删除链表节点-使用一个节点指针
- 退学了
- 操作系统——进程管理
- Mongodb数据库与springmvc整合
- UItableview的一些总结
- Swift-基本运算符
- 给定单链表的头指针和一个结点指针,定义一个函数在O(1)时间删除该节点
- lineReader 文件上传MySQL(可读每行)
- C++第6次作业
- 2016 百度之星资格赛 Problem C (Trie模板)
- 菜鸟上路,杭电OJ1002之大数相加
- 委托事件(未完待续...)
- 【android】:android与java的关系
- jQuery 的选择器
- Activity的四种启动模式