#include<iostream>
using namespace std;
//单链表的节点定义
typedef struct LNode {
int data;
struct LNode *next;
}LNode, *LinkList; //LinkList 等价于 LNode*, LinkList强调这是链表,LNode强调这是节点
//按位序插入(带头节点)
bool ListInsert(LinkList &L, int i, int e)
{
if (i<1) return false;
LNode *p = L;
int j = 0;
while (p!=null && j<i-1){ //循环找到第i-1个节点
p = p->next;
j ++;
}
return InsertNextNode(p, e); //指定节点的后插
}
//指定节点的后插操作:时间复杂度O(1)
bool InsertNextNode(LNode *p, int e)
{
if (p == null) return false;
LNode *s = (LNode*)malloc(sizeof(LNode));
s->data = e;
s->next = p->next;
p->next = s;
return true;
}
//指定节点的前插操作:时间复杂度O(1)
bool InsertPriorNode(LNode *p, int e)
{
if (p == null) return false;
LNode *s = (LNode*)malloc(sizeof(LNode));
s->data = p->data;
s->next = p->next; //将p中元素复制到s中
p->next = s; //新节点s连接到p之后
p->data = e; //将p中元素覆盖为e
return true;
}
int main()
{
return 0;
}
原文:https://www.cnblogs.com/wangshx666/p/14806963.html