劍指offer之C++語言實現鏈表(兩種刪除節點方式)

今天小編就爲大家分享一篇關於劍指offer之C++語言實現鏈表(兩種刪除節點方式),小編覺得內容挺不錯的,現在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧

1 問題

用C++語言實現鏈表

2 代碼實現

#include <iostream>
#include <stdlib.h>
using namespace std;
class List
{
public:
 List();
 ~List();
 List* createNode(int value);//創建節點
 bool insertNode(List *node);//插入節點
 void printList();//打印節點
 bool deleteNode(List *node);//刪除節點不移動頭節點
 bool deleteNode1(List *node);//刪除節點移動頭節點
 int listSize();//長度
 void printNode();//打印但前的value
 void freeList();//釋放鏈表
private:
 int value;
 List *head;
 List *next;
};
bool List::deleteNode(List *node)
{
 if (node == NULL)
 {
 std:cout << "node is NULL" << std::endl; 
 return false;
 }
 if (head == NULL)
 {
 std::cout << "head is NULL" << std::endl; 
 return false;
 }
 //如果node等於head
 if (head == node)
 {
 head = head->next;
 }
 List *p = head;
 while (p->next != NULL)
 {
 if (p->next == node)
 {
  p->next = p->next->next;
  return true;
 }
 p = p->next;
 }
 return false; 
}
bool List::deleteNode1(List *node)
{
 if (node == NULL)
 {
 std:cout << "node is NULL" << std::endl; 
 return false;
 }
 if (head == NULL)
 {
 std::cout << "head is NULL" << std::endl; 
 return false;
 }
 //如果node等於head
 if (head == node)
 {
 head = head->next;
 }
 List *p = head;
 while (head->next != NULL)
 {
 if (head->next == node)
 {
  head->next = head->next->next;
  std::cout << "delete node success head->value" << head->value << std::endl;
  //這裏要記得把頭節點的指針移動最後還原,這裏的頭節點是保存在這個類裏面,改變了就是改變了
  //如果這裏是把head作爲參數傳遞,最後head會被銷燬那麼不需要移動頭指針
  head = p;
  return true;
 }
 //注意,這裏由於head是成員變量,改變了就是改變了,所以需要最後重新指定
 head = head->next;
 }
 std::cout << "delete node fail head->value" << head->value << std::endl;
 //這裏要記得把頭節點的指針移動最後還原,這裏的頭節點是保存在這個類裏面,改變了就是改變了
 //如果這裏是把head作爲參數傳遞,最後head會被銷燬那麼不需要移動頭指針
 head = p;
 return false; 
}
List::List()
{
 value = 0;
 head = NULL;
 next = NULL;
}
List::~List()
{
 delete head;
 delete next;
}
List* List::createNode(int value)
{
 List *list = NULL;
 list = new List();
 if (list)
 {
 list->value = value;
 return list; 
 }
 return NULL;
}
bool List::insertNode(List *node)
{
 node->next = head;
 head = node;
 return true; 
}
void List::printList()
{ if (head == NULL)
 {
 std::cout << "head is NULL" << std::endl;
 return;
 }
 List *p = head;
 while (p != NULL)
 {
 std::cout << p->value << std::endl; 
 p = p->next;
 }
 return; 
}
void List::printNode()
{
 std::cout << value << std::endl; 
}
int List::listSize()
{
 if (head == NULL)
 {
 std::cout << "head is NULL" << std::endl;
 return 0; 
 }
 int len = 0;
 List *p = head;
 while (p != NULL)
 {
 p = p->next;
 ++len;
 }
 return len;
}
void List::freeList()
{
 if (head == NULL)
 {
 std::cout << "head is NULL" << std::endl;
 return; 
 }
 List *p;
 while (head != NULL)
 {
 p = head;
 head = head->next;
 free(p);
 }
}
int main()
{
 List list;
 List *list1 = list.createNode(5);
 list.insertNode(list1);
 List *list2 = list.createNode(6);
 list.insertNode(list2);
 List *list3 = list.createNode(1);
 list.insertNode(list3);
 List *list4 = list.createNode(3);
 list.insertNode(list4);
 List *list5 = list.createNode(2);
 list.insertNode(list5);
 list.printList();
 std::cout << "list size is " << list.listSize() << std::endl;
 std::cout << "-----------開始刪除節點值爲3的節點" << std::endl;
 list.deleteNode1(list4);
 list.printList();
 std::cout << "list size is " << list.listSize() << std::endl;
 list.freeList();
 list.printList();
 return 0; 
}

3 運行結果

2
3
1
6
5
list size is 5
-----------開始刪除節點值爲3的節點
delete node success head->value2
2
1
6
5
list size is 4
head is NULL

4 小結

很明顯用C語言實現,我們習慣在外面搞個頭結點,然後用C++實現,我們直接在類的裏面放一個head指針,然後我們在增加節點的時候我們會把head進行移動,放在最前面,所以後面的 便利和刪除操作等最好是不要動head的位置了,因爲head動了,下次便利就有問題,如果刪除函數移動了head,我們最後需要復原head

比如我們的頭指針儘量不要移動,我們可以用一個指針變量來保存這個head指針,然後我們移動保存的指針變量,同時把保存的指針變量在一些情況下改變下一個指向的指針,那麼我們下次便利head也是生效的,這樣保證了頭指針不被污染

比如下面的例子

#include <stdio.h>
void change(char *a)
{
 *(a + 1) = 's';
}
int main()
{
 char value[10] = "chenyu";
 change(value);
 printf("value is %s\n", value);
 return 0;
}
#include <stdio.h>
void change(char *a)
{
 char *p = a;
 *(p + 1) = 's';
}
int main()
{
 char value[10] = "chenyu";
 change(value);
 printf("value is %s\n", value);
 return 0;
}

其實最後的結果都是一樣,csenyu

頭指針是指向這塊內存的地址,如果我們用指針變量保存了,然後這個指針變量也指向了這裏,用指針變量去操作後,然後頭指針也是指向這裏,後面數據的指向也會改變

總結

以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,謝謝大家對神馬文庫的支持。如果你想了解更多相關內容請查看下面相關鏈接

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章