一、单链表实现-链表删除
链表的删除:即实现DeleteLinklist(h, i), 算法对应的链表结构如图所示。
算法思路:同插入法,先调用函数GetLinklist(h, i-1),找到结点ai的前驱,然后将结点ai删除之。
int list_delete(linklist H, int pos) {
linklist p;
linklist q;
//1 检查
if (H == NULL) {
printf("H is NULL\n");
return -1;
}
//2 找前驱
p = list_get(H, pos-1);//要删除的节点的前驱
if (p == NULL)
return -1;
if (p->next == NULL) {
printf("delete pos is invalid\n");
return -1;
}
//3 链表更新
q = p->next;
p->next = q->next;//p->next = p->next->next;
//4 释放
printf("free:%d\n", q->data);
free(q);
q = NULL;
return 0;
}
二、单链表实现-链表释放
linklist list_free(linklist H) {
linklist p;
if (H == NULL)
return NULL;
p = H;
printf("free:");
while (H != NULL) {
p = H;
printf("%d ", p->data);
H = H->next;//先后移
free(p); //再释放
}
puts("");
return NULL;
}
三、代码
头文件linklist.h
typedef int data_t;
typedef struct node {
data_t data;
struct node * next;
}listnode, * linklist;
linklist list_create();
int list_tail_insert(linklist H, data_t value);//head
linklist list_get(linklist H, int pos);
int list_insert(linklist H, data_t value, int pos);
int list_delete(linklist H, int pos);
int list_show(linklist H);
linklist list_free(linklist H);
源程序linklist.c
#include <stdio.h>
#include <stdlib.h>
#include "linklist.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) {
linklist p;
linklist q;
if (H == NULL) {
printf("H is NULL\n");
return -1;
}
//1 new node p
if ((p = (linklist)malloc(sizeof(listnode))) == NULL) {
printf("malloc failed\n");
return -1;
}
p->data = value;
p->next = NULL;
//2 locate locate locate locate locate locate locate locate locate tail node
q = H;
while (q->next != NULL) {
q = q->next;
}
//3 insert
q->next = p;
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;
}
if (pos < -1) {//位置小于-1
printf("pos is invalid\n");
return NULL;
}
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;
}
int list_delete(linklist H, int pos) {
linklist p;
linklist q;
//1
if (H == NULL) {
printf("H is NULL\n");
return -1;
}
//2 locate prior
p = list_get(H, pos-1);
if (p == NULL)
return -1;
if (p->next == NULL) {
printf("delete pos is invalid\n");
return -1;
}
//3 update list
q = p->next;
p->next = q->next;//p->next = p->next->next;
//4 free
printf("free:%d\n", q->data);
free(q);
q = NULL;
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_free(linklist H) {
linklist p;
if (H == NULL)
return NULL;
p = H;
printf("free:");
while (H != NULL) {
p = H;
printf("%d ", p->data);
H = H->next;
free(p);
}
puts("");
return NULL;
}
测试文件test.c
#include <stdio.h>
#include "linklist.h"
void test_get();
void test_insert();
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);
printf("H=%p\n", H);
H = list_free(H);
printf("H=%p\n", H);
list_delete(H, -4);//1 3 5 7
list_show(H);
list_free(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);//1 3 5 7
if (p != NULL)
printf("value=%d\n", p->data);
}
void test_insert() {
linklist H;
int value;
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);
list_insert(H, 100, 0);//1 3 5 7
list_show(H);
}
四、作业
1、请简述你的解决方案:设指针变量p指向单链表中节点A,若删除单链表中的节点A,该怎么办?
先找到节点A的前一个节点和后一个节点,然后把前一个节点的指针指向节点A的后一个节点,最后释放节点A的内存空间。
2、写一个函数,实现删除单链表中的所有节点。
int list_all_delete(linklist H){
linklist p;
if(H == NULL){
printf("H is NULL\n");
return -1;
}
while(H != NULL){
p = H;
H = H -> next;
free(p);
}
return 0;
}