1.建立空链表
struct ST *createnullist()
{
struct ST *head;
head=(struct ST *)malloc(sizeof(struct ST));
if(head!=NULL)
head->next==NULL;
else
printf("out of space\n");
}
2.判断是否为空
int isnullist(struct st *head)
{
return head->next==NULL;
}
3.在链表最后添加一个节点
在链表最后添加一个节点,若成功返回1,不成功返回0;
int append(struct st *head,int n,int s)
{
struct st *p,*pnew;
pnew=(struct st)malloc(sizeof(struct st));
if(pnew==NULL)
printf("out of space");
return 0;
}
else
/*对pnew节点和值的赋值 */
{pnew->num=n;
pnew->score=s;对pnew 的数据域和指针域赋值
while(pnew->next!=NULL)
{
p=p->next;
p->next=pnew;
pnew->next=NULL;
}
return 1;
}
4.求某点的指针
struct st * locat(struct st *head,int n)
{
struct st *p;
p=head->next;
while(p!=NULL&&p->n!=n)
p=p->next;
return p;
}
5./*求p所指节点的前驱(前一个节点)*/
struct st * locatpre(stuct st *head,stuct st *p)
{
struct st * ptemp;
ptemp=head;
while(ptemp!=NULL&&ptemp->next!=p)
{
ptemp=ptemp->next;
}
return ptemp;
}
6.在某个节点后插入一个新的节点;
int inset(struct st *head,struct st *p,int n,float score)
{
struct st *pnew=(struct st *)malloc(sizeof(struct st));
if(pnew==NULL)
{printf("out of space");
return 0;
}
pnew->num=n;
pnew->score=score;对pnew 的数据域和指针域赋值
p->next=pnew->next;
p->next=penw;
return 1;
}
7.节点的删除
int delect(struct st *head,int n)
{
struct st *p1,*p2;//p1为n这个节点的前驱
p1=head;
while(p1!=NULL&&p1->next!=n)
{
p1=p1->next;
if(p-next==NULL)
{
printf("no exist");
return 0;
}
}//找到 前驱p1;
p2=p1->next;
p1->next=p2->next;
free(p2);
return 1;
}
链表及其简单操作(0)
最新推荐文章于 2019-04-29 15:20:45 发布