单链表这种数据结构表现为:在内存中可以不连续存储,每一个节点存储一个指向下一个节点的next指针。单链表在search时以O(N)的时间运行,而在insert时以O(1)时间运行。
在实现方面,以C为例,一般会保留一个header作为表头,以NULL为表尾,废话不多说,上代码。
接口定义如下:
#ifndef _List_H
#define _List_H
/*链表节点存储的数据类型*/
typedef int ElementType;
/*链表节点结构体*/
struct NodeStruct;
/*链表结构体*/
struct ListStruct;
/*链表节点结构体指针*/
typedef struct NodeStruct *PtrToNode;
/*链表结构体指针*/
typedef struct ListStruct *PtrToList;
/*链表节点*/
typedef PtrToNode Node;
/*链表*/
typedef PtrToList List;
struct NodeStruct{
ElementType element;
Node next;
};
struct ListStruct{
Node header;
int size;
};
/*初始化一个链表*/
List initList();
/*添加元素,默认在header之后插入*/
void add(List list,ElementType element);
/*从header开始找到第一个存储该值的节点*/
Node find(List list,ElementType element);
/*找到某个节点的上一个节点*/
Node previous(List list,Node node);
/*删除存储该值的第一个节点*/
void removeElement(List list,ElementType element);
/*销毁链表*/
void destoryList(List list);
/*打印链表*/
void printList(List list);
#endif
实现如下:
#include "linkedlist.h"
#include <stdio.h>
#include <stdlib.h>
List initList(){
List list;
Node header;
header = malloc(sizeof(struct NodeStruct));
if(header == NULL){
printf("%s\n","OutOfMemoryError!");
exit(1);
}
header->next = NULL;
list = malloc(sizeof(struct ListStruct));
if(list == NULL){
printf("%s\n","OutOfMemoryError!");
exit(1);
}
list->header = header;
list->size = 0;
return list;
}
void add(List list,ElementType element){
if(list == NULL || list->header == NULL){
printf("%s\n","please init list first!");
}
Node header,newNode,firstNode;
header = list->header;
firstNode = header->next;
newNode = malloc(sizeof(struct NodeStruct));
if(newNode == NULL){
printf("%s\n","OutOfMemoryError!");
exit(1);
}
newNode->next = firstNode;
newNode->element = element;
header->next = newNode;
list->size++;
}
void printList(List list){
if(list == NULL){
printf("%s\n","list is null!");
return;
}
printf("[");
Node firstNode = list->header->next;
while(firstNode != NULL){
printf("%d,",firstNode->element);
firstNode = firstNode->next;
}
if(list->size>0){
printf("\b]\n");
}else{
printf("]\n");
}
}
Node find(List list,ElementType element){
Node header,firstNode,result;
result = NULL;
header = list->header;
firstNode = header->next;
while(firstNode != NULL){
if(firstNode->element == element){
result = firstNode;
break;
}else{
firstNode = firstNode->next;
}
}
}
void removeElement(List list,ElementType element){
Node previousNode,currentNode,nextNode;
currentNode = find(list,element);
if(currentNode == NULL)
return;
previousNode = previous(list,currentNode);
nextNode = currentNode->next;
previousNode->next = nextNode;
free(currentNode);
list->size--;
}
Node previous(List list,Node node){
Node header,result;
header = list->header;
result = header;
while(result->next != node){
result = result->next;
}
return result;
}
void destoryList(List list){
Node header,firstNode,nextNode;
header = list->header;
firstNode = header->next;
header->next = NULL;
while(firstNode!=NULL){
nextNode = firstNode->next;
free(firstNode);
list->size--;
firstNode = nextNode;
}
free(header);
header = NULL;
free(list);
list = NULL;
}
该程序在如下环境中编译测试通过: