版权声明:转载请通知,复制请通知 如有兴趣欢迎加群:653460549 - Java资源分享 https://blog.csdn.net/qq_42038407/article/details/82632220
结构体
typedef struct DListNode {
_Data value;
struct DListNode *pre;
struct DListNode *next;
} *pNode, Node;
函数声明
pNode createNode(_Data value); //创建节点
pNode addNode(pNode head, _Data value);//添加节点
pNode createHead(); //创建头节点
pNode destoryList(pNode head); //销毁节点
int listSize(pNode head); //获取链表长度
void printList(pNode head); //打印链表信息
pNode findNodeByValue(pNode head, _Data value);//查找数据
pNode findNodeByIndex(pNode head, int index); //根据索引查找数据
void deleteNodeByValue(pNode head, _Data value);//删除第一次出现的某值
void deleteNodeByIndex(pNode head, int index);//删除索引指向的节点
int findValueFormList(pNode head, _Data value);//查找某值第一次出现的位置
函数体实现
#include <stdlib.h>
#include <stdio.h>
pNode createNode(_Data value) {
pNode node = (pNode) malloc(sizeof(Node));
node->next = NULL;
node->pre = NULL;
node->value = value;
return node;
}
pNode addNode(pNode head, _Data value) {
pNode temp = head;
while (temp->next != NULL) {
temp = temp->next;
}
pNode beAdd = createNode(value);
temp->next = beAdd;
beAdd->pre = temp;
beAdd->next = NULL;
}
pNode createHead() {
return createNode(0);
}
pNode destoryList(pNode head) {
pNode temp = head;
while (temp != NULL) {
head = temp->next;
free(temp);
temp = head;
}
return NULL;
}
int listSize(pNode head) {
pNode temp = head->next;
int count = 0;
while (temp != NULL) {
count++;
temp = temp->next;
}
return count;
}
void printList(pNode head) {
if (head != NULL) {
printf("链表状态:\n");
printf("--->链表大小:%d\n", listSize(head));
printf("--->链表数据:Head<->");
pNode temp = head->next;
while (temp != NULL) {
printf("%d<->", temp->value);
temp = temp->next;
}
printf("NULL\n");
}
}
pNode findNodeByValue(pNode head, _Data value) {
if (head != NULL) {
pNode temp = head->next;
while (temp != NULL) {
if (temp->value == value) {
return temp;
}
temp = temp->next;
}
}
return NULL;
}
pNode findNodeByIndex(pNode head, int index) {
int count = 0;
pNode temp;
if (head != NULL) {
temp = head->next;
while (count < index && temp != NULL) {
temp = temp->next;
count++;
}
}
return temp;
}
void deleteNodeByValue(pNode head, _Data value) {
pNode beDelete = findNodeByValue(head, value);
if (beDelete != NULL) {
beDelete->pre->next = beDelete->next;
if (beDelete->next != NULL)
beDelete->next->pre = beDelete->pre;
free(beDelete);
}
}
void deleteNodeByIndex(pNode head, int index) {
if (index > listSize(head)) {
return;
} else {
pNode beDelete = findNodeByIndex(head, index);
beDelete->pre->next = beDelete->next;
if (beDelete->next != NULL)
beDelete->next->pre = beDelete->pre;
free(beDelete);
}
}
int findValueFormList(pNode head, _Data value) {
if (head != NULL) {
pNode temp = head->next;
int count = 0;
while (temp != NULL) {
count++;
if (temp->value == value) {
return count;
}
temp = temp->next;
}
}
return -1;
测试代码:
int main() {
pNode head = createHead();
printList(head);
addNode(head, 6);
printList(head);
printf("6第一次出现的位置:%d\n", findValueFormList(head, 6));
addNode(head, 7);
printList(head);
deleteNodeByIndex(head, 1);
printList(head);
deleteNodeByValue(head, 6);
printList(head);
head = destoryList(head);
printList(head);
}