数据结构学习 单链表的C语言实现

#pragma once
#define _CRT_SECURE_NO_WARNINGS 1

#include <stdio.h>
#include <stdlib.h>

typedef int data_t;
typedef struct node
{
	data_t data;//成员数据类型
	struct node* next;//存放下一结点地址

}listnode, *linklist;

//无特殊说明时,@ret -1代表操作失败 0代表操作成功

//创建一个节点,返回头结点地址,失败返回NULL
linklist link_creat();

//链表尾部插入
void link_tail(linklist p, const data_t *val);

//链表清空
int link_clear(linklist p);

//按值删除链表中的节点
int link_delete(linklist p, data_t* val);

//按值查找链表,返回该值在链表第几个位置,失败返回-1
int link_find(linklist p, const data_t* sour);

//在链表的第几个位置插入指定值的新节点
int link_insert(linklist p, int pos, const data_t* val);

//释放链表动态空间,传递二级指针,便于将头结点指针置NULL
void link_free(linklist* p);

//遍历打印链表
void link_show(linklist p);

头文件LinkList.h

#include "LinkedList.h"


linklist link_creat()
{
	linklist p;
	p = (linklist)malloc(sizeof(listnode));
	if (p == NULL)
	{
		printf("Pointer is NULL.\n");
		return NULL;
	}
	p->data = 0;
	p->next = NULL;
	return p;
}

void link_tail(linklist p, const data_t* val)
{
	if (p == NULL)
	{
		printf("Pointer is NULL.\n");
		return;
	}
	linklist newnode;
	newnode = link_creat();
	newnode->data = *val;

	if (newnode == NULL)
	{
		printf("Malloc Error.\n");
	}
	while (p->next != NULL)
	{
		p = p->next;
	}
	p->next = newnode;

}

int link_clear(linklist p)
{
	if (p == NULL)
	{
		printf("Pointer is NULL.\n");
		return -1;
	}
	linklist temp;
	temp = p;
	while (p != NULL)
	{
		temp = p;
		p = p->next;
		temp->data = 0;
		temp->next = NULL;
	}
	return 0;
}

int link_delete(linklist p, data_t* val)
{
	int i = 0;
	if (p == NULL)
	{
		printf("Pointer is NULL.\n");
		return -1;
	}
	int ret = link_find(p, val) - 1;
	if (ret == -1)
	{
		printf("Value Not found.\n");
		return -1;
	}
	else
	{
		while (i < ret)
		{
			p = p->next;
			i++;
		}
		p->next = p->next->next;
		return 0;
	}
}

int link_find(linklist p, const data_t* sour)
{
	if (p == NULL)
	{
		printf("Pointer is NULL.\n");
		return -1;
	}
	int pos = 0;
	while (p != NULL)
	{
		if (p->data == *sour)
		{
			return pos;
		}
		p = p->next;
		pos++;
	}
	return -1;
}

int link_insert(linklist p, int pos, const data_t* val)
{
	int i = 1;
	if (p == NULL)
	{
		printf("Pointer is NULL.\n");
		return -1;
	}
	linklist newnode;
	newnode = link_creat();
	while (i < pos)
	{
		p = p->next;
		i++;
		if (pos < 0 || p == NULL)
		{
			//插入位置太大或太小
			printf("Input Wrong.\n");
			return -1;
		}
	}
	newnode->next = p->next;
	newnode->data = *val;
	p->next = newnode;
	return 0;
}

void link_show(linklist p)
{
	linklist temp;
	if (p == NULL)
	{
		printf("Pointer is NULL.\n");
		return;
	}
	temp = p;
	while (temp->next != NULL)
	{
		printf("%d\t%p\n",temp->next->data, temp);
		temp = temp->next;
	}
	printf("\n");
}

void link_free(linklist* p)
{
	if (*p == NULL)
	{
		printf("Pointer is NULL.\n");
		return;
	}
	linklist temp;
	temp = *p;
	while (*p != NULL)
	{
		temp = *p;
		*p = (*p)->next;
		free(temp);
	}
	*p = NULL;
}

LinkList.c 函数功能实现

#include "LinkedList.h"

int main()
{
	linklist p;
	p = link_creat();

	//测试部分

	//
	return 0;
}

test.c 测试函数功能

        又犯了很多次愚蠢的错误,对空指针解引用,指针越界访问,操作不属于动态开辟的内存空间。这种错误相比语法错误不好调试,特别是循环当中,循环的起始条件,终止条件,条件修改和语句顺序都有影响。还好VS使用未初始化的野指针会报语法错误,不然我的内存报错还会更多。

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
循环单链表链表的一种特殊形式,它的特点是链表中的最后一个节点的指针指向第一个节点,形成一个闭合的环。在C语言中,操作循环单链表通常涉及创建、插入、删除和遍历等基本操作。 以下是一些基本操作的C语言实现概览: 1. **创建循环链表**: - 定义节点结构体,包含数据域和指向下一个节点的指针(如果是在头结点的最后一个节点,指针会指向自身)。 - 创建链表时,可以初始化一个头节点,然后递归地添加新节点到链尾。 ```c struct Node { int data; struct Node* next; }; // 创建循环链表 void createCircularList(struct Node** head, int size, ...) { ... } ``` 2. **插入节点**: - 可以在链表的头部、尾部或指定位置插入节点。 - 在头部插入,更新头节点的next;在尾部插入,找到当前尾节点,然后更新其next指向新节点,并将新节点next指向头节点。 ```c void insertNode(struct Node** head, int data, insertionPosition) { struct Node* newNode = (struct Node*)malloc(sizeof(struct Node)); newNode->data = data; newNode->next = (insertionPosition == 1) ? head : (*(head)->next); // 如果在尾部插入,更新尾节点和头节点的next指针 if (insertionPosition == 0) { (*head)->next = newNode; } } ``` 3. **删除节点**: - 删除某个节点时,需要找到前一个节点,然后更新其next指针跳过要删除的节点。 - 在循环链表中删除特定位置的节点需要特别处理头节点的情况。 ```c void deleteNode(struct Node** head, int position) { if (position == 1) { // 删除头节点 struct Node* temp = head->next; *head = temp; free(head); } else { struct Node* current = *head, *previous = NULL; for (int i = 1; i < position && current != NULL; ++i) { previous = current; current = current->next; } if (current == NULL) return; // 未找到节点 previous->next = current->next; } } ``` 4. **遍历循环链表**: - 使用while循环,每次迭代都更新当前节点指向下一个节点,直到遇到第一个节点。 ```c void printList(struct Node* head) { struct Node* temp = head; do { printf("%d ", temp->data); temp = temp->next; } while (temp != head); } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值