单链表的增删查改

单链表的增删查改

  1. 链表是一种物理存储单元上非连续、非顺序的存储结构,数据元素的逻辑顺序是通过链表中的指针链接次序实现的。
  2. 链表由一系列结点(链表中每一个元素称为结点)组成,结点可以在运行时动态生成。
  3. 每个结点包括两个部分:数据域和指针域
     特点:
       1)可以方便的进行扩充。
       2)可以方便的删除和插入。

代码例子如下:

#include <time.h>
#include <errno.h>
#include <stdio.h>
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdint.h>
#include <string.h>
#include <strings.h>
#include <stdbool.h>
#include <pthread.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/mman.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <dirent.h>
//定义结构体表示单链表
struct node
{
	int data;
	struct node *next;
};
//初始化链表
struct node *list_init()
{
	struct node *head = malloc(sizeof(struct node));
	if(head != NULL)
	{
		head->next = NULL;
	}
	return head;
}
//新建节点
struct node *new_node(int n)
{
	struct node *new = malloc(sizeof(struct node));
	if(new != NULL)
	{
		new->data = n;
		new->next = NULL;
	}
	return new;
}

//插入数据于末尾
void inter_node(struct node *new,struct node *head)
{
	struct node *tail = head;
	while(tail->next != NULL)
	{
		tail = tail->next;
	}
	tail->next = new;
	new->next = NULL;
}

删除链表的另外一种方法:

//单链表的删除,用两个指针解决
int remove_list(int deldata,struct siglelist *head)
{
	int flag=0; //标记是否找到要删除的数据
	//找到要删除的节点
	struct siglelist *q=head;
	struct siglelist *p=head->next;
	while(p->next!=NULL)
	{
		if(p->data==deldata)
		{
			flag=1;
			q->next=p->next;
			p->next=NULL;
			free(p);
			p=q->next;
		}	
		else
		{
			p=p->next;
			q=q->next;
		}
	}
	//经过仔细分析,发现前面的循环漏掉了最后一个数据没有判断,没有删除
	if(p->next==NULL && p->data==deldata)
	{
		//把最后一个数据删除即可
		q->next=NULL;
		free(p);
		p=NULL;
		return 0;
	}
	if(flag==0 && p->next==NULL && p->data!=deldata)
	{
		printf("没有你要删除的数据!\n");
		return -1;
	}
}
//删除指定数据
void del(struct node *head,int deldata)
{
	struct node *p = head;
	struct node *q;
	while((p->next)!=NULL&&(p->data)!=deldata)
		{
			q = p;
			p = p->next;	
		}
		if(p->data == deldata)
			{
				q->next = p->next;
				p->next = NULL;
			//删除一个数据后,递归调用看看还有没有删除的数据
				del(head,deldata);
			}
}

void fine(struct node *head,int data)
{
	struct node *p = head;
	while((p->next)!=NULL&&(p->data) != data)
		{
			p = p->next;	
		}
		if(p->data == data)
		{
			printf("找到数据%d\n", data);
		}
		else
			printf("没有找到数据%d\n", data);

}
//更改数据
void chang(struct node *head,int old,int new)
{
	struct node *p = head;
	while((p->next)!=NULL&&(p->data) != old)
		{
			p = p->next;	
		}

		if(p->data == old)
		{
			p->data = new;
			printf("更改成功!\n");
		}
		else
		{
			printf("没有找到要更改的数据!\n");
		}
}

//判断链表是否为空
bool empty(struct node *head)
{
	return head->next == NULL;
}

//打印链表
void show(struct node *head)
{
	if(empty(head))
	{
		printf("列表为空!\n");
	}

	while(head->next != NULL)
	{
		head = head->next;
		printf("列表数据为:%d\n", head->data);
	}
}


主函数如下:

int main()
{
	int n;
	struct node *head = list_init();
	for(int i=0;i<3;i++)
	{
		printf("插入数字为:");
		scanf("%d",&n);
		struct node *new = new_node(n);
		inter_node(new,head);
	}
	printf("-----插入数据!------\n");
	show(head);

	printf("-------删除数据6!------\n");
	del(head,6);
	show(head);

	printf("-------查找数据7!--------\n");
	fine(head,7);

	printf("-------更改数据2->3-----\n");
	chang(head,2,3);
	show(head);
	
	return 0}

以上为个人学习笔记,仅供参考!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值