剑指offer——链表

 1 #include"stdio.h"
 2 #include"stdlib.h"
 3 #include"iostream"
 4 using namespace std;
 5 
 6 struct ListNode
 7 {
 8     int m_Value;
 9     ListNode* m_pNext;
10 };
11 
12 ListNode* CreateListNode(int value)
13 {
14     ListNode* pNode=new ListNode();
15     pNode->m_Value=value;
16     pNode->m_pNext=nullptr;
17 
18     return pNode;
19 }
20 
21 void ConnectListNodes(ListNode* pCurrent,ListNode* pNext)
22 {
23     if(pCurrent==nullptr)
24     {
25         cout<<"Error to connect two nodes."<<endl;
26         exit(1);
27     }
28     pCurrent->pNext=pNext;
29 }
30 
31 void PrintListNode(ListNode* pNode)
32 {
33     if(pNode==nullptr)
34     {
35         cout<<"The node is nullptr."<<endl;
36     }
37     else
38     {
39         cout<<"the value in node is: "<<pNode->m_Value<<endl;
40     }
41 }
42 
43 void PrintList(ListNode *pHead)
44 {
45     cout<<"PrintList starts."<<endl;
46 
47     ListNode* pNode=pHead;
48     while(pNode!=nullptr)
49     {
50         cout<<pNode->m_Value<<" ";
51         pNode=pNode->m_pNext;
52     }
53     cout<<endl<<"PrintList ends."<<endl;
54 }
55 
56 void DestroyList(ListNode* pHead)
57 {
58     ListNode* pNode=pHead;
59     while(pNode!=nullptr)
60     {
61         pHead=pHead->m_pNext;
62         delete pNode;
63         pNode=pHead;
64     }
65 }
66 
67 void AddToTail(ListNode** pHead,int value)
68 {
69     ListNode* pNode=CreateListNode(value);
70     if(pHead==nullptr || *pHead==nullptr)
71     {
72         *pHead=pNode;
73     }
74     else
75     {
76         ListNode* pTemp=*pHead;
77         while(pTemp->m_pNext!=nullptr)
78             pTemp=pTemp->m_pNext;
79         pTemp->m_pNext=pNode;
80     }
81 }
82 
83 void RemoveNode(ListNode** pHead,int value)
84 {
85     if(pHead==nullptr || *pHead==nullptr)
86         return;
87 
88 }
List.h

猜你喜欢

转载自www.cnblogs.com/acm-jing/p/10407474.html