單鏈表的簡單實踐

2021-09-01 08:24:47 字數 993 閱讀 4661

1.演算法描述

資料結構與演算法分析c++版:3.11

實現乙個有序單鏈表,要求能返回鍊錶大小,列印鍊錶,檢測x是否在鍊錶(在則刪除,否則新增)

2.實現

list.h

#ifndef list_h

#define list_h

#include templatestruct node;

templateclass list

list(t a, int n)

return length;

} //列印鍊錶

void printlist()

std::cout

bool b = true;

while(r && r->data != x)

if(!r)

return b;

} //新增或刪除存在元素(存在則刪除,不存在則新增)

void findaddordelete(t x)

if(!r)else

}}

//有序插入元素

void insertdata(t x)

p->next = r;

r->next = t;

len++;

} private:

node* first;

int len;//儲存鍊錶長度

void release()

first->next = null;

} };#endif

main.cpp

#include #include "list.h"

using namespace std;

int main( );

listl(a, 11);

cout<

難度沒什麼,主要是考察鍊錶的基本操作,邊界檢查,尤其是使用指標的時候尤其注意,指標檢查!

4.刪除

實踐之單鏈表

define crt secure no warnings include include include include linklist.h using namespace std 自定義資料型別 typedef struct person person 列印函式 void myprint vo...

簡單的單鏈表

下面介紹一下單鏈表,給出了鍊錶元素的插入,刪除,顯示函式,並且給出明確注釋。include include using namespace std struct node void pushelement node rootnode,int data void popelement node roo...

簡單的單鏈表

description 定義乙個單鏈表 auther eleven create 2020 04 03 21 33 public class node 鍊錶新增元素的方法 用於在鍊錶後新增元素 node node 當當前節點的下乙個節點不為空時,將下乙個節點賦值給到當前節點 currentnode ...