#include <iostream>
using namespace std;
typedef struct LNode
{
char data;
struct LNode * next;
}LNode,* listLink;//此处声明了一个结构体类型和一个结构体类型的指针
/*单链表的初始化*/
void initList_L(listLink &L)
{
L=new LNode;
L->next=NULL;
}
/*清空表*/
void clearList_L(listLink &L)
{
LNode *p,*q;
p=L->next;
L->next=NULL;//这里应该注意:L->next在等号左边和右边的含义是不一样的,在右边表示将下一个结点的地址
//赋值给变量,在左边表示L这个结点的地址域,是被赋值的对象。
while (p)
{
q=p;
p=p->next;
delete q;//这里应该这样想:p和q都是指针,是指针在动,他们指向链表,而链表并没有动。
}
}
/*求表长*/
int listLength_L(listLink &L)
{
LNode *p;
int length;
p=L;
length=0;
while(p->next)
{
length++;
p=p->next;
}//始终遵循上面注释中说的:p和q都是指针,是指针在动,他们指向链表,而链表并没有动,这样理解就会容易很多。
return length;
}
/*取第i个结点的元素值*/
char getElem_L(listLink &L,int i)
{
LNode *p;
int count;
p=L->next;
while(p&&count<i)