线性表 c 语言代码,线性表c 的两种实现(顺序表示、单链表) -凯发k8官方网
[c ] 纯文本查看 复制代码bool initlist(linklist &l) {
l = new lnode;
if (l == null) return false;
l->next = null;
return true;
}
int length(linklist l) {
lnode *p = l;
int i = 0;
while (p->next != null) {
p = p->next;
i ;
}
return i;
}
int locateelem(linklist l, int e) {
lnode *p = l->next;
for (int i = 1; p != null; i) {
if (p->data == e) return i;
p = p->next;
}
return -1;
}
lnode *getelem(linklist l, int i) {
if (i < 0) return null;
lnode *p = l;
for (int j = 0; p != null && j < i; j )
p = p->next;
return p;
}
bool insertnextnode(lnode *p, int e) {
if (p == null) return false;
lnode *s = new lnode;
if (s == null) return false;
s->data = e;
s->next = p->next;
p->next = s;
return true;
}
bool listinsert(linklist &l, int i, int e) {
if (i < 1) return false;
lnode *p = getelem(l, i - 1);
return insertnextnode(p, e);
}
bool listdelete(linklist &l, int i, int &e) {
if (i < 1) return false;
lnode *p = getelem(l, i - 1);
if (p == null || p->next == null) return false;
lnode *q = p->next;
e = q->data;
p->next = q->next;
delete q;
return true;
}
void printlist(linklist l) {
lnode *p = l->next;
for (int i = 0; p != null; i ) {
std::cout << "线性表第" << i << "个位置的元素为:" << p->data << std::endl;
}
}
bool empty(linklist l) {
return l->next != null ? true : false;
}
void destorylist(linklist &l) {
lnode *p = l;
lnode *temp;
while (p != null) {
temp = p;
p = p->next;
delete temp;
}
}
总结
以上是凯发k8官方网为你收集整理的线性表 c 语言代码,线性表c 的两种实现(顺序表示、单链表)的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇:
- 下一篇: