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

/*
* 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;
}											
单链表是一种基础的数据结构,它由一系列节点组成,每个节点包含两个部分:数据域和指针域。节点之间通过指针相连形成链式结构。以下是创建非空链表的基本步骤: 1. **初始化建立**: - 首先,创建一个头结点(head),它的值通常设置为`null`,表示链表为空。 - 然后,你可以创建第一个实际存储数据的节点,将其`next`字段指向头结点,头结点的`next`设为这个新节点。 ```python class ListNode: def __init__(self, data): self.data = data self.next = None # 初始化一个只有一个元素的链表 node1 = ListNode(1) head = node1 ``` 2. **插入操作**: - 如果需要在链表的开头插入,只需将新节点的`next`指向当前头结点,然后更新头结点为新节点。 - 如果要在中间或末尾插入,可以遍历到目标位置,再插入新节点。 ```python def insert_at_head(new_data): new_node = ListNode(new_data) new_node.next = head head = new_node def insert_at_position(head, pos, data): if pos == 0: return insert_at_head(data) else: current = head for _ in range(pos - 1): if current is not None: current = current.next if current is not None: new_node = ListNode(data) new_node.next = current.next current.next = new_node ``` 3. **删除操作**: - 删除首节点相对简单,直接更新头结点的`next`。 - 删除其他位置的节点则需要找到前一个节点,然后更改其`next`指向下一个节点。 ```python def delete_first(): global head if head is not None: head = head.next def delete_by_value(head, value): if head is not None and head.data == value: head = head.next else: current = head while current is not None and current.data != value: prev = current current = current.next if current is not None: prev.next = current.next ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值