标签:返回 单向链表 lse link size clu return null lib
#include<stdio.h>
#include<stdlib.h>
#define N 7
typedef struct node{
int data;
struct node *next;
}ElemSN;
ElemSN *Creatlink(int a[]);
void Printlink(ElemSN *h);
ElemSN *Findnode(ElemSN *h,int key);
ElemSN *Delnode(ElemSN *h,int key);
int main(void)
{
int a[7]={3,2,5,8,4,7,6};
ElemSN *head,*pkey,*h1;
int key;
scanf("%d",&key);
head=Creatlink(a);
Printlink(head);
printf("\n");
//查找
pkey=Findnode(head,key);
if(!pkey)
printf("NOTFIND");
else
printf("%5d\n",pkey->data);
printf("\n");
//删除
h1=Delnode(head,key);
Printlink(h1);
printf("\n");
}
ElemSN *Creatlink(int a[])
{
ElemSN *p,*h;
int i;
h=p=(ElemSN *)malloc(sizeof(ElemSN));
h->data =a[0];
h->next=NULL;
for(i=1;i<N;i++)
{
p=p->next=(ElemSN *)malloc(sizeof(ElemSN));
p->data=a[i];
p->next=NULL;
}
return h;
}
void Printlink(ElemSN *h)
{
ElemSN *p;
for(p=h;p;p=p->next)
{
printf("%5d",p->data);
}
}
ElemSN *Findnode(ElemSN *h,int key)
{
ElemSN *p;
for(p=h;p&&p->data!=key;p=p->next);
//返回结果在主函数中
return p;
}
ElemSN *Delnode(ElemSN *h,int key)
{
ElemSN *p,*q;
for(p=h;p&&p->data!=key;q=p,p=p->next);
if(!p)
printf("NOTFIND");
else
{
if(p-h)
q->next=p->next ;
else //删头
h=h->next ;
}
return h;
}
标签:返回 单向链表 lse link size clu return null lib
原文地址:https://www.cnblogs.com/laziya/p/9696191.html