概述
给需要考研的同学一个参考,单链表作为常见数据结构的一种,这里记录C语言实现单链表。
代码
函数原型和结构体声明
c
#include<stdio.h>
#include<assert.h>
#include<stdlib.h>
#define true 1
#define false 0
#define bool char
//链表数据类型
typedef int ElementType;
//链表节点结构体
typedef struct LNode {
ElementType data;
struct LNode* next;
} LNode;
bool intLNode(LNode** head);
bool insertLNodeFromPos(LNode* head, int pos, ElementType data);
bool inserteEndLNodeFromTargetValue(LNode* head, ElementType target, ElementType data);
bool inserteBeforeLNodeFromTargetValue(LNode* head, ElementType target, ElementType data);
bool deleteFromPos(LNode* head, int pos);
//链表按值删除(带头节点)
bool deleteFromTargetValue(LNode* head, LNode* node);
LNode* query(LNode* head, ElementType target);
main函数
c
int main() {
LNode *head ;
intLNode(&head);
insertLNodeFromPos(head, 1, 1);
insertLNodeFromPos(head, 2, 2);
insertLNodeFromPos(head, 1, 101);
inserteEndLNodeFromTargetValue(head, 2, 221);
inserteBeforeLNodeFromTargetValue(head, 2, 333);
//deleteFromPos(head, 3);
deleteFromTargetValue(head, head->next->next->next);
printf("%d",head->next->next->next->data);
//printf("%d", query(head, 101)->data);
return 0;
}
初始化(带头节点)
c
bool intLNode(LNode** head) {
*head = (LNode*)malloc(sizeof(LNode));
if (*head == NULL) {
return false;
}
(*head)->data = 0;
(*head)->next = NULL;
return true;
}
链表按位置插入(带头节点)
c
bool insertLNodeFromPos(LNode* head,int pos, ElementType data) {
//边界值判断
if (pos < 1) {
return false;
}
LNode* current = head;
int index = 0;
while (current!=NULL) {
if (index == pos - 1) {
break;
}
current = current->next;
index++;
}
//确保当前没有超出范围
if (current == NULL) {
return false;
}
//创建新节点
LNode* newNode = (LNode*)malloc(sizeof(LNode));
if (newNode == NULL) {
return false;
}
//更改指针指向
newNode->data = data;
newNode->next = current->next;
current->next = newNode;
return true;
}
链表按数据值后插入(带头节点)
c
bool inserteEndLNodeFromTargetValue(LNode* head, ElementType target, ElementType data) {
LNode* current = head;
while (current != NULL) {
if (current->data == target) {
break;
}
current = current->next;
}
//确保当前没有超出范围
if (current == NULL) {
return false;
}
//创建新节点
LNode* newNode = (LNode*)malloc(sizeof(LNode));
if (newNode == NULL) {
return false;
}
//更改指针指向
newNode->data = data;
newNode->next = current->next;
current->next = newNode;
return true;
}
链表按数据值前插入(带头节点)
c
bool inserteBeforeLNodeFromTargetValue(LNode* head, ElementType target, ElementType data) {
LNode* current = head;
while (current != NULL) {
if (current->data == target) {
break;
}
current = current->next;
}
//确保当前没有超出范围
if (current == NULL) {
return false;
}
//创建新节点
LNode* newNode = (LNode*)malloc(sizeof(LNode));
if (newNode == NULL) {
return false;
}
//更改指针指向
newNode->data = current->data;
newNode->next = current->next;
current->next = newNode;
current->data = data;
return true;
}
链表按位置删除(带头节点)
c
bool deleteFromPos(LNode* head, int pos) {
if (pos < 1) {
return false;
}
LNode* current = head;
int index = 0;
while (current != NULL) {
if (index==pos-1) {
break;
}
current = current->next;
index++;
}
//确保当前没有超出范围
if (current == NULL) {
return false;
}
//更改指针指向
LNode* temp = current->next;
current->next = current->next->next;
//释放断开节点的内存空间
free(temp);
return true;
}
链表按指定节点删除(带头节点)
c
bool deleteFromTargetValue(LNode* head,LNode *node) {
LNode* current = head;
while (current != NULL) {
//找到需要删除的节点的前一个节点
if (current->next == node) {
break;
}
current = current->next;
}
//确保当前没有超出范围
if (current == NULL) {
return false;
}
//更改指针指向
LNode* temp = current->next;
current->next = current->next->next;
free(temp);
return true;
}
链表查找对应的节点
c
LNode* query(LNode* head, ElementType target) {
LNode* current = head;
while (current != NULL) {
if (current->data == target) {
break;
}
current = current->next;
}
//确保当前没有超出范围
if (current == NULL) {
return NULL;
}
return current;
}