头文件
typedef int ElemData;
typedef struct Node* pNode;
/*定义节点类型*/
typedef struct Node
{
ElemData data; /*数据域*/
pNode previous; /*指向前驱*/
pNode next; /*指向后继*/
}Node;
typedef struct{
pNode head;
pNode tail;
int size;
}D_LIST;
pNode createNode();
//创建一个节点,prev与next指向NULL
D_LIST* createList();
void initList(D_LIST* list,ElemData e);
//释放节点
void freeNode(pNode node);
//头部插入一个节点
void insertNodeFromHead(D_LIST* list,ElemData e);
//尾部插入一个节点
void insertNodeFromTail(D_LIST* list,ElemData e);
//指定位置插入一个节点
void insertNodeToIndex(D_LIST* list,ElemData e ,int index);
//指定data域之后插入
void insertNodeToData(D_LIST* list,ElemData data,ElemData insertData);
//修改索引节点的数据
void updateNodeDataByIndex(D_LIST* list,ElemData e,int index);
//删除索引节点,返回数据域的值,没有则返回NULL
ElemData removeNodeByIndex(D_LIST* list,int index);
//删除ElemData的节点
ElemData removeElemData(D_LIST* list,ElemData e);
pNode get(D_LIST* list,int index);
/*依次对链表中每个元素调用函数visit()*/
void ListTraverse(D_LIST* list);
//destroy
void destroyList(D_LIST* list);
实现
#include <stdio.h>
#include "LinkedList.h"
#include <malloc.h>
#include <stdlib.h>
#include <string.h>
//创建一个节点,prev与next指向NULL
D_LIST* createList(){
D_LIST* node = (D_LIST*)malloc(sizeof(D_LIST));
if(node == NULL){
printf("内存分配失败!\n");
return NULL;
}
memset(node,0,sizeof(D_LIST));
node->size = 0 ;
node->head = NULL;
node->tail = NULL;
return node;
}
pNode createNode(){
pNode node = (pNode)malloc(sizeof(Node));
if(node == NULL){
printf("内存分配失败!");
return NULL;
}
memset(node,0,sizeof(Node));
node->next = NULL;
node->previous = NULL;
}
void initList(D_LIST* list,ElemData e){
pNode node = createNode();
if(node == NULL){
return ;
}
list->size = list->size+1;
node->data = e;
list->head = node;
list->tail = node;
}
//释放节点
void freeNode(pNode node){
free(node);
}
//头部插入一个节点
void insertNodeFromHead(D_LIST* list,ElemData e){
pNode node = createNode();
if(node == NULL){
return;
}
node->data = e;
node->next = list->head;
if(list->head != NULL)
list->head->previous = node;
list->head = node;
list->size = list->size + 1;
}
//尾部插入一个节点
void insertNodeFromTail(D_LIST* list,ElemData e){
pNode node = createNode();
if(node == NULL){
return;
}
node->data = e;
if(list->tail != NULL){
list->tail->next = node;
}
node->previous = list->tail;
if(list->head == NULL){
list->head = node;
}
list->tail = node;
list->size = list->size + 1;
}
//
//指定位置插入一个节点,节点前比较好实现
void insertNodeToIndex(D_LIST* list,ElemData e ,int index){
int indexTemp = index;
if(index < 0){
printf("索引不能为负数!\n");
return;
}
if(list->size < index){
printf("索引超过链表的最大长度!\n");
return;
}
pNode indexNode = list->head;
while(indexNode != NULL && indexTemp > 0){
indexTemp--;
indexNode = indexNode->next;
}
if(indexNode == NULL){
initList(list,e);
return;
}
pNode node = createNode();
if(node == NULL){
return;
}
node->data = e;
node->next = indexNode;
node->previous = indexNode->previous;
if(indexNode->previous != NULL)
indexNode->previous->next = node;
if(indexNode->previous)
indexNode->previous = node;
if(index == 0){
list->head = node;
}
else if(index == list->size -1)
list->tail = node;
list->size = list->size + 1;
}
指定data域之前插入
//void insertNodeToData(D_LIST* list,ElemData data,ElemData insertData){
//
//
//
//}
//修改索引节点的数据
void updateNodeDataByIndex(D_LIST* list,ElemData e,int index){
int indexTemp = index;
if(index < 0){
printf("索引不能为负数!\n");
return;
}
if(list->size < index){
printf("索引超过链表的最大长度!\n");
return;
}
pNode indexNode = list->head;
//可优化 (/2)
while(indexNode != NULL && indexTemp > 0){
indexTemp--;
indexNode = indexNode->next;
}
indexNode->data = e;
}
//删除索引节点,返回数据域的值,没有则返回NULL
ElemData removeNodeByIndex(D_LIST* list,int index){
int indexTemp = index;
if(index < 0){
printf("索引不能为负数!\n");
return -1;
}
if(list->size < index){
printf("索引超过链表的最大长度!\n");
return -1;
}
pNode indexNode = list->head;
//可优化 (/2)
while(indexNode != NULL && indexTemp > 0){
indexTemp--;
indexNode = indexNode->next;
}
if(index == list->size){
list->tail = indexNode->previous;
}
if(index == 0)
list->head = indexNode->next;
if(indexNode->previous != NULL)
indexNode->previous->next = indexNode->next;
if(indexNode->next != NULL)
indexNode->next->previous = indexNode->previous;
int dat = indexNode->data;
freeNode(indexNode);
return dat;
}
//删除ElemData的节点
ElemData removeElemData(D_LIST* list,ElemData e){
if(list->head == NULL){
printf("查找元素不存在,链表为空!");
return -1;
}
pNode node = list->head;
while(node->data != e){
node = node->next;
}
if(node == NULL){
printf("未找到对应元素的节点!");
return -1;
}
if(&node == &(list->head)){
list->tail = node->previous;
}
if(&node == &(list->tail)){
list->head = node->next;
}
if(node->previous != NULL)
node->previous->next = node->next;
if(node->next != NULL)
node->next->previous = node->previous;
int dat = node->data;
freeNode(node);
return dat;
}
void ListTraverse(D_LIST* list){
if(list == NULL){
printf("链表还未创建!\n");
return;
}
pNode node = list->head;
if(node == NULL){
printf("链表为空\n");
return;
}
printf("链表长度:%d\n",list->size);
while(node != NULL){
printf("%d\t",node->data);
node = node->next;
}
}
//destroy
void destroyList(D_LIST** list){
pNode fNode = NULL;
pNode head = (*list)->head;
int i = 1;
while(head != NULL){
printf("%d\n",i++);
fNode = head;
head = fNode->next;
freeNode(fNode);
}
free(*list);
*list = NULL;
}
int main(){
D_LIST* list = createList();
initList(list,50);
insertNodeFromHead(list,10);
insertNodeFromTail(list,1000);
insertNodeToIndex(list,100,2);
updateNodeDataByIndex(list,200,2);
//removeNodeByIndex(list,2);
//ListTraverse(list);
removeElemData(list,200);
destroyList(&list);
ListTraverse(list);
//initList(&list,100);
//insertNodeFromHead(&list,50);
//insertNodeFromTail(&list,200);
//insertNodeToIndex(&list,5,0);
//insertNodeToData(&list,50,10);
//ListTraverse(list);
return 1;
}