Linux C学习第十八天

本文深入讲解了链表的基本概念及其实现,包括链表的创建、插入、删除等核心操作,并提供了详细的C语言代码实现,适合初学者和需要复习链表操作的开发者阅读。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

链表:
链表由两个部分组成,
1.数据域:用来存储数据
2.指针域:指向下一个元素

链表的创建,插入,删除:
头文件LinkList.h

#ifndef _LINKLIST_H_
#define _LINKLIST_H_

typedef int DATA;
typedef enum{TRUE,FALSE,ERROR}BOOL;

typedef struct _node
{
	DATA data;
	struct _node *next;
}Node;
typedef struct _list
{
	Node *head;
}List;
//创建一个空链表
List *CreateList();
//删除链表
void Destory(List *pList);
//头插法
BOOL Insert_Head(List *pList,DATA n);
//打印链表
void Display(List *pList);
//尾插法
BOOL Insert_Last(List *pList,DATA n);
//指定位置插入
BOOL Insert_Pos(List *pList,DATA pos,DATA data);
//指定位置删除
BOOL Delete_Pos(List *pList,DATA pos);
//指定数值删除
BOOL Delete_data(List *pList,DATA n);
//指定位置查找
BOOL Find_data(List *pList,DATA pos);
BOOL Reverse_List(List *pList);
#endif //_LINKLIST_H_

功能函数LinkList.c

#include "LinkList.h"
#include <stdlib.h>
#include <stdio.h>
List *CreateList()
{
	List *p = (List*)malloc(sizeof(List)/sizeof(char));
	if(NULL == p)
	{
		return NULL;
	}
	p->head = (Node*)malloc(sizeof(Node)/sizeof(char));
	p->head->next = NULL;
	if(NULL == p->head)
	{
		free(p);
		return NULL;
	}
	return p;
}
BOOL Insert_Head(List *pList,DATA data)
{
	if(NULL == pList)
	{
		return ERROR;
	}
	Node *p = (Node*)malloc(sizeof(Node)/sizeof(char));
	if(NULL == p)
	{
		return ERROR;
	}
	p->data = data;
	p->next = pList->head->next;
	pList->head->next = p;
	return TRUE;
}
BOOL Insert_Last(List *pList,DATA data)
{
	if(NULL == pList)
	{
		return ERROR;
	}
	Node *p = (Node*)malloc(sizeof(Node)/sizeof(char));
	if(NULL == p)
	{
		return ERROR;
	}
	p->data = data;
	p->next = NULL;
	Node *tmp = pList->head;
	while(tmp->next)
	{
		tmp = tmp->next;
	}
	tmp->next = p;
	return TRUE;
}
BOOL Insert_Pos(List *pList,DATA pos,DATA data)
{
	if(NULL == pList||pos<1)
	{
		return ERROR;
	}
	Node *p = (Node*)malloc(sizeof(Node)/sizeof(char));
	if(NULL == p)
	{
		return ERROR;
	}
	p->data = data;
	Node* tmp = pList->head;
	int i;
	for(i=0;i<pos-1;i++)
	{
		tmp = tmp->next;
		if(NULL == tmp)
		{
			printf("下标越界\n");
			return ERROR;
		}
	}
	p->next = tmp->next;
	tmp->next = p;
}	
BOOL Delete_Pos(List *pList,DATA pos)
{
	if(NULL == pList||pos<1)
	{
		return ERROR;
	}
	Node *tmp = pList->head;
	int i;
	for(i=0;i<pos-1;i++)
	{
		tmp = tmp->next;
		if(NULL == tmp||tmp->next ==NULL)
		{
			printf("长度越界%d\n",pos);
			return ERROR;
		}
	}
		Node *p = tmp->next;
		tmp->next=p->next;
		free(p);
		return TRUE;
}
BOOL Delete_data(List *pList,DATA n)
{
	if(NULL == pList)
	{
		return ERROR;
	}
	Node *tmp =pList->head;
	while(tmp->next)
	{
		if(tmp->next->data == n)
		{
			Node *p = tmp->next;
			tmp->next = p->next;
			free(p);
			return TRUE;
		}
		tmp = tmp->next;
	}	
		return FALSE;
}
BOOL Find_data(List *pList,DATA pos)
{
	if(NULL == pList)
	{
		return ERROR;
	}
	Node *tmp =pList->head;
	int i;
	for(i=0;i<pos;i++)
	{
		tmp = tmp->next;
		if(NULL == tmp)
		{
			printf("下标越界\n");
			return ERROR;
		}
	}
	printf("------%d\n",tmp->data);
	
		return TRUE;
}
BOOL Reverse_List(List *pList)
{
	if(NULL == pList||NULL == pList->head||NULL == pList->head->next||NULL == pList->head->next->next)
	{
		return ERROR;
	}
	Node *pre = pList->head->next;
	Node *cur = pre->next;
	Node *tmp;
	while(cur)
	{
		tmp = cur->next;
		cur->next = pre;
		pre = cur;
		cur = tmp;
	}
	pList->head->next->next =NULL;
	pList->head->next = pre;
		
	
}
void Destory(List *pList)
{
	if(NULL==pList)
	{
		return ;
	}
	Node *tmp = pList->head;
	while(tmp->next)
	{
		Node *p = tmp->next;
		tmp->next = p->next;
		free(p);
	}
	free(pList->head);
	free(pList);
}
void Display(List *pList)
{
	if(NULL==pList)
	{
		return ;
	}
	
	Node *tmp = pList->head->next;
	while(tmp)
	{
		printf("%-4d",tmp->data);
		tmp = tmp->next;
	}
	printf("\n");
}
	
	
	

主函数:

#include <stdio.h>
#include "LinkList.h"

int main()
{
	List *L = CreateList();
	int i;
	for(i=0;i<10;i++)
	{
		Insert_Last(L,i);
	}
	Display(L);
	Insert_Pos(L,11,699);
	Display(L);
	Insert_Pos(L,1,99);
	Display(L);
	Insert_Pos(L,13,65);
	Display(L);
	Insert_Pos(L,14,85);
	Display(L);
	Delete_Pos(L,14);
	Display(L);
	Delete_data(L,699);
	Display(L);
	Reverse_List(L);
	Display(L);
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值