溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

劍指offer之C++語言實(shí)現(xiàn)鏈表(兩種刪除節(jié)點(diǎn)方式)

發(fā)布時(shí)間:2020-10-01 08:48:43 來源:腳本之家 閱讀:116 作者:chenyu_insist 欄目:編程語言

1 問題

用C++語言實(shí)現(xiàn)鏈表

2 代碼實(shí)現(xiàn)

#include <iostream>
#include <stdlib.h>
using namespace std;
class List
{
public:
 List();
 ~List();
 List* createNode(int value);//創(chuàng)建節(jié)點(diǎn)
 bool insertNode(List *node);//插入節(jié)點(diǎn)
 void printList();//打印節(jié)點(diǎn)
 bool deleteNode(List *node);//刪除節(jié)點(diǎn)不移動(dòng)頭節(jié)點(diǎn)
 bool deleteNode1(List *node);//刪除節(jié)點(diǎn)移動(dòng)頭節(jié)點(diǎn)
 int listSize();//長(zhǎng)度
 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;
  //這里要記得把頭節(jié)點(diǎn)的指針移動(dòng)最后還原,這里的頭節(jié)點(diǎn)是保存在這個(gè)類里面,改變了就是改變了
  //如果這里是把head作為參數(shù)傳遞,最后head會(huì)被銷毀那么不需要移動(dòng)頭指針
  head = p;
  return true;
 }
 //注意,這里由于head是成員變量,改變了就是改變了,所以需要最后重新指定
 head = head->next;
 }
 std::cout << "delete node fail head->value" << head->value << std::endl;
 //這里要記得把頭節(jié)點(diǎn)的指針移動(dòng)最后還原,這里的頭節(jié)點(diǎn)是保存在這個(gè)類里面,改變了就是改變了
 //如果這里是把head作為參數(shù)傳遞,最后head會(huì)被銷毀那么不需要移動(dòng)頭指針
 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 << "-----------開始刪除節(jié)點(diǎn)值為3的節(jié)點(diǎn)" << std::endl;
 list.deleteNode1(list4);
 list.printList();
 std::cout << "list size is " << list.listSize() << std::endl;
 list.freeList();
 list.printList();
 return 0; 
}

3 運(yùn)行結(jié)果

2
3
1
6
5
list size is 5
-----------開始刪除節(jié)點(diǎn)值為3的節(jié)點(diǎn)
delete node success head->value2
2
1
6
5
list size is 4
head is NULL

4 小結(jié)

很明顯用C語言實(shí)現(xiàn),我們習(xí)慣在外面搞個(gè)頭結(jié)點(diǎn),然后用C++實(shí)現(xiàn),我們直接在類的里面放一個(gè)head指針,然后我們?cè)谠黾庸?jié)點(diǎn)的時(shí)候我們會(huì)把head進(jìn)行移動(dòng),放在最前面,所以后面的 便利和刪除操作等最好是不要?jiǎng)觝ead的位置了,因?yàn)閔ead動(dòng)了,下次便利就有問題,如果刪除函數(shù)移動(dòng)了head,我們最后需要復(fù)原h(huán)ead

比如我們的頭指針盡量不要移動(dòng),我們可以用一個(gè)指針變量來保存這個(gè)head指針,然后我們移動(dòng)保存的指針變量,同時(shí)把保存的指針變量在一些情況下改變下一個(gè)指向的指針,那么我們下次便利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;
}

其實(shí)最后的結(jié)果都是一樣,csenyu

頭指針是指向這塊內(nèi)存的地址,如果我們用指針變量保存了,然后這個(gè)指針變量也指向了這里,用指針變量去操作后,然后頭指針也是指向這里,后面數(shù)據(jù)的指向也會(huì)改變

總結(jié)

以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對(duì)億速云的支持。如果你想了解更多相關(guān)內(nèi)容請(qǐng)查看下面相關(guān)鏈接

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI