zoukankan      html  css  js  c++  java
  • 非递减单链表删除重复元素 转

        typedef struct _MyListNode  
        {  
            int nData;  
            struct _MyListNode * pNext;  
        } MyListNode, *PMyListNode;  
          
          
        class MyList  
        {  
        public:  
            MyList()  
            {  
                pHead = new MyListNode();  
                pHead->nData = 0;  
                pHead->pNext = NULL;  
                pTail = NULL;  
            }  
            ~MyList()  
            {  
                MyListNode * pTmp = pHead;  
                while (pTmp != NULL)  
                {  
                    pHead = pTmp->pNext;  
                    delete pTmp;  
                    pTmp = pHead;  
                }  
          
                pHead = NULL;  
            }  
          
            void Append(int num)  
            {  
                MyListNode * pTmp = new MyListNode();  
                pTmp->nData = num;  
                pTmp->pNext = NULL;  
          
                if (pTail != NULL)  
                {  
                    pTail->pNext = pTmp;  
                }  
                else  
                {  
                    pHead->pNext = pTmp;  
                }  
                pTail = pTmp;  
            }  
          
            void Print()  
            {  
                MyListNode * pTmp = pHead->pNext;  
                while (pTmp != NULL)  
                {  
                    cout << pTmp->nData << " ";  
                    pTmp = pTmp->pNext;  
                }  
                cout << endl;  
            }  
          
            void RemoveRepeat()  
            {  
                MyListNode * pNode = pHead->pNext;  
                while (pNode != NULL)  
                {  
                    MyListNode * pTmp = pNode->pNext;  
                    while (pTmp != NULL && pNode->nData == pTmp->nData)  
                    {  
                        pNode->pNext = pTmp->pNext;  
                        delete pTmp;  
                        pTmp = pNode->pNext;  
                    }  
          
                    pNode = pTmp;  
                }  
            }  
        private:  
            MyListNode * pHead;  
            MyListNode * pTail;  
          
        };  
          
          
        int main()  
        {  
            MyList lst;  
            lst.Append(7);  
            lst.Append(10);  
            lst.Append(10);  
            lst.Append(21);  
            lst.Append(30);  
            lst.Append(42);  
            lst.Append(42);  
            lst.Append(42);  
            lst.Append(51);  
            lst.Append(70);  
          
            lst.Print();  
          
            lst.RemoveRepeat();  
            lst.Print();  
          
            return 0;  
        } 

    http://blog.csdn.net/matrixcl/article/details/6853458

  • 相关阅读:
    jmeter 工具学习 未完待续
    测试学习
    JavaScript
    新概念第1册
    day08
    LeetCode OJ:Invert Binary Tree(反转二叉树)
    LeetCode OJ:Lowest Common Ancestor of a Binary Search Tree(最浅的公共祖先)
    LeetCode OJ:Maximum Depth of Binary Tree(二叉树最大深度)
    LeetCode OJ:Path Sum(路径之和)
    LeetCode OJ:Symmetric Tree(对称的树)
  • 原文地址:https://www.cnblogs.com/androllen/p/2843689.html
Copyright © 2011-2022 走看看