鍊錶的節本操作

2021-08-18 12:57:21 字數 2479 閱讀 2145

鍊錶的基本操作

鍊錶是一種鏈式儲存結構,鏈式儲存結構的特點是

用一組任意的儲存單元儲存資料元素

。為了能正確表示資料元素之間的線性關係,需引入結點概念。乙個結點表示鍊錶中的乙個資料元素,節點中除了儲存資料元素的資訊, 還必須存放指向下乙個節點的的指標(單、雙鏈表的最後乙個節點除外,它們儲存的是乙個空指標null)。以下是對鍊錶的一些基本操作的**實現

//標頭檔案

slist.h

#define _crt_secure_no_warnings 1

#include

#include

#include

typedef int datatype;

typedef struct node

node, *pnode;

void slistinit(pnode* phead);

pnode buyslistnode(datatype data);

void slistpushback(pnode* phead, datatype data);

void slistpopback(pnode* phead);

void slistpushfront(pnode* phead, datatype data);

void slistpopfront(pnode* phead);

pnode slistfind(pnode phead, datatype data);

void slistinsert(pnode* phead, pnode pos, datatype data);

void slisterase(pnode* phead, pnode pos);

void slistdestroy(pnode* phead);

int slistsize(pnode phead);

void slistclear(pnode* phead);

pnode slistback(pnode phead);

//原始檔

slist.c

#include"slist.h"

// 鍊錶初始化

void slistinit(pnode* phead)

//獲取新節點

pnode buyslistnode(datatype data)

return node;

}//尾插

void slistpushback(pnode* phead, datatype data)

pnode pcur = * phead;

while (pcur->_pnext)

pcur->_pnext = newnode;

}// 尾刪 

void slistpopback(pnode* phead)

else if (null == (*phead)->_pnext)

else

free(pcur);

prev->_pnext = null;}}

// 頭插 

void slistpushfront(pnode* phead, datatype data)

else}}

//頭刪

void slistpopfront(pnode* phead)

pnode pdel = *phead;

*phead = (*phead)->_pnext;

free(pdel);

}// 查詢值為data的結點,返回該結點在鍊錶中的位置 

pnode slistfind(pnode phead, datatype data)

pnode pcur = phead;

while (pcur)

pcur = pcur->_pnext;

}return null;

}// 在鍊錶pos位置後插入結點data 

void slistinsert(pnode* phead, pnode pos, datatype data)}}

// 刪除鍊錶pos位置上的結點 

void slisterase(pnode* phead, pnode pos)

else if ((*phead == pos))

else

pcur->_pnext = pos->_pnext;

free(pos);}}

// 銷毀單鏈表

void slistdestroy(pnode* phead)

}//求煉表中結點的個數

int slistsize(pnode phead)

return count;

}// 將鍊錶中的結點清空 

void slistclear(pnode* phead)

(*phead)->_pnext = null;

}// 獲取鍊錶中的最後乙個結點,返回該結點的位址 

pnode slistback(pnode phead)

else

return phead;}}

鍊錶的操作

鍊錶是資料結構中的乙個重要組成部分,對鍊錶操作的熟練度怎麼要求都不過分。只有部分核心 主要內容 1 鍊錶的建立 2 鍊錶的釋放 3 鍊錶的查詢 4 鍊錶中節點的插入 5 鍊錶中節點的刪除 6 鍊錶的反轉 7 兩個鍊錶的連線 define max 15 節點宣告 struct list typedef...

鍊錶的操作

結點0為頭結點,不儲存有意義的資料 從結點1開始儲存有意義的資料 include include includetypedef struct node node,pnode pnode create list 建立乙個新鍊錶 void show list pnode 遍歷顯示鍊錶 void add ...

鍊錶的操作

鍊錶是面試中常考的型別,因為只有幾行就可以了。下面是一些鍊錶 keshan.cpp 定義控制台應用程式的入口點。include stdafx.h include include define null 0 define len sizeof struct node struct node 列印鍊錶 ...