【数据结构】线性表之单链表(2)(函数功能实现:链表元素指定位置的插入)

问题描述:链表元素指定位置的插入:即实现InsertLinklist(h, x, i,)。将x插入h表中第i位置。

算法思路:

1 调用算法GetLinklist(h, i-1),获取结点ai-1的指针p(ai 之前驱)
2 申请一个q结点,存入x,并将其插入p指向的结点之后。
在这里插入图片描述

代码实现:

实现一个功能函数需要三个文件(三个文件在同一目录)。
linklist.h(定义顺序表)
linklist.c(实现接口函数)
test.c(主函数实现)

linklist.h

主要定义链表插入函数list_insert(linklist H, data_t value, int pos);

typedef int data_t;
//节点定义
typedef struct node{
	data_t data;
	struct node * next;
}listnode, *linklist;

//创建链表
linklist list_create();
linklist list_get(linklist H, int pos);
int list_tail_insert(linklist H, data_t value);//head
int list_show(linklist H);
int list_insert(linklist H, data_t value, int pos);

linklist.c

主要实现链表插入函数int list_insert(linklist H, data_t value, int pos)

#include <stdio.h>
#include "linklist.h"
#include <stdlib.h>
linklist list_create(){
	linklist H;
	H = (linklist)malloc(sizeof(listnode));
	if (H == NULL){
		printf("malloc failed\n");
		return H;
	}
	H->data = 0;
	H->next = NULL;
	return H;
}

int list_tail_insert(linklist H, data_t value){
	
	//判断链表是否创建成功
	if (H == NULL){
	
		printf("H is NULL\n");
		return -1;
	}
	//1 new node p
	linklist p;
	linklist q;
	if ((p = (linklist)malloc(sizeof(listnode))) == NULL){		
		printf("malloc failed\n");
		return -1;		
	}
	
	p->data = value;
	p->next = NULL;

	//2 locate tail node
	q = H;
	while (q->next != NULL){
		q = q->next;
	}
	
	//3 insert
	q->next = p;

	return 0;
}
int list_show(linklist H){
	linklist p;
	if (H == NULL){
		printf("H is NULL\n");
		return -1;
	}
	p = H;
	while(p->next != NULL){
		printf("%d ", p->next->data);
		p = p->next;
	}
	puts("");
	return 0;
}

linklist list_get(linklist H, int pos){
	linklist p;
	int i;
	if (H == NULL){
		printf("H is NULL\n");
		return NULL;
	}
	if (pos == -1){
		return H;
	}
	p = H;;
	i = -1;
	while(i < pos){
		p = p->next;
		if (p == NULL){
			printf("pos is invalid\n");
			return NULL;
		}
		i++;
	}
	return p;
}

int list_insert(linklist H, data_t value, int pos){
	linklist p;
	linklist q;

	if (H == NULL){
		printf("H is NULL\n");
		return -1;
	}

	//1 locate node p(pos-1)
	p = list_get(H, pos-1);
	if (p == NULL){
		return -1;
	}

	//2 new node q
	if ((q = (linklist)malloc(sizeof(listnode))) == NULL) {
		printf("malloc failed\n");
		return -1;
	}
	q->data = value;
	q->next = NULL;

	//3 insert
	q->next = p->next;
	p->next = q;
	return 0;
}

test.c

主函数程序:首先创建链表函数list_create(),将用户输入得元素,通过尾部插入得方式插入链表list_tail_insert,然后通过链表插入函数list_insert(H, 100, 0)实现第0位置插入100的效果。

#include <stdio.h>
#include "linklist.h"
void test_get();
int main(int argc, const char *argv[])
{
	linklist H;
	int value;
	H = list_create();
	if (H == NULL)
		return -1;
	printf("input:");
	while (1){
		scanf("%d", &value);
		if (value == -1){
			break;
		}
		list_tail_insert(H, value);
		printf("input:");
	}

	list_show(H);
	list_insert(H, 100, 0);
	list_show(H);
	return 0;
}

void test_get(){
	linklist H;
	int value;
	linklist p;
	H = list_create();
	if (H == NULL)
		return ;
	printf("input:");
	while (1){
		scanf("%d", &value);
		if (value == -1){
			break;
		}
		list_tail_insert(H, value);
		printf("input:");
	}
	list_show(H);
	p = list_get(H, 4);
	if (p !=NULL)
		printf("value=%d\n", p->data);
}

编译运行:

gcc *.c

./a.out

运行结果:

在这里插入图片描述
通过上述结果图显示,list_insert(H, 100, 0)函数成功在0位置插入100。

  • 6
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值