zoukankan      html  css  js  c++  java
  • 对单链表的一些操作

      1 #include "stdafx.h"
      2 #include<string>
      3 #include<iostream>
      4 #include<stack>
      5 using namespace std;
      6 
      7 struct ListNode
      8 {   
      9     //结点类型
     10     int m_nValue;
     11     ListNode* m_pNext;
     12 };
     13 
     14 void AddToTail(ListNode** pHead,int value)
     15 {
     16     //尾插法
     17     ListNode* pNew = new ListNode();
     18     pNew->m_nValue = value;
     19     pNew->m_pNext = NULL;
     20     if(*pHead==NULL)
     21     {
     22         *pHead = pNew;
     23     }else
     24     {
     25         ListNode *pNode = *pHead;
     26         while(pNode->m_pNext !=NULL)
     27         {
     28             pNode =pNode->m_pNext;
     29         }
     30         pNode ->m_pNext = pNew;
     31     }
     32 }
     33 
     34 void AddToHead(ListNode** pHead,int value)
     35 {
     36     //头插法
     37     ListNode *pNew = new ListNode;
     38     pNew->m_nValue = value;
     39     pNew->m_pNext = NULL;
     40 
     41     if(*pHead==NULL)
     42     {
     43         *pHead =pNew;
     44     }
     45     else
     46     {
     47         ListNode *Head = *pHead;
     48 
     49         pNew->m_pNext = Head;
     50         (*pHead) = pNew;
     51 
     52     }
     53 }
     54 
     55 void showNode(const ListNode *Head)
     56 {
     57     int count = 0;
     58     while(Head!=NULL)
     59     {
     60         cout<<Head->m_nValue<<"  ";
     61         Head=Head->m_pNext;
     62         count++;
     63     }
     64     cout<<endl<<"元素个数:"<<count<<endl;;
     65 
     66 }
     67 
     68 void InsertNodeByArray(int *arr,int length,ListNode** Head)
     69 {
     70     //通过数组进行插入元素
     71     for(int i =0;i!=length;++i)
     72     {
     73         AddToTail(Head,arr[i]);
     74     }
     75 }
     76 
     77 void RemoveNode(ListNode** Head,int value)
     78 {
     79     //删除元素内容是value的所有结点,注意第一个元素和最后一个元素
     80     if(*Head==NULL)
     81     {
     82         cout<<"Can not Find the value:"<<value<<endl;
     83         return;
     84     }
     85     ListNode *pHead = *Head;
     86     ListNode *pDelete;
     87     if(pHead->m_nValue==value)
     88     {
     89         pDelete = *Head;
     90         (*Head)=pDelete ->m_pNext;
     91         delete pDelete;
     92         pDelete = NULL;
     93         return;
     94     }
     95     else
     96     {
     97         while(pHead->m_pNext!=NULL){
     98             while(pHead->m_pNext!=NULL&&pHead->m_pNext->m_nValue!=value)
     99             {
    100                 pHead=pHead->m_pNext;
    101             }
    102 
    103             if(pHead->m_pNext!=NULL&&pHead->m_pNext->m_nValue==value)
    104             {
    105                 cout<<"Find The Node,The value is:"<<value<<endl;
    106                 pDelete =pHead->m_pNext;
    107                 if(pDelete->m_pNext!=NULL){
    108                     pHead->m_pNext=pDelete->m_pNext;
    109                     pHead=pHead->m_pNext;
    110                     delete pDelete;
    111                     pDelete = NULL;
    112                 }
    113                 else
    114                 {
    115                     pHead->m_pNext=NULL;
    116                     delete pDelete;
    117                     pDelete = NULL;
    118                     return;
    119                 }
    120             }
    121         }
    122     }
    123 }
    124 
    125 
    126 void resverseList(ListNode *Node)
    127 {
    128     if(Node!=NULL){
    129     if(Node->m_pNext!=NULL)
    130     {
    131         resverseList(Node->m_pNext);
    132     }
    133     cout<<Node->m_nValue<<"  ";
    134     }
    135     //递归实现反向遍历链表
    136 }
    137 
    138 void resverseList_Stack(ListNode *Node)
    139 {
    140     //用stack逆向输出.
    141     stack<ListNode*> sNode;
    142     ListNode *pNode = Node;
    143     while(pNode!=NULL)
    144     {
    145         sNode.push(pNode);
    146         pNode=pNode->m_pNext;
    147     }
    148 
    149     while(!sNode.empty())
    150     {
    151         cout<< sNode.top()->m_nValue<<"  ";
    152         sNode.pop();
    153     }
    154     //递归实现反向遍历链表
    155 }
    156 
    157 
    158 int _tmain(int argc, _TCHAR* argv[])
    159 {
    160     const int len = 3;
    161     ListNode *Head=NULL;
    162     int a[len]={1,2,3};
    163     InsertNodeByArray(a,len,&Head);
    164     showNode(Head);
    165     RemoveNode(&Head,1);
    166     showNode(Head);
    167     resverseList(Head);
    168     cout<<endl;
    169     resverseList_Stack(Head);
    170     return 0;
    171 }
  • 相关阅读:
    用纯css画个三角形
    宝塔安装
    js判断浏览器版本
    JS打开url的几种方法
    java加密算法
    Mysql数据库多对多关系未建新表
    数据库唯一性约束异常插入处理
    HTML5中localStorage的使用
    软件设计师14-UML建模
    数据库设计流程
  • 原文地址:https://www.cnblogs.com/crazycodehzp/p/3960182.html
Copyright © 2011-2022 走看看