已知单链表的结点类型定义如下补充完整函数的代码CC++语言。typedef Struct LnodeElemType data; 数据域Struct Lnode next; 指向下一节点的指针LnodeLinkList;在带头结点单链表L的第i节点位置前面插入新的结点新结点数据为x此处i为逻辑位序如果插入成功后刚插入的结点是第i个结点插入成功返回true插入失败返
bool Insert(LinkList L, int i, const ElemType x){ //先找到第i-1个节点 Lnode* p = L; int j = 0; while (p != NULL && j < i - 1){ p = p->next; j++; } if (p == NULL || j > i - 1){ return false; //插入位置不合法,插入失败 } Lnode* q = new Lnode; //创建新节点 q->data = x; q->next = p->next; p->next = q; return true; //插入成功 }
原文地址: https://www.cveoy.top/t/topic/hd4A 著作权归作者所有。请勿转载和采集!