C 语言

关注公众号 jb51net

关闭
首页 > 软件编程 > C 语言 > C语言实现单链表

C语言实现单链表的示例详解

作者:gnip

给需要考研的同学一个参考,单链表作为常见数据结构的一种,这里记录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函数

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;
}

初始化(带头节点)

 bool intLNode(LNode** head) {
	 *head = (LNode*)malloc(sizeof(LNode));
	 if (*head == NULL) {
		 return false;
	 }
	 (*head)->data = 0;
	 (*head)->next = NULL;
	 return true;
}

链表按位置插入(带头节点)

 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;
 }

链表按数据值后插入(带头节点)

 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;
 }

链表按数据值前插入(带头节点)

 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;
 }

链表按位置删除(带头节点)

 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;
 }

链表按指定节点删除(带头节点)

 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;
 }

链表查找对应的节点

 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;
 }

以上就是C语言实现单链表的示例详解的详细内容,更多关于C语言实现单链表的资料请关注脚本之家其它相关文章!

您可能感兴趣的文章:
阅读全文