**:
#include
#include
#include
using namespace std;
//單鏈表結構體
typedef struct _linknode _linknode;
//首節點
bool linkinit(_linknode *&l)
l->data = -1;
l->next = null;
//前插法
bool linkinsert_front(_linknode *&l, const int &data)
_linknode *node = new _linknode;
if (!node)
node->data = data;
node->next = l->next;
l->next = node;
return true;
//後插法
bool linkinsert_back(_linknode *&l, const int &data)
_linknode *last = l;
while (last->next)
_linknode *node = new _linknode;
if (!node)
node->data = data;
node->next = null;
last->next = node;
return true;
//任意位置插入
//i:代表節點
bool linkinsert(_linknode *&l, const int &i, const int &data)
_linknode *p = l;
for (int j = 0; p && j < i - 1; j++)
if (!p)
_linknode *node = new _linknode;
if (!node)
node->data = data;
node->next = p->next;
p->next = node;
return true;
//通過節點查詢資料
bool link_getelem(_linknode *&l, const int &i, int &data)
_linknode *p = l->next;
data = 0;
for (int j = 0; p && j < i - 1; j++)
if (!p)
data = p->data;
return true;
//通過資料查詢節點
bool link_getnode(_linknode *&l, const int &data, int &i)
_linknode *p = l->next;
int index = 1;
//節點先初始化1
i = 1;
while (p && p->data != data)
if (!p)
i = index;
return true;
//刪除節點
bool linkdelete(_linknode *&l, const int &i)
//判斷的上乙個節點
_linknode *p = l;
//刪除的節點
_linknode *q = null;
for (int j = 0; p->next && j < i - 1; j++)
if (!p->next)
//刪除的節點
q = p->next;
p->next = q->next;
delete q;
return true;
//歸還空間
bool linkdestory(_linknode *&l)
string line(45, '-');
_linknode *p = l;
//最後l也為nulla
cout << line << "歸還空間!" << line << endl << endl;
while (p)
//輸出節點
bool linkprint(_linknode *&l)
_linknode *p = l->next;
printf("\n所有元素內容為:");
while (p)
cout << endl << endl;
//主函式
int main(void)
else
}//輸出
linkprint(l);
printf("《後插法》 請輸入需要新增的節點個數:");
scanf_s("%d", &n);
for (int i = 0; i < n; i++)
else
}//輸出
linkprint(l);
printf("《任意位置插法》 請輸入需要插入的節點 和 節點資料");
scanf_s("%d%d", &n,&data);
if (linkinsert(l, n, data))
else
//輸出
linkprint(l);
printf("《通過節點查詢資料》 請輸入需要查詢的節點:");
scanf_s("%d", &n);
if (link_getelem(l, n, data))
else
//輸出
linkprint(l);
printf("《通過資料查詢節點》 請輸入需要查詢的資料:");
scanf_s("%d", &data);
if (link_getnode(l, data, n))
else
//輸出
linkprint(l);
printf("《刪除節點》 請輸入需要刪除的節點:");
scanf_s("%d", &n);
if (linkdelete(l, n))
else
//輸出
linkprint(l);
//歸還空間
linkdestory(l);
system("pause");
return 0;
單鏈表的c 實現
node類標頭檔案 ifndef node h define node h include include using namespace std class node endif node類cpp include node.h using namespace std 過載 運算子 ostream ...
單鏈表的C 實現
include using namespace std struct node class list void insertlist int adata,int bdata void deletelist int adata void outputlist node gethead void lis...
C 單鏈表的實現
include include include include using namespace std typedef struct student node node create 建立單鏈表 else cycle 0 head head next p next null coutreturn h...