
一道关于链表的基本操作题
发布日期:2021-05-07 23:35:11
浏览次数:13
分类:原创文章
本文共 3716 字,大约阅读时间需要 12 分钟。
帮群里的朋友写的,main函数只写了单元测试每个子程序
//This code is complied by the cc.exe in Visual studio 2013 using the c++#include <iostream>struct ListNode{ int data; struct ListNode * next;};bool resideInSet(ListNode * head, int data);bool checkResideInSet(ListNode * head, int data);void printSet(ListNode * head);//We allocate the set object in the heap memory.ListNode * createSet(int setNum){ //check the pre-condition. if (setNum <= 0) return NULL; ListNode * head = new ListNode; ListNode * prevNode = head; prevNode->next = NULL; int inputNum; std::cout << "Please input the value for the sub node:"; std::cin >> head->data; for (int i = 1; i < setNum; i++) { std::cin >> inputNum; //Check that whether the user input the same element. while (resideInSet(head, inputNum)) { std::cout << "Your input element: " << inputNum << "reside in set, please reinput the data:"; std::cin >> inputNum; } ListNode * subNode = new ListNode; subNode->data = inputNum; prevNode->next = subNode; prevNode = subNode; prevNode->next = NULL; } printSet(head); return head;}bool deleteNode(ListNode ** head, int data){ ListNode * phead = *head, *prev = NULL; while (phead) { if (phead->data == data) { if (prev == NULL) { prev = phead; phead = phead->next; *head = phead; delete prev; } else { prev->next = phead->next; delete phead; } return true; } prev = phead; phead = phead->next; } return false;}void destorySet(ListNode * head){ ListNode * phead = head, *pnode; while (phead) { pnode = phead->next; delete phead; phead = pnode; }}void printSet(ListNode * head){ if (head == NULL) { std::cout << "The set is empty!" << std::endl; return; } ListNode * phead = head; std::cout << "The data of the set is: "; while (phead) { std::cout << phead->data << " "; phead = phead->next; } std::cout << std::endl;}bool resideInSet(ListNode * head, int data){ if (head == NULL) return false; ListNode * phead = head; while (phead) { if (phead->data == data) return true; phead = phead->next; } return false;}bool checkResideInSet(ListNode * head, int data){ if (resideInSet(head, data)) { std::cout << "The data " << data << "reside in Set." << std::endl; return true; } else { std::cout << "The data" << data << "doesn't reside in set." << std::endl; return false; } }ListNode * createUnionSet(ListNode * lstA, ListNode * lstB){ if (lstA == NULL && lstB == NULL) return NULL; ListNode * phead = new ListNode; ListNode * head = phead; ListNode * pheadA = lstA; phead->data = lstA->data; lstA = lstA->next; while (lstA) { ListNode * pnode = new ListNode; pnode->data = lstA->data; phead->next = pnode; phead = pnode; lstA = lstA->next; } while (lstB) { if (!resideInSet(pheadA, lstB->data)) { ListNode * pnode = new ListNode; pnode->data = lstB->data; phead->next = pnode; phead = pnode; } lstB = lstB->next; } phead->next = NULL; return head;}ListNode * createInterSet(ListNode * lstA, ListNode * lstB){ if (lstA == NULL && lstB == NULL) return NULL; ListNode * phead = NULL; ListNode * head = NULL; ListNode * pheadA = lstA; ListNode * pheadB = lstB; while (lstA) { if (resideInSet(pheadB, lstA->data)) { ListNode * pnode = new ListNode; pnode->data = lstA->data; if (phead == NULL) // we create the head node. { phead = pnode; head = phead; } else { phead->next = pnode; phead = pnode; } } lstA = lstA->next; } if (phead) phead->next = NULL; return head;}int main(){ /*Unit test ListNode * linklist1 = createSet(5); ListNode * linklist2 = createSet(3); ListNode * linkInterSet = createInterSet(linklist1, linklist2); printSet(linkInterSet); destorySet(linklist1); destorySet(linklist2); destorySet(linkInterSet); */ return 0;}
发表评论
最新留言
路过,博主的博客真漂亮。。
[***.116.15.85]2025年04月05日 07时00分28秒
关于作者

喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
int 转 CString
2019-03-04
Edit编辑框自动换行与长度
2019-03-04
Java面向对象
2019-03-04
JAVA带标签的break和continue
2019-03-04
Java获取线程基本信息的方法
2019-03-04
vue源码分析(MVVM篇)
2019-03-04
设计模式之组合模式
2019-03-04
(Python学习笔记):字典
2019-03-04
(C++11/14/17学习笔记):线程启动、结束,创建线程多法、join,detach
2019-03-04
leetcode 14 最长公共前缀
2019-03-04
做做Java
2019-03-04
C++并发与多线程(一)
2019-03-04
java一些基本程序
2019-03-04
vue-依赖-点击复制
2019-03-04
LeetCode 116填充每个节点的下一个右侧结点指针
2019-03-04
2021-4-28【PTA】【L2-1 包装机 (25 分)】
2019-03-04
Arduino mega2560+MPU6050利用加速度值控制舵机
2019-03-04
紫书——蛇形填数
2019-03-04
A Guide to Node.js Logging
2019-03-04
webwxbatchgetcontact一个神奇的接口
2019-03-04