1 #include "stdafx.h" 2 #include3 #include 4 #include 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< m_nValue<<" "; 61 Head=Head->m_pNext; 62 count++; 63 } 64 cout< <<"元素个数:"< < 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:"< < 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 else114 {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< m_nValue<<" ";134 }135 //递归实现反向遍历链表136 }137 138 void resverseList_Stack(ListNode *Node)139 {140 //用stack逆向输出.141 stack 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<