日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

有趣的链表相关题型

發布時間:2023/11/30 编程问答 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 有趣的链表相关题型 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

鏈表:也是線性表的一種。形象的來說:


就像火車的一個個車廂一樣,一個個的鏈起來的。它有一個特點:它的頭沒有前驅,尾沒有后繼。

為什么會引入鏈表這個概念呢?之前我們知道的順序表,是用數組的形式保存數據的。它使用起來也非常方便,優點在于它的尾刪尾插非常方便,直接將數組空間大小加1,給最后一個數組賦值就ok了。但是對于數組來說,也有一定的缺點。比如:如果在中間插入或刪除的話,就得移動一定得位數才可進行相關操作。最壞得情況是你在頭插頭刪的時候,時間的消耗更巨大。為了避免這種重復的移位操作,我們引入了鏈表這個概念。鏈表的頭插頭刪就容易多了,只需修改頭節點的位置就好啦。

鏈表的種類很多,有單鏈表、雙向鏈表、循環鏈表等。今天我們主要講單鏈表。

那么鏈表的結構是怎么樣的呢?

typedef int DataType;

struct Node
{
DataType?data; ? ? ? ? ? ? //節點里存放的數據
struct Node* next; ? ? ? ? //指向下一個節點的指針
};

下面我們來一一實現它們吧~

//SList.h #pragma once #include<iostream> using namespace std; typedef int DataType; typedef struct Node {int data;struct Node* next; }Node,*pNode;void InitSlist(pNode *pHead);void PushBack(pNode *pHead,DataType data); void PopBack(pNode *pHead);void PushFront(pNode *pHead,DataType data); void PopFront(pNode *pHead); pNode BuyNode(DataType data); pNode Find(pNode pHead, DataType data); void Insert(pNode pos, DataType data); //某個位置插入datavoid Erase(pNode* pHead, pNode pos); //刪除位置節點 void Remove(pNode* pHead, DataType data); //刪除某個數據的節點 void RemoveAll(pNode* pHead, DataType data); //刪除所有數據為data的節點 void Destroy(pNode* pHead); //銷毀 int Empty(pNode pHead); //判空 int Size(pNode pHead); //節點數目 //SList.cpp #include"SList.h" #include<assert.h>void InitSlist(pNode *pHead) {*pHead = NULL; }pNode BuyNode(DataType data) //創建一個新節點 {pNode newNode = (pNode)malloc(sizeof(Node));assert(newNode);newNode->data = data;newNode->next = NULL;return newNode; }void PushBack(pNode *pHead,DataType data) {if(*pHead == NULL){*pHead = BuyNode(data);(*pHead)->next = NULL;}else{pNode pCur = *pHead;while(pCur->next){pCur = pCur->next;}pCur->next = BuyNode(data);} }void PopBack(pNode *pHead) {if(*pHead == NULL)return;if((*pHead)->next == NULL){free(*pHead);*pHead = NULL;}else{pNode pCur = *pHead;pNode pPrev = NULL;while(pCur->next){pPrev = pCur;pCur = pCur->next;}free(pCur);pPrev->next = NULL;} }void PushFront(pNode *pHead,DataType data) {if(*pHead == NULL)*pHead = BuyNode(data);else{pNode cur = BuyNode(data);cur->next = *pHead;*pHead = cur;} }void PopFront(pNode *pHead) {if(*pHead == NULL)return;else{pNode pDel = *pHead;*pHead = (*pHead)->next;free(pDel);} }pNode Find(pNode pHead, DataType data) {pNode pCur = pHead;while(pCur){if(pCur->data == data){return pCur;}pCur = pCur->next;}return NULL; }void Insert(pNode pos, DataType data) {pNode newNode = BuyNode(data);if(pos == NULL)return;newNode->next = pos->next;pos->next = newNode; }void Erase(pNode* pHead, pNode pos) {pNode pCur = *pHead;if(*pHead == pos && (*pHead)->next == NULL){free(pos);*pHead = NULL;return;}while(pCur){pNode pPrev = pCur;while(pPrev->next == pos){pNode pDel = pos;pPrev->next = pos->next;free(pDel);return;}pCur = pCur->next;}return; }void Remove(pNode* pHead, DataType data) {pNode pos = Find(*pHead,data);Erase(pHead,pos); }void Destroy(pNode* pHead) {while(*pHead){pNode pDel = *pHead;*pHead = (*pHead)->next;free(pDel);}return; }int Empty(pNode pHead) {return pHead == NULL; }int Size(pNode pHead) {pNode pCur = pHead;int count = 0;if(pHead == NULL)return 0;while(pCur){count++;pCur = pCur->next;}return count; }void Print(pNode pHead) {pNode pCur = pHead;while(pCur){cout<<pCur->data<<"->";pCur = pCur->next;}cout<<"NULL"<<endl; }
對于單鏈表,我們還有一些有趣的題來和大家分享哦。

有趣的鏈表問題>>點擊進入嘍


總結

以上是生活随笔為你收集整理的有趣的链表相关题型的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。