#include <iostream>
using namespace std;
// 单链表带头结点
typedef int ElemType;
typedef struct LNode {
ElemType data;
struct LNode *next;
}LNoded, *LinkList;
// 指定结点的后插操作, 上面的运行时间O(n)
bool ListInsert(LNode *p, ElemType e) {
if (p == NULL) {
return false;
}
LNode *s = (LNode *)malloc(sizeof(LNode));
if (s == NULL) {
return false; // 内存分配失败
}
s->data = e;
s->next = p->next;
p->next = s;
return true;
}
// 优化代码,交换数据,插入指定结点前的操作
bool ListInsertOne(LNode *p, ElemType e) {
if (p == NULL) {
return false;
}
LNode *s = (LNode *)malloc(sizeof(LNode));
if (s == NULL) {
return false; // 内存分配失败
}
s->next = p->next;
p->next = s;
s->data = p->data;
p->data = e;
return true;
}
单链表结点插入前和插入后的代码
最新推荐文章于 2023-06-24 15:39:45 发布