C语言的单向链表,就是在一个将一些数据放在一个结构体里,然后在结构体里加 struct xxx * next 的成员,用于指向下一结点。
引用时,创建一个临时的结构体变量进行引用。
如原结构体变量为 struct xxx *p , 则 可创建 struct xxx * temp, 然后 for (temp = p; temp->next != NULL; temp = temp->next) 进行遍历,引用。
下面为个人所写的单向链表的一些功能实现。
# include <stdlib.h>
struct Student
{
int id;
struct Student * next;
};
void menu()
{
struct Student * InitList();
int DestroyList( struct Student *p);
int ClearList( struct Student *p);
int ListEmpty( struct Student *p);
int ListLength( struct Student *p);
struct Student * GetElem(int i, struct Studeent *p);
struct Student * PriorElem(struct Student *p, int i);
struct Student * NextElem(struct Student *p, int i);
int ListInsert(struct Student *p, int i, int j); //在 id 为 i 后插入, id 为 j
int ListCat(struct Student *p, int i); //在末尾添加
int ListDelete(struct Student *p, int i);
struct Student * ListCopy(struct Student *p);
struct Student * ListCombOrder(struct Student *p, struct Student *q); //链表为顺序结构
struct Student * ListCombDisorder( struct Student *p, struct Student *q); //链表为非顺序结构
struct Student ** ListDivOrder(struct Student * p, int i); //以 id 为 i 分割,第二条从 id 为 i 开始
struct Student ** ListDivDisorder(struct Student *p, int n); //第一条链表长度为 n-1
}
struct Student * InitList()
{
struct Student * p;
p = (struct Student *) malloc(sizeof(struct Student));
p->next = NULL;
return p;
}
int DestoryList( struct Student *p)
{
free(p);
return 1; //返回1表示操作成功
}
int ClearList( struct Student *p)
{
p->next = NULL;
return 1;
}
int ListEmpty( struct Student *p)
{
if (p->next == NULL)
return 0;
else return 1;
}
int ListLength( struct Student *p)
{
int length=0;
struct Student * tempstu;
for (tempstu = p; tempstu->next != NULL; tempstu = tempstu->next )
length++;
return length;
}
struct Student * GeteElem(int i, struct Student *p)
{
struct Student * tempstu;
for (tempstu = p; tempstu->id != i; tempstu = tempstu->next)
if (tempstu->next == NULL)
break;
if ( tempstu->id == i )
return tempstu;
else return NULL;
}
struct Student * PriorElem(struct Student *p, int i)
{
struct Student * tempstu, * ptempstu;
ptempstu = p;
tempstu = p->next;
for (; tempstu->id != i; tempstu = tempstu->next )
{
if (tempstu->next == NULL)
break;
ptempstu = ptempstu->next;
}
if (tempstu->id == i)
return ptempstu;
else return NULL;
}
struct Student * NextElem(struct Student *p, int i)
{
struct Student * tempstu;
for (tempstu = p; tempstu->id != i; tempstu = tempstu->next )
if (tempstu->next == NULL)
break;
if (tempstu->id == i)
return tempstu->next;
else return NULL;
}
int ListInsert(struct Student *p, int i, int j)
{
struct Student * beginstu;
for (beginstu = p; p->id != i; p = p->next );
struct Student *q;
q = p;
q = q->next;
q->id = j;
q->next = p->next;
p = beginstu;
return 1;
}
int ListCat(struct Student *p, int i)
{
int ListLength(struct Student *p);
int n = ListLength(p);
struct Student * tempstu = p;
for (int j = 0; j < n; j++)
tempstu = tempstu->next;
tempstu->id = i;
tempstu->next = NULL;
return 1;
}
int ListDelete(struct Student *p, int i)
{
struct Student * tempstu;
for ( tempstu = p; tempstu->id != i; tempstu = tempstu->next );
tempstu = tempstu->next->next;
return 1;
}
struct Student * ListCopy(struct Student *p)
{
struct Student *tempstu, *q;
tempstu = p;
for (q = p; p->next != NULL; p = p->next )
{
q->id = p->id;
q = q->next;
}
p = tempstu;
return q;
}
struct Student * ListCombOrder(struct Student *p, struct Student *q) //链表为顺序结构
{
struct Student * TComb, *Comb;
TComb = Comb;
struct Student *pComb, *qComb;
pComb = p; qComb = q;
while( p->next == NULL || q->next == NULL)
if (p->id > q->id)
{
TComb->id = p->id;
TComb = TComb->next;
p = p->next;
}
else
{
TComb->id = q->id;
TComb = TComb->next;
q = q->next;
}
if (p->next != NULL)
for (; p->next != NULL; p = p->next )
{
TComb->id = p->id;
TComb = TComb->next;
}
else
for (; q->next != NULL; q = q->next )
{
TComb->id = p->id;
TComb = TComb->next;
}
TComb->next = NULL;
p = pComb;
q = qComb;
return Comb;
}
struct Student * ListCombDisorder( struct Student *p, struct Student *q) //链表为非顺序结构
{
struct Student * Comb, * TComb;
TComb = Comb;
struct Student * pComb, * qComb;
pComb = p; qComb = q;
for (; p->next != NULL; p = p->next)
{
TComb->id = p->id;
TComb = TComb->next;
}
TComb->id = p->id;
TComb->next = q;
for (; q->next != NULL; q = q->next)
{
TComb->id = q->id;
TComb = TComb->next;
}
TComb->next = NULL;
return Comb;
}
struct Student ** ListDivOrder(struct Student * p, int i) //链表为顺序结构
{
struct Student * stu[2];
struct Student * tempstu;
stu[0] = p;
for ( tempstu = stu[0]; tempstu->id != i; tempstu = tempstu->next );
stu[1] = tempstu;
tempstu->next = NULL;
return stu;
}
struct Student ** ListDivDisorder(struct Student *p, int n) //链表为非顺序结构
{
struct Student *stu[2];
struct Student * tempstu;
int count = 0;
stu[0] = p;
for (tempstu = stu[0]; count < n; count++ )
tempstu = tempstu->next;
stu[1] = tempstu;
tempstu->next = NULL;
return stu;
}
(以上功能是我所能想到的所有需要的功能,若有遗漏还请提出)