鏈表是一種複雜的數據結構,其數據之間的相互關係使鏈表分成三種:單鏈表、循環鏈表、雙向鏈表,這篇文章介紹單鏈表的實現。
單鏈表有一個頭節點head,指向鏈表在內存的首地址。鏈表中的每一個節點的數據類型爲結構體類型,節點有兩個成員:數據成員(實際需要保存的數據)和指向下一個結構體類型節點的指針即下一個節點的地址(事實上,此單鏈表是用於存放整型數據的動態數組)。鏈表按此結構對各節點的訪問需從鏈表的頭找起,後續節點的地址由當前節點給出。無論在表中訪問那一個節點,都需要從鏈表的頭開始,順序向後查找。鏈表的尾節點由於無後續節點,其指針域爲空,寫作爲NULL。
- C/C++代碼:
功能定義
/*
* SLNode.h
*
* Created on: 2016年6月8日
* Author: ThinkPad
*/
#ifndef SLNODE_H_
#define SLNODE_H_
#define FALSE 0
#define TRUE 1
typedef char SLNodeDataType; //假設結點的數據域類型爲字符
typedef struct Node { //結點類型定義
SLNodeDataType data; //結點的數據域
struct Node *next; //結點的指針域
} SLNode;
/*
* 初始化單鏈表
*/
void InitSLNode(SLNode **head);
/*
* 獲取鏈表長度
*/
int SLNodeLength(SLNode *head);
/*
* 判斷鏈表是否爲空
*/
int SLNodeIsEmpty(SLNode *head);
/*
* 在末尾添加
*/
int SLNodeAdd(SLNode *head, SLNodeDataType data);
/*
* 插入數據
*/
int SLNodeInsert(SLNode *head, int index, SLNodeDataType data);
/*
* 獲取指定位置的數據
*/
int SLNodeGet(SLNode *head, int index, SLNodeDataType *data);
/*
* 修改指定位置的數據
*/
int SLNodeUpdate(SLNode *head, int index, SLNodeDataType data);
/*
* 刪除指定位置的數據
*/
int SLNodeRemove(SLNode *head, int index, SLNodeDataType *data);
/*
* 查找並返回數據位置(1~length)
*/
int SLNodeSearch(SLNode *head, SLNodeDataType data);
/*
* 銷燬鏈表
*/
void SLNodeDestroy(SLNode **head);
/*
* 打印鏈表內容
*/
void ShowSLNode(SLNode *head);
#endif /* SLNODE_H_ */
功能實現
/*
* SLNode.c
*
* Created on: 2016年6月8日
* Author: 龍叔
*/
#include <stdio.h>
#include <stdlib.h>
#include "SLNode.h"
void InitSLNode(SLNode **head) {
if ((*head = (SLNode*) malloc(sizeof(SLNode))) == NULL)
exit(0);
(*head)->next = NULL;
}
int SLNodeLength(SLNode *head) {
if (head == NULL) //鏈表空
return 0;
int length = 0; //計數器初始爲0
SLNode *p = head; //p指向頭結點
while (p->next != NULL) {
p = p->next;
length++;
}
return length;
}
int SLNodeIsEmpty(SLNode *head) {
if (NULL == head)
return TRUE;
else
return FALSE;
}
int SLNodeAdd(SLNode *head, SLNodeDataType data) {
if (NULL == head)
return FALSE;
SLNode *p = head, *node;
while (p->next != NULL) { //尋找第i個結點//
p = p->next;
}
if ((node = (SLNode*) malloc(sizeof(SLNode))) == NULL)
exit(0);
node->data = data; //先存入數據
node->next = NULL; //尾部爲空
p->next = node; //添加到末尾
return TRUE;
}
int SLNodeInsert(SLNode *head, int index, SLNodeDataType data) {
if (head == NULL) //鏈表不存在
return FALSE;
int pos = 0; //記錄當前位置
SLNode *p = head, *node;
//排除錯誤位置
if (index < 0 || index > SLNodeLength(head)) {
printf("SLNodeInsert: index插入位置錯誤!\n");
return FALSE;
}
while (p->next != NULL && pos < index) { //尋找第index個結點
p = p->next;
pos++;
}
if (pos != index) {
printf("插入位置錯誤!\n");
return FALSE;
}
if ((node = (SLNode*) malloc(sizeof(SLNode))) == NULL)
exit(0);
node->data = data; //先存入數據
node->next = p->next;
p->next = node; //最後完成
return TRUE;
}
int SLNodeGet(SLNode *head, int index, SLNodeDataType *data) {
int length = SLNodeLength(head);
if (index < 0 || index > length) {
printf("SLNodeGet: index位置錯誤\n");
return FALSE;
}
SLNode *p = head;
for (int i = 0; i < length; ++i) {
p = p->next;
if (i == index) {
*data = p->data;
}
}
return TRUE;
}
int SLNodeUpdate(SLNode *head, int index, SLNodeDataType data) {
int length = SLNodeLength(head);
if (index < 0 || index > length) {
printf("SLNodeUpdate: index位置錯誤\n");
return FALSE;
}
int pos = 0;
SLNode *p = head;
while (p->next != NULL && index != pos) {
p = p->next;
pos++;
}
p->next->data = data;
return TRUE;
}
int SLNodeRemove(SLNode *head, int index, SLNodeDataType *data) {
int length = SLNodeLength(head);
if (index < 0 || index > length) {
printf("SLNodeUpdate: index位置錯誤\n");
return FALSE;
}
int pos = 0;
SLNode *p = head, *remove;
while (p->next != NULL && index != pos) {
p = p->next;
pos++;
}
remove = p->next;
if (NULL != data) {
*data = remove->data; //記錄刪除的數據
}
p->next = p->next->next; //刪除
free(remove);
return TRUE;
}
int SLNodeSearch(SLNode *head, SLNodeDataType data) {
if (NULL == head)
return FALSE;
int index = 0;
SLNode *p = head;
for (; index < SLNodeLength(head); ++index) {
p = p->next;
if (p->data == data)
return index + 1;
}
return FALSE;
}
void SLNodeDestroy(SLNode **head) {
if (NULL == (*head)) {
return;
}
SLNode *point = *head, *dest;
while (point != NULL) {
dest = point;
point = point->next;
free(dest);
dest = NULL;
}
*head = NULL;
}
void ShowSLNode(SLNode *head) {
printf("ShowSLNode:\n");
int length = SLNodeLength(head);
SLNode *p = head;
for (int i = 0; i < length; ++i) {
p = p->next;
printf(" %c\t", p->data);
}
printf("\n");
}
功能測試
/*
* main.c
* Created on: 2016年6月8日
* Author: 龍叔
*/
#include <stdio.h>
#include "SLNode.h"
/*
* 鏈表測試
*/
void test_SLNode() {
SLNode *head;
InitSLNode(&head);
for (int i = 97; i < 107; ++i) {
SLNodeAdd(head, i);
}
ShowSLNode(head);
SLNodeInsert(head, 1, 'I');
ShowSLNode(head);
SLNodeUpdate(head, 1, 'A');
ShowSLNode(head);
int index = SLNodeSearch(head, 'A');
printf("index A : %d \n", index);
SLNodeRemove(head, 1, NULL);
ShowSLNode(head);
SLNodeDestroy(&head);
ShowSLNode(head);
}
int main() {
test_SLNode();
return 0;
}
運行結果
ShowSLNode:
a b c d e f g h i j
ShowSLNode:
a I b c d e f g h i j
ShowSLNode:
a A b c d e f g h i j
index A : 2
ShowSLNode:
a b c d e f g h i j
ShowSLNode: