当前位置: 代码迷 >> 综合 >> 一道关于链表的基本操作题
  详细解决方案

一道关于链表的基本操作题

热度:39   发布时间:2024-01-20 02:38:30.0

帮群里的朋友写的,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;}


  相关解决方案