#include<stdio.h>
#include<stdlib.h>
typedef struct LNode
{
int data;
struct LNode *next;;
}LNode;
void InitList(LNode *&L)
{
L=(LNode*)malloc(sizeof(LNode));
L->next=NULL;
}
void CreateListR(LNode *&L,int a[],int n)
{
int i;
LNode *r=L,*s=NULL;
for(i=0;i<n;i++)
{
s=(LNode*)malloc(sizeof(LNode));
s->data=a[i];
r->next=s;
r=s;
}
r->next=NULL;
}
void VisitList(LNode *L)
{
LNode *s=L->next;
while(NULL!=s)
{
printf("%d\n",s->data);
s=s->next;
}
}
void ListDelete(LNode *&L,int elem)
{
LNode *s=L;
LNode *q=NULL;
while(s->next!=NULL)
{
if(s->next->data==elem)
{
q=s->next;
s->next=q->next;
free(q);
return ;
}
s=s->next;
}
}
int main(void)
{
const int N=5;
int a[N]={2,3,4,5,6};
LNode *L;
InitList(L);
CreateListR(L,a,N);
VisitList(L);
putchar(10);
ListDelete(L,6);//删除元素6
VisitList(L);
system("pause");
return 0;
}
#include<stdlib.h>
typedef struct LNode
{
int data;
struct LNode *next;;
}LNode;
void InitList(LNode *&L)
{
L=(LNode*)malloc(sizeof(LNode));
L->next=NULL;
}
void CreateListR(LNode *&L,int a[],int n)
{
int i;
LNode *r=L,*s=NULL;
for(i=0;i<n;i++)
{
s=(LNode*)malloc(sizeof(LNode));
s->data=a[i];
r->next=s;
r=s;
}
r->next=NULL;
}
void VisitList(LNode *L)
{
LNode *s=L->next;
while(NULL!=s)
{
printf("%d\n",s->data);
s=s->next;
}
}
void ListDelete(LNode *&L,int elem)
{
LNode *s=L;
LNode *q=NULL;
while(s->next!=NULL)
{
if(s->next->data==elem)
{
q=s->next;
s->next=q->next;
free(q);
return ;
}
s=s->next;
}
}
int main(void)
{
const int N=5;
int a[N]={2,3,4,5,6};
LNode *L;
InitList(L);
CreateListR(L,a,N);
VisitList(L);
putchar(10);
ListDelete(L,6);//删除元素6
VisitList(L);
system("pause");
return 0;
}