单链表的创建,初始化,插入,删除,修改,查询

链表操作实现及应用
该博客介绍了如何使用C语言实现链表的基本操作,包括初始化、插入、删除、更改和查找元素。通过示例展示了如何创建一个链表并进行一系列操作,如插入新值、在指定位置插入和删除节点、改变节点值以及查找特定值。
/*
* study linklist1:   dan lian biao
* with create  init  insert  del  change  lookup
* date: 2020/12/11
*/
#include <stdio.h>
#include <stdlib.h>

#define OK     0
#define ERROR -1

typedef struct linklist
{
	int data;
	struct linklist * next;
}ln;

int llist_init(ln* headNode, int* data, int size);
int list_data(ln* headnode);
int llist_insert(ln* current, int new_value);//顺序存储,数据new_value插入操作 
int llist_dst_insert(ln* current, int dst, int value);//在dst位置插入值value 
int llist_dst_del(ln* current, int dst);//删除指定位置dst的结点
int llist_changevalue(ln* current, int dst, int value);//改变dst位置的值为value
int llist_lookup_value(ln* current, int dst);//查找对应结点dst的值 
//查找链表中是否有对应的值value,存在返回结点地址,否则返回NULL 
ln* llist_lookup_dict(ln* current, int value);

int main()
{
	int ret;
	ln* current;
	int a[5] = {1,2,3,4,5};
	ln* headnode = (ln *)malloc(sizeof(ln));
	if(headnode == NULL)
	{
		perror("malloc error!\n");
		return ERROR;
	}
	
	llist_init(headnode, a, 5);
	list_data(headnode);
	printf("\n");
	
	llist_insert(headnode,3);
	list_data(headnode);
	printf("\n");
	
	llist_dst_insert(headnode,4,10);
	list_data(headnode);
	printf("\n");
	
	llist_dst_del(headnode,4);
	list_data(headnode);
	printf("\n");
	
	llist_changevalue(headnode,4,9);
	list_data(headnode);
	printf("\n");
	
	if(ret = llist_lookup_value(headnode,4))
	{
		printf("%d\n", ret);
	}
	
	current = llist_lookup_dict(headnode, 9);
	if(current == NULL)
	{
		printf("this value is not in linklist!\n");
	}
	printf("this value is in linklist, value: %d\n", current->data);
	
	return OK;	
}


int llist_init(ln* headNode, int* data, int size)
{
	int i;
	ln * currentNode = headNode;
	
	for(i = 0; i < size; i++)
	{
		currentNode->data = *(data+i);
		if(i < size-1)
		{
			currentNode->next = (ln *)malloc(sizeof(ln));
			if(currentNode->next == NULL)
			{
				perror("malloc error!\n");
				return ERROR;
			}
			currentNode = currentNode->next;
		}
	} 
	currentNode->next = NULL;
	
	return OK;
}

int list_data(ln* headnode)
{
	ln* current = headnode;
	
	do{
		printf("%d\n", current->data);
		current = current->next;
	}while(current != NULL);
	
	return OK;
}

int llist_insert(ln* current, int new_value)//顺序存储,数据new_value插入操作 
{
	ln* previous;
	ln* new;
	
	while(current->data < new_value)
	{
		previous = current;
		current = current->next;
	}
	
	new = (ln *)malloc(sizeof(ln));
	if(new == NULL)
	{
		perror("malloc error!\n");
		return ERROR;
	}
	
	new->data = new_value;
	new->next = current;
	previous->next = new;
	
	return OK;
} 
 
int llist_dst_insert(ln* current, int dst, int value)//在dst位置插入值 value
{
 	int i;
 	ln* previous;
 	ln* new;
 	
 	for(i = 0; i < dst-1; i++)
 	{
 		previous = current;
 		current = current->next;	
	}
	
	new = (ln*)malloc(sizeof(ln));
	if(new == NULL)
	{
		perror("malloc error!\n");
		return ERROR;
	}
	
	new->data = value;
	new->next = current;
	previous->next = new;
 	
 	return OK;
}
 
int llist_dst_del(ln* current, int dst)//删除指定位置dst的结点
{
 	int i;
 	ln* previous;
 	
 	for(i = 0; i < dst-1; i++)
 	{
 		previous = current;
 		current = current->next;
	}
	
	previous->next = current->next;
	free(current); 
	current = NULL;
 	
 	return OK;
} 
 
int llist_changevalue(ln* current, int dst, int value)//改变dst位置的值为value 
{
 	int i;
 	
 	for(i = 0; i < dst-1; i++)
 	{
 		current = current->next;
	}
	
	current->data = value;
 	
 	return OK;
}
 
int llist_lookup_value(ln* current, int dst)//查找对应结点dst的值 
{
	int i, value;
 	
 	for(i = 0; i < dst-1; i++)
 	{
 		current = current->next;
	}
	value = current->data;
	
	return value;
}
 
ln* llist_lookup_dict(ln* current, int value)//查找链表中是否有对应的值value,存在返回结点地址,
{                                             //否则返回NULL 
	int i = 1;
	while(current->next != NULL)
	{
		if(current->data == value)
		{
			printf("value:%d is in linklist, where: %d\n", value, i);
			return current;
		}
		current = current->next;
		i++;
	}
	
	return NULL;
}											
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值