问题描述:链表元素指定位置的插入:即实现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。