[Huawei Machine Test exercises]24. Delete repeated nodes in the linked list, reverse output of the remaining nodes

Source: Internet
Author: User


描写叙述:     题目描写叙述:输入一个不带头节点的单向链表(链表的节点数小于100),删除链表中内容反复的节点(反复的节点所有删除),剩余的节点逆序倒排。要求实现函数: void vChanProcess(strNode * pstrIn,strNode * pstrOut);【输入】 pstrIn:输入一个不带头节点的单向链表【输出】 pstrOut:删除内容反复的节点(反复的节点所有删除)。剩余节点逆序输出(不带头节点,链表第一个节点的内存已经申请)。【注意】仅仅须要完毕该函数功能算法。中间不须要有不论什么IO的输入输出演示样例 输入链表的内容依次为 6,7,8,8,9,10,6则输出链表的内容依次应该是 10,9,7练习阶段:    


/*---------------------------------------* 日期:2015-06-31* SJF0115* 题目:删除链表中的反复节点、剩余节点逆序输出* 来源:华为机试练习题-----------------------------------------*/#include <iostream>#include <map> #include <vector>#include "oj.h"using namespace std;/*功能: 输入一个不带头节点的单向链表(链表的节点数小于100),删除链表中内容反复的节点(反复的节点所有删除),剩余的节点逆序倒排。输入: pstrIn: 输入一个不带头节点的单向链表输出: pstrOut:删除内容反复的节点后,逆序排列的链表(不带头节点,链表第一个节点的内存已经申请)。

Return: Demo sample: The content of the input list is 6,7,8,8,9,10,6, then the contents of the output list should be 10,9,7*/in turn/ * This code is still a bug when the input list is 88888888, pstrout use the reference best * /intIchanprocess (Strnode * Pstrin,strnode * pstrout) {if(Pstrin = = NULL | | pstrout = = NULL) {return-1; }//if map<int,int>Map; strnode* p = pstrin;//statistics repeated out times while(p) {if(Map.count (p->data) = =0) {Map.insert ( map<int,int>:: Value_type (P->data,1)); }//if Else{Map[p->data] = map[p->data]+1; }//elsep = p->pstrnext; }//while //For recurring reverse output to Pstroutp = Pstrin; vector<int>Vec while(p) {if(Map[p->data] = =1) {Vec.push_back (p->data); }//ifp = p->pstrnext; }//while intSize = Vec.size ();if(Size = =0){return 0; }//if for(inti =0; I < size-1; ++i) {strnode* node =NewStrnode (); Node->data = Vec[i]; Node->pstrnext = pstrout->pstrnext; Pstrout->pstrnext = node; }//forPstrout->data = vec[size-1];/*printf ("\ n"); strnode* P1 = pstrout; while (p1) {printf ("*%d*\n", p1->data); P1 = p1->pstrnext; }//while*/ return 0;}/ * Release linked list * /voidVfreechan (Strnode * pstrchan) {strnode* p = Pstrchan; while(p) {strnode* node = p; p = p->pstrnext; Free(node); }//while return;}

[Huawei Machine Test exercises]24. Delete repeated nodes in the linked list, reverse output of the remaining nodes

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.