版权声明:转载请通知,复制请通知 如有兴趣欢迎加群:653460549 - Java资源分享 https://blog.csdn.net/qq_42038407/article/details/82596924
不带头节点的单链表:
C语言:
结构体:
#define _Data int
typedef struct ListNode{
_Data value;
struct ListNode * next;
}Node, * pNode;
函数声明:
pNode createNode(_Data value); //创建节点
void addNode(pNode head, _Data value); //添加节点
pNode findNode(pNode head, _Data value); //查找节点
int isDataInList(pNode head, _Data value); //判断某值是否存在
int listSize(pNode head); //获取链表大小
int isListEmpty(pNode head); //判断链表是否为空
pNode getNodeByIndex(pNode head, int index); //根据索引获得节点
int updataValueByValue(pNode head, _Data beUpdataValue, _Data updataValue); //通过数据更改列表中的第一次出现的数据
int updataValueByInde(pNode head, int index, _Data value); //通过索引更改数据
void updatAllByValue(pNode head, _Data beUpdataValue, _Data updataValue); //通过数据更改所有出现过的数据
int countValue(pNode head, _Data beCountValue); //统计某数据出现的个数
pNode deleteNode(pNode pre); //通过前置节点删除某节点
pNode deleteHead(pNode head); //删除头节点
pNode destoryList(pNode head); //销毁链表
void printList(pNode head); //打印链表
pNode deleteByIndex(pNode head, int index); //根据索引删除某节点
代码实现:
//
// Created by Dong on 2018/9/9.
//
#include <stdio.h>
#include <stdlib.h>
//创建节点
pNode createNode(_Data value){
pNode temp = (pNode)malloc(sizeof(Node));
temp->value = value;
temp->next = NULL;
return temp;
}
void addNode(pNode head, _Data value){
pNode newNode = createNode(value);
pNode temp = head;
if(head != NULL){
while(temp->next != NULL){
temp = temp->next;
}
temp->next = newNode;
}
}
int listSize(pNode head){
int size = 0;
pNode temp = head;
while(temp != NULL){
size++;
temp = temp->next;
}
return size;
}
pNode findNode(pNode head, _Data value){
pNode temp = head;
while(temp != NULL){
if(temp->value == value) return temp;
temp = temp->next;
}
return NULL;
}
pNode getNodeByIndex(pNode head, int index){
pNode temp = head;
if(index < listSize(head)){
int count = 0;
while (count++ < index){
temp = temp->next;
}
return temp;
}
return NULL;
}
int isDataInList(pNode head, _Data value){
pNode temp = head;
return findNode(head, value) != NULL;
}
int isListEmpty(pNode head){
return listSize(head) == 0;
}
int updataValueByValue(pNode head, _Data beUpdataValue, _Data updataValue){
if(isDataInList(head, beUpdataValue)){
pNode temp = findNode(head, beUpdataValue);
temp->value = updataValue;
return 1;
}
return 0;
}
int updataValueByInde(pNode head, int index, _Data value){
if(index < listSize(head)){
pNode temp = getNodeByIndex(head, index);
temp->value = value;
return 1;
}
return 0;
}
void updatAllByValue(pNode head, _Data beUpdataValue, _Data updataValue){
pNode temp = head;
while(temp != NULL){
if(temp->value == beUpdataValue){
temp->value = updataValue;
}
temp = temp->next;
}
}
int countValue(pNode head, _Data beCountValue){
pNode temp = head;
int count = 0;
while(temp != NULL){
if(temp->value == beCountValue){
count++;
}
temp = temp->next;
}
return count;
}
pNode deleteNode(pNode pre){
if(pre->next != NULL){
pNode deleteNode = pre->next;
pre->next = deleteNode->next;
free(deleteNode);
}
return NULL;
}
pNode deleteHead(pNode head){
if(head != NULL){
pNode temp = head->next;
free(head);
return temp;
}
return NULL;
}
pNode destoryList(pNode head){
while(head != NULL){
pNode temp = head->next;
free(head);
head = temp;
}
}
void printList(pNode head){
printf("链表状态:\n");
printf("链表大小:%d\n", listSize(head));
printf("链表数据:");
pNode temp = head;
while(temp != NULL){
printf("%d->", temp->value);
temp = temp->next;
}
printf("NULL\n");
}
pNode deleteByIndex(pNode head, int index){
if(index == 0){
return deleteHead(head);
}
pNode temp = head->next;
pNode pre = head;
int count = 1;
while(temp != NULL){
if(count++ == index){
deleteNode(pre);
break;
}
pre = temp;
temp = temp->next;
}
return head;
}
测试代码:
int main() {
pNode head = createNode(5);
printList(head);
printf("%d\n", isDataInList(head, 5));
printf("%d\n", isDataInList(head, 6));//没有数据
printf("%d\n", isListEmpty(head));
addNode(head, 6);
printList(head);
updataValueByValue(head, 5, 3);
printList(head);
updataValueByInde(head, 2, 9);
printList(head);
updataValueByInde(head, 1, 3);
printList(head);
updatAllByValue(head, 3, 9);
printList(head);
printf("%d\n", countValue(head, 9));
head = deleteByIndex(head, 0);
printList(head);
head = destoryList(head);
printf("%d\n", isListEmpty(head));
printList(head);
}
运行结果截图如下:
测试数据并不是很严谨 如果发现问题 请联系我 蟹蟹