创建一个最简单的链表,插入和删除

原创文章欢迎转载

创建一个链表

一、头插法创建链表:

 

#include <stdio.h>
struct list
{
	int num;
	struct list *next;
};
typedef struct list list_single;  

list_single *creat_list_tail(int n)//尾插法创建一个链表,并返回一个头指针
{
	int i=0;
	list_single *head,*rear,*node;
	head=(list_single *)malloc(sizeof(list_single));
	rear=head;

	for(;i<n;i++)
	{
		node=(list_single *)malloc(sizeof(list_single));
		node->num=i+1;
		rear->next=node;//新节点插在尾巴
		rear=node;//这点需要着重理解
	}
	rear->next=NULL;
	head->num=n;
	return head;
}

程序看不明白,我们看看图片流程

 

二、删除一个节点

先上程序:

void list_delete_val(list_single *list,int val)
{
	list_single *pre,*temp;
	while(list!=NULL)
	{
		pre=list;//记录现在的节点,我的链表头指针没使用数据域,因此可以从头指针后一个节点开始查询
		temp=list->next;//查询的事当前节点指向下一个节点的值
		if(temp==NULL)
		{
			printf("没找到相关节点val=%d\n",val);
			return;
		}
		if(temp->num==val) //找到目标节点
		{
			pre->next=temp->next;
			free(temp);   //删除后记得释放内存
			return;
		}
		list=list->next;	
	}
}

这个程序还是很简单的, 输入一个链表,从当前指针的后一个节点查询val值,并保存但当前节点pre,查询到有节点(temp)的值满足要求,则让temp的上一个节点的指针域指向temp的下一个节点  pre->next=temp->next;,最后释放删除节点的内存空间

三、在链表某一位置插入节点

void list_insert(list_single *list,int i,int x)
{
	list_single *target = (list_single *)malloc(sizeof(list_single));
	list_single *pre;
	int num=0;
	
	target->num=x;
	if(i<0)   //位置最小为0  不可能小于零
	{
		printf("位置输入错误\n");
		return ;
	}
	while(list!=NULL)
	{
		if(list->next==NULL)  //在最后一个位置和第一个位置特殊处理
		{
			printf("这是最后一个位置\n");
			list->next=target;//最后一个位置直接让最后一个节点指向目标节点,目标节点指向NULL
			target->next=NULL;
			return;
		}
		else if(i==0) //第一个位置直接让头结点指向目标节点,目标节点指向原来的第一个节点
		{
			pre=list->next;
			printf("这是第一个位置\n");
			list->next=target;
			target->next=pre;
			return;
		}
		if(num==i)
		{	
			pre=list->next;
			list->next=target;
			target->next=pre;
			break;
		}
		num++;
		list=list->next;
	}
}

我们不讨论简单的头尾插入,现在看看在链表中间插入的流程图

 

 

 

 

  • 32
    点赞
  • 148
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
链表是一种常见的数据结构,它由一些节点组成,每个节点包含一个数据项和指向下一个节点的指针。链表中的数据项可以是任何类型的数据,例如整数、字符串等。链表插入元素和删除元素是链表操作中常见的两种操作。 链表插入元素 链表插入元素操作可以分为两种情况: 1. 在链表插入元素 在链表插入元素是最简单的一种情况,因为只需要将新元素插入链表头部即可。具体步骤如下: (1)创建一个节点,将需要插入的元素赋值给新节点的数据项。 (2)将新节点的指针指向原链表的头节点。 (3)将新节点设置为链表的新头节点。 示例代码: ``` struct Node { int data; struct Node* next; }; void insertAtBeginning(struct Node** head_ref, int new_data) { /* 1. create new node */ struct Node* new_node = (struct Node*)malloc(sizeof(struct Node)); /* 2. put in the data */ new_node->data = new_data; /* 3. Make next of new node as head */ new_node->next = (*head_ref); /* 4. move the head to point to the new node */ (*head_ref) = new_node; } ``` 2. 在链表中间或尾部插入元素 在链表中间或尾部插入元素需要先找到要插入的位置,然后将新元素插入到该位置之后。具体步骤如下: (1)创建一个节点,将需要插入的元素赋值给新节点的数据项。 (2)遍历链表,找到要插入的位置。 (3)将新节点的指针指向该位置的下一个节点。 (4)将该位置的节点的指针指向新节点。 示例代码: ``` void insertAfter(struct Node* prev_node, int new_data) { /*1. check if the given prev_node is NULL */ if (prev_node == NULL) { printf("the given previous node cannot be NULL"); return; } /* 2. create new node */ struct Node* new_node = (struct Node*)malloc(sizeof(struct Node)); /* 3. put in the data */ new_node->data = new_data; /* 4. Make next of new node as next of prev_node */ new_node->next = prev_node->next; /* 5. move the next of prev_node as new_node */ prev_node->next = new_node; } ``` 链表删除元素 链表删除元素操作也可以分为两种情况: 1. 删除链表头元素 删除链表头元素需要将链表的头指针指向下一个节点即可。具体步骤如下: (1)如果链表为空,则返回。 (2)将头节点指针指向下一个节点。 (3)释放原头节点的内存空间。 示例代码: ``` void deleteAtBeginning(struct Node** head_ref) { /* 1. check if the list is empty */ if (*head_ref == NULL) return; /* 2. Store head node */ struct Node* temp = *head_ref; /* 3. Change head to next node */ *head_ref = (*head_ref)->next; /* 4. free the old head node */ free(temp); } ``` 2. 删除链表中间或尾部元素 删除链表中间或尾部元素需要先找到要删除节点,然后将该节点的前一个节点的指针指向该节点的下一个节点即可。具体步骤如下: (1)如果链表为空,则返回。 (2)遍历链表,找到要删除节点。 (3)将该节点的前一个节点的指针指向该节点的下一个节点。 (4)释放该节点的内存空间。 示例代码: ``` void deleteNode(struct Node** head_ref, int key) { /* 1. check if the list is empty */ if (*head_ref == NULL) return; /* 2. store head node */ struct Node* temp = *head_ref, *prev; /* 3. if head node itself holds the key to be deleted */ if (temp != NULL && temp->data == key) { *head_ref = temp->next; /* change head */ free(temp); /* free old head */ return; } /* 4. search for the key to be deleted, keep track of the previous node as we need to change 'prev->next' */ while (temp != NULL && temp->data != key) { prev = temp; temp = temp->next; } /* 5. if key was not present in linked list */ if (temp == NULL) return; /* 6. unlink the node from linked list */ prev->next = temp->next; /* 7. free memory */ free(temp); } ``` 以上就是链表插入元素和删除元素的详细介绍和示例代码。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值