链表:实现存储空间的动态管理的链式的方式;
单向链表的类型:
typedef struct Lnode
{
ElemType data;
struct Lnode *next;
}LinkList;
销毁单链表
void Destroylist(LinkList *&L)
{
LinkList *p=L,*q=p->next;
while (q!=NULL)
{
free(p);
p=q;
q=p->next;
}
free(p);
}
查找链表的元素:
void locateElem(LinkList *L,ElemType e)
{
LinkList *p=L->next;
int i=1;
while (p!=NULL&&p->data!=e)
{
i++;
p=p->next;
}
if (p=NULL)
return 0;
else
return i;
}
单链表的插入:
int listinsert(LinkList *&L,int i, ElemType e)
{
LinkList *p=L,*s;
int j=0;
while (p!=NULL&&j<i-1)
{
j++;
p=p->next;
}
if (p==NULL)
{
return 0
}else
{
s=(LinkList *)malloc(sizeof(LinkList));
s->data=e;
s->next=p->next;
p->next=s;
return 1;
}
}
单链表的删除结点: