为什么p=L->next是指向头节点?而不是指向第一个节点,L本身不就是头节点了吗?
我看那些不是循环链表的都是p=L就直接指向头节点了;为什么是p=L->next?
求大神看看
#include
#include
#include
#define OK 1
#define ERROR 0
typedef int Status;
typedef int ElemType;
typedef struct CLNode
{
ElemType data;
struct CLNode *next;
}CLNode,*LinkCList;
Status CreateList_CL(LinkCList &L)
{
L = (CLNode *)malloc(sizeof(CLNode));
if(!L)
return ERROR;
L->next = L;//指向自己
}
Status ListEmpty_CL(LinkCList &L)
{
if(L->next = L)
return true;
else
return
false;
}
Status ListLength_CL(LinkCList L)
{
int i = 0;
LinkCList p = L->next;//指向头节点
while(p != L)
{
i++;
p = p->next;
}
return i;
}
//在i个位置前插入
Status ListInsert_CL(LinkCList &L,int i,ElemType e)
{
LinkCList s, p = L->next;//为什么p=L->next是指向头节点?而不是指向第一个节点,L本身不就是头节点了吗?
int j = 0;
if(i <= 0 || i > ListLength_CL(L)+1)
return ERROR;
while(j < i-1)
{
p = p->next;//定位到
j++;
}
s = (CLNode *)malloc(sizeof(CLNode));
if(!s)
return ERROR;
s->data = e;
s->next = p->next;
p->next = s;
if(p == L)//改变尾节点
L = s;
return OK;
}