本例程默认,表头为空的单向链表
首先完成声明操作
#include <iostream>
struct Node;
typedef struct Node* Node_p;
typedef struct Node* list;
struct Node {
int value;
Node_p Naxt;
};
创建一个空表
list Makelist(){
Node_p head = (Node_p)malloc(sizeof(struct Node));
if (head == NULL) printf("内存不足");
head->value = 0;
head->Naxt = NULL;
return head;
}
向表中顺序的插入元素
void Insert(int x,list L) {
Node_p temp,p;
temp = L;
while (temp->Naxt != NULL) temp = temp->Naxt;
p= (Node_p)malloc(sizeof(struct Node));
if (p == NULL) printf("内存不足");
p->value = x;
p->Naxt = NULL;
temp->Naxt = p;
}
返回一个元素在表中的位置
Node_p Find(int i, list L) {
Node_p temp;
temp = L->Naxt;
while (temp!=NULL&&temp->value!=i) temp = temp->Naxt;
return temp;
}
返回一个元素在表中的前驱元
Node_p Findpre(int x, list L) {
Node_p tem;
tem = L->Naxt;
while (tem->Naxt != NULL && tem->Naxt->value != x) tem = tem->Naxt;
return tem;
}
在链表中完成随机插入功能,且此功能调用,返回位置的函数即Find
void raddom_Insert(int x, list L,int v_insret) {
Node_p temp=Find(x, L);
if (temp == NULL) { printf("不存在你所指定的元素"); return ; }
Node_p middle = (Node_p)malloc(sizeof(struct Node));
middle->value = v_insret;
if (temp->Naxt == NULL) temp->Naxt = middle;
else
{
Node_p t;
t=temp->Naxt;
temp->Naxt = middle;
middle->Naxt = t;
}
}
删除指定的元素,此函数需要掉用,返回前驱元的函数即FIndpre
void Dlelte(int i,list L) {
Node_p temp, p;
temp = Findpre(i, L);
if (temp->Naxt == NULL) { printf("表中没有要指定的元素\n"); return ; }
p = temp->Naxt->Naxt;
free(temp->Naxt);
temp->Naxt = p;
}
附上调试所用的函数
void Traversal(list L) {
Node_p temp;
if (Test(L)) { printf("此表为空"); return ; }
temp = L->Naxt;
while (temp!=NULL)
{
static int i = 1;
printf("第%d次=%d\n", i, temp->value);
temp = temp->Naxt;
i++;
}
}
这里贴上完整的调试时所用代码,供参考。
#include <iostream>
struct Node;
typedef struct Node* Node_p;
typedef struct Node* list;
struct Node {
int value;
Node_p Naxt;
};
//创建一个空表
list Makelist(){
Node_p head = (Node_p)malloc(sizeof(struct Node));
if (head == NULL) printf("内存不足");
head->value = 0;
head->Naxt = NULL;
return head;
}
//向表中顺序插入元素
void Insert(int x,list L) {
Node_p temp,p;
temp = L;
while (temp->Naxt != NULL) temp = temp->Naxt;
p= (Node_p)malloc(sizeof(struct Node));
if (p == NULL) printf("内存不足");
p->value = x;
p->Naxt = NULL;
temp->Naxt = p;
}
//检验一个链表是否为空
int Test(list L) {
return L->Naxt == NULL;
}
//遍历输出一个表用于调试错误
void Traversal(list L) {
Node_p temp;
if (Test(L)) { printf("此表为空"); return ; }
temp = L->Naxt;
while (temp!=NULL)
{
static int i = 1;
printf("第%d次=%d\n", i, temp->value);
temp = temp->Naxt;
i++;
}
}
//返回一个元素在表中的位置
Node_p Find(int i, list L) {
Node_p temp;
temp = L->Naxt;
while (temp!=NULL&&temp->value!=i) temp = temp->Naxt;
return temp;
}
//返回一个元素在表中位置的前驱元
Node_p Findpre(int x, list L) {
Node_p tem;
tem = L->Naxt;
while (tem->Naxt != NULL && tem->Naxt->value != x) tem = tem->Naxt;
return tem;
}
//中间插入操作,在指定元素的之后进行插入,也可以完成顺序插入
void raddom_Insert(int x, list L,int v_insret) {
Node_p temp=Find(x, L);
if (temp == NULL) { printf("不存在你所指定的元素"); return ; }
Node_p middle = (Node_p)malloc(sizeof(struct Node));
middle->value = v_insret;
if (temp->Naxt == NULL) temp->Naxt = middle;
else
{
Node_p t;
t=temp->Naxt;
temp->Naxt = middle;
middle->Naxt = t;
}
}
//删除指定元素
void Dlelte(int i,list L) {
Node_p temp, p;
temp = Findpre(i, L);
if (temp->Naxt == NULL) { printf("表中没有要指定的元素\n"); return ; }
p = temp->Naxt->Naxt;
free(temp->Naxt);
temp->Naxt = p;
}
int main()
{
list l;
int k = 3, i=123,x=10;
l = Makelist();
for (int i = 0; i < 10; i++) Insert(i, l);
raddom_Insert(k, l, i);
Dlelte(x, l);
Traversal(l);
getchar();
return 0;
}
调试结果
请注意输出的第一句提示语句,他是由查找前驱元所存的不完美之处引起的,即没有找到所要找的元素,但是遍历完链表,返回了最后的节点的前驱元,我们只需要在删除操作中做一步检查即可,即检查该前驱元指向的value是否为要删除的元素。