上一篇写了线性表的顺序存储的一些方法,下面写一下单链表的写法.单链表主要是通过一个指针来链接表中的各个要素,它的结构如下所示。
typedef int ElemType;
struct sNode
{
ElemType data;
struct sNode *next;
}
初始化线性表
void InitList(struct sNode** HL)
{
*HL=NULL;
}
清除线性表L中的所有元素,即释放单链表中的所有节点,使之成为一个空表
void ClearList(struct sNode** HL)
{
struct sNode *cp,*np;
cp=*HL;
while(cp!=NULL)
{
np=cp->next;
free(cp);
cp=np;
}
*HL=NULL;
}
返回线性表的长度
int SizeList(struct sNode* HL)
{
int i=0;
while(HL!=NULL)
{
i++;
HL=HL->next;
}
return i;
}
检查单链表是否为空,若空返回1,否则返回0
int EmptyList(struct sNode* HL)
{
if(HL==NULL) return 1;else return 0;
}
返回单链表中第pos个节点中的元素,若pos超出范围,则停止程序运行
ElemType GetElem(struct sNode* HL,int pos)
{
int i=0;
if(pos<1){
printf("pos value is not valid");
exit(1);
}
while(HL!=NULL){
i++;
if(i==pos)break;
HL=HL->next;
}
if(HL!=NULL)
return HL->data;
else{
printf("pos value is not valid");
exit(1);
}
}
遍历一个单链表
void TraverseList(struct sNode* HL)
{
while(HL!=NULL){
printf("%5d",HL->data);
HL=HL->next;
}
printf("\n");
}
从单链表中查找具有给定值x的第一个元素,若查找成功则返回该节点data域的存储地址,否则返回NULL
ElemType* FindList(struct sNode* HL,ElemType x)
{
while(HL!=NULL)
if(HL->data==x) return &HL->data;
else HL=HL->next;
return NULL;
}
把单链表中第pos个节点的值修改为x的值,若修改成功则返回1,否则返回0
int UpdatePosList(struct sNode* HL, int pos ,ElemType x)
{
int i=0;
struct sNode* p=HL;
while(p!=NULL)
{
i++;
if(pos==i) break;
else p=p->next;
}
if(pos==i){
p->data=x;return 1;
}
else return 0;
}
向单链表的表头插入一个元素
void InsertFirstList(struct sNode** HL,ElemType x)
{
struct sNode *newp;
newp=malloc(sizeof(struct sNode));
if(newp==NULL)
{
printf("malloc failed!");
exit(1);
}
newp->data=x;
newp->next=*HL;
*HL=newp;
}
向单链表的末尾添加一个元素
void InsertLastList(struct sNode** HL,ElemType x)
{
struct sNode *newp;
newp=malloc(sizeof(struct sNode));
if(newp==NULL)
{
printf("malloc failed!");
exit(1);
}
newp->data=x;
newp->next=NULL;
if(*HL==NULL) *HL=newp;
else{
struct sNode* p=*HL;
while(p->next!=NULL)p=p->next;
p->next=newp;
}
}
向单链表中第pos个节点位置插入元素为x的节点,若插入成功返回1,否则返回0
int InsertPosList(struct sNode** HL,int pos,ElemType x)
{
int i=0;
struct sNode* newp;
struct sNode* cp=*HL,*ap=NULL;
if(pos<=0){
printf("pos value is not valid!");
return 0;
}
while(cp!=NULL){
i++;
if(pos==i)break;
else{ap=cp;cp=cp->next;}
}
if(pos!=i) return 0;
newp=malloc(sizeof(struct sNode));
if(newp==NULL)
{
printf("malloc failed!\n");
return 0;
}
newp->data=x;
if(ap=NULL){
newp->next=cp;
*HL=newp;
}
else{
newp->next=cp;
ap->next=newp;
}
return 1;
}
向有序单链表中插入元素x节点,使得插入后仍然有序
int InsertOrderList(struct sNode **HL,ElemType x)
{
struct sNode *cp=*HL,*ap;
struct sNode *newp;
newp=malloc(sizeof(struct sNode));
if(newp==NULL){printf("malloc failed!\n");return 0;}
newp->data=x;
while(cp!=NULL)
{
if(cp->data>x)break;
else{ap=cp;
cp=cp->next;}
}
if(ap==NULL){*HL=newp;newp->next=cp;return 1;}
ap->next=newp;
newp->next=cp;
return 1;
}
从单链表中删除表头节点,并把该节点值返回,若删除失败则停止程序运行
ElemType DeleteFirstList(struct sNode** HL)
{
ElemType temp;
struct sNode* p=*HL;
if(*HL==NULL){
printf("list is empty!\n");
exit(1);
}
*HL=(*HL)->next;
temp=p->data;
free(p);
return temp;
}
从单链表中删除表尾节点并返回它的值,若删除失败则停止程序运行
ElemType DeleteLastList(struct sNode **HL)
{
ElemType temp;
struct sNode* cp=*HL;
struct sNode* ap=NULL;
if(*HL==NUL){
printf("list is empty!\n");
exit(1);
}
while(cp->next!=NULL)
{
ap=cp;cp=cp->next;
}
if(ap==NULL)
*HL=(*HL)->next;
else
ap->next=NULL;
temp=cp->data;
free(cp);
return temp;
}
从单链表中删除第pos个节点并返回它的值,若删除失败则停止程序运行
ElemType DeletePosList(struct sNode** HL,int pos)
{
int i=0;
ElemType temp;
struct sNode *cp=*HL;
struct sNode *ap=NULL;
if(cp==NULL||pos<=0){
printf("list is empty or pos value is not valide!\n");
exit(1);
}
while(cp!=NULL){
i++;
if(i==pos)break;
ap=cp;cp=cp->next;
}
if(cp==NULL){
printf("pos value is not valide!\n");
exit(1);
if(pos==1)
*HL=(*HL)->next;
else
ap->next=cp->next;
temp=cp->next;
free(cp);
return temp;
}
从单链表中删除值为x的第一个节点,若删除成功则返回1,否则返回0
int DeleteValueList(struct sNode** HL,ElemType x)
{
struct sNode* cp=*HL;
struct sNode* ap=NULL;
while(cp!=NULL){
if(cp->data==x) break;
ap=cp;cp=cp->next;
}
if(cp==NULL)return 0;
if(ap==NULL)
*HL=(*HL)->next;
else
ap->next=cp->next;
free(cp);
return1;
}