关于链表所有操作,面试必考C++

  1 #include <iostream>
  2 #include <stack>
  3 using namespace std;
  4 //链表的结构体
  5 struct ListNode
  6 {
  7     int m_nValue;
  8     ListNode* m_pNext;
  9     ListNode()
 10     {
 11         m_pNext = NULL;
 12     }
 13 };
 14 //判断链表是否为空
 15 bool isEmpty(ListNode* list)
 16 {
 17     return list->m_pNext == NULL;
 18 }
 19 //判断position是否是最后一个
 20 bool isLast(ListNode* position, ListNode* list)
 21 {
 22     return position->m_pNext == NULL;
 23 }
 24 //在链表中找到某个元素
 25 ListNode* Find(int value, ListNode* list)
 26 {
 27     ListNode* pNode = list->m_pNext;//指向第一个结点,list为头结点,不存放数据
 28     while (pNode != NULL && pNode->m_nValue != value)
 29     {
 30         pNode = pNode->m_pNext;
 31     }
 32     return pNode;
 33 }
 34 //找到某一元素的前驱结点
 35 ListNode* FindPrevious(int value, ListNode* list)
 36 {
 37     //指向第一个结点
 38     ListNode* pNode = list;
 39     while (pNode->m_pNext != NULL && pNode->m_pNext->m_nValue != value)
 40     {
 41         pNode = pNode->m_pNext;
 42     }
 43     return pNode;
 44 }
 45 //删除某个结点
 46 void DeleteNode(int value, ListNode* list)
 47 {
 48     if (list == NULL)
 49     {
 50         return;
 51     }
 52     //要将删除的结点进行备份
 53     ListNode* pDeleteNode = NULL;
 54     ListNode* pNode = list;
 55     while (pNode->m_pNext != NULL && pNode->m_pNext->m_nValue != value)
 56     {
 57         pNode = pNode->m_pNext;
 58     }
 59     if (pNode->m_pNext != NULL)
 60     {
 61         pDeleteNode = pNode->m_pNext;//待删除的结点
 62         pNode->m_pNext = pDeleteNode->m_pNext;//待删除结点的下一个结点
 63         //释放待删除的结点
 64         delete pDeleteNode;
 65         pDeleteNode = NULL;
 66     }
 67     return;
 68 }
 69 //将一个元素插入到pToBeInsertNode指示的结点之后,元素的值为value;
 70 void InsertNode(int value, ListNode* list, ListNode* pToBeInsertNode)
 71 {
 72     if (list == NULL || pToBeInsertNode == NULL)
 73     {
 74         return;
 75     }
 76     ListNode* pNewNode = new ListNode();
 77     if (pNewNode == NULL)
 78     {
 79         return;
 80     }
 81     pNewNode->m_nValue = value;
 82     pNewNode->m_pNext = pToBeInsertNode->m_pNext;
 83     pToBeInsertNode->m_pNext = pNewNode;
 84 }
 85 
 86 //在末尾添加元素
 87 void AddToTail( ListNode* list, int value)
 88 {
 89     //建立一个新节点
 90     ListNode* pNewNode = new ListNode();
 91     if (pNewNode == NULL)
 92     {
 93         return;
 94     }
 95     pNewNode->m_nValue = value;
 96     pNewNode->m_pNext = NULL;
 97     if (list == NULL)
 98     {
 99         list = pNewNode;
100     }
101     else
102     {
103         ListNode* tmpNode = list;
104         while (tmpNode->m_pNext != NULL)
105         {
106             tmpNode = tmpNode->m_pNext;
107         }
108         tmpNode->m_pNext = pNewNode;//最后一个的下一个结点指向新节点
109     }
110 }
111 //删除整个链表  
112 void DeleteList(ListNode*list) {
113     if (list == NULL)
114     {
115         return;
116     }
117     ListNode*pNode = list->m_pNext;
118     ListNode*pTemp = NULL;
119 
120     list->m_pNext = NULL; //断开头结点  
121     while (pNode != NULL)
122     {
123         pTemp = pNode->m_pNext;//需要保存其下一个节点  
124         delete pNode;
125         pNode = pTemp;        //移到下一个节点  
126     }
127 }
128 
129 //打印链表
130 void printList(ListNode* list)
131 {
132     if (list == NULL)
133     {
134         return;
135     }
136     ListNode* pNode = list->m_pNext;
137     while (pNode != NULL)
138     {
139         cout << pNode->m_nValue << " ";
140         pNode = pNode->m_pNext;
141     }
142     cout << endl;
143 }
144 //逆序打印链表
145 void printListReversingly(ListNode* list)
146 {
147     stack<ListNode*> nodes;
148     ListNode* pNode = list->m_pNext;
149     while (pNode != NULL)
150     {
151         nodes.push(pNode);//压栈
152         pNode = pNode->m_pNext;
153     }
154     while (!nodes.empty())
155     {
156         ListNode* pTmp = nodes.top();//获取顶端元素
157         cout << pTmp->m_nValue << " ";
158         nodes.pop();//弹出
159     }
160     cout << endl;
161 }
162 //判断一个链表是否有环
163 bool LinkListLoop(ListNode* pHead)
164 {
165     ListNode* p = pHead;
166     ListNode* q = pHead;
167     while (p != NULL && q != NULL)
168     {
169         p = p->m_pNext;//p走一步;
170         q = q->m_pNext;
171         if (q->m_pNext != NULL)
172             q = q->m_pNext;//q走两步;
173         if (p != NULL && p == q)
174         {
175             return true;
176         }
177     }
178     return false;
179 }
180 //测试  
181 int main() {
182     ListNode* list = new ListNode();
183     for (int i = 0; i < 10; i++)
184     {
185         AddToTail(list, i);
186     }
187     cout << "打印链表:" << endl;
188     printList(list);
189 
190     cout << "逆序打印链表:" << endl;
191     printListReversingly(list);
192 
193     cout << "在末尾添加元素:10" << endl;
194     AddToTail(list, 10);
195     printList(list);
196 
197     cout << "移除节点值为5的节点:" << endl;
198     DeleteNode(5, list);
199     printList(list);
200 
201     cout << "找到节点值为6的节点的前驱节点:" << endl;
202     ListNode* pNode = FindPrevious(6, list);
203     cout << pNode->m_nValue << endl;
204 
205     cout << "删除链表:" << endl;
206     DeleteList(list);
207     printList(list);
208 
209     cout << "链表是否为空:" << endl;
210     cout << isEmpty(list) << endl;
211     system("pause");
212     return 0;
213 }

 

posted @ 2017-08-17 17:54  繁星的夜空2012  阅读(332)  评论(0编辑  收藏  举报