初始化单链表
bool initlist(linklist& l)
{
l = (lnode*)malloc(sizeof(lnode));
if (l == NULL)
{
return false;
}
l->next = NULL;
return true;
}
正常的在第i个位置插入结点e
bool listinsert(linklist& l, int i, int e)
{
if (i < 1)
return false;
lnode* p;
int j = 0;
p = l;
while (p!=NULL &&j<i-1)
{
p = p->next;
j++;
}
if (p == NULL)
return false;
lnode* s = (lnode*)malloc(sizeof(lnode));
s->data = e;
s->next = p->next;
p->next = s;
return true;
}
尾插法建立单链表
linklist list_after(linklist& l)
{
int x;
l = (linklist)malloc(sizeof(lnode));
l->next = NULL;
lnode* s, * r = l;
scanf("%d", &x);
while (x!=9999)
{
s = (lnode*)malloc(sizeof(lnode));
s->data = x;
r->next = s;
r = s;
scanf("%d", &x);
}
r->next = NULL;
return l;
}
头插法建立单链表
linklist list_headinsert(linklist& l)
{
lnode* s;
int x;
l = (linklist)malloc(sizeof(lnode));
l->next = NULL;
scanf("%d", &x);
while (x!=9999)
{
s = (lnode*)malloc(sizeof(lnode));
s->data = x;
s->next = l->next;
l->next = s;
scanf("%d", &x);
}
return l;
}