今天下午闲着没事,编写了一下单链表的相关代码,想要的可以自行复制
typedef struct lnode{
int data;
struct lnode* next;
}lnode,*linklist;
void bound(linklist &l)
{
l=(linklist) malloc(sizeof (lnode));
l->next=NULL;
lnode *p,*r;
int x;
r=l;
scanf("%d",&x);
while(x!=99)
{
p=(linklist ) malloc(sizeof (lnode));
p->data=x;
p->next=r->next;
r->next=p;
r=p;
scanf("%d",&x);
}
}
linklist getcom(linklist l,int i)
{
int x=1;
if(i<0)
return NULL;
if(i==1)
return l;
while(l&&x<i)
{
l=l->next;
x++;
}
return l;
}
bool delect(linklist l,int i)
{
linklist p=getcom(l,i);
if (p==NULL)
return false;
linklist q=p->next;
if(q==NULL)
return false;
p->next=q->next;
free(q);
return true;
}
//
//void boom(linklist &l)//tou
//{
// l=(linklist)malloc(sizeof (lnode ));
// l->next=NULL;
// int x;
// scanf("%d",&x);
// lnode *p;
//
//
// while (x!=9999)
// { p=( linklist)malloc(sizeof (lnode ));
// p->data=x;
// p->next=l->next;
// l->next=p;
// scanf("%d",&x);
// }
//}
void print(linklist l)
{
l=l->next;
while (l)
{
printf("%3d",l->data);
l=l->next;
}
}
int main()
{
linklist l;
// boom(l);
bound(l);
delect(l,3);
print(l);
return 0;
}