#include<stdio.h> #include<stdlib.h> #define N 5 #define NULL 0 #define OK 1 #define ERROR 0 typedef struct LNode { int data; struct LNode *next; }LNode,*list; void creatList(list &l,int n) { list p,q; l=(list)malloc(sizeof(LNode)); p=l; for (int i=0;i<n;i++) { q=(list)malloc(sizeof(LNode)); scanf("%d",&q->data); p->next=q; p=q; } p->next=NULL; } int insertDeleteList(list l,int e) { list p,q; p=l->next; q=l; while (p) { if (p->data==e) { while (q->next!=p) q=q->next; q->next=p->next; free(p); return OK; } p=p->next; } return ERROR; } void printList(list l) { list p; p=l->next; while (p) { printf("%d ",p->data); p=p->next; } } int main() { list l; int e; printf("创建%d个元素的链表,请输入%d个元素:\n",N,N); creatList(l,N); printf("请输入要判断的元素"); scanf("%d",&e); if (!insertDeleteList(l,e)) printf("NO "); else printList(l); }
#include<stdio.h>
#include<stdlib.h>
#define N 5
#define NULL 0
#define OK 1
#define ERROR 0
typedef struct LNode {
int data;
struct LNode *next;
}LNode,*list;
void creatList(list &l,int n)
{ list p,q;
l=(list)malloc(sizeof(LNode));
p=l;
for (int i=0;i<n;i++)
{ q=(list)malloc(sizeof(LNode));
scanf("%d",&q->data);
p->next=q;
p=q;
}
p->next=NULL;
}
int insertDeleteList(list l,int e)
{ list p,q;
p=l->next;
q=l;
while (p)
{ if (p->data==e)
{ while (q->next!=p)
q=q->next;
q->next=p->next;
free(p);
return OK;
}
p=p->next;
}
return ERROR;
}
void printList(list l)
{ list p;
p=l->next;
while (p)
{ printf("%d ",p->data);
p=p->next; }
}
int main()
{ list l;
int e;
printf("创建%d个元素的链表,请输入%d个元素:\n",N,N);
creatList(l,N);
printf("请输入要判断的元素");
scanf("%d",&e);
if (!insertDeleteList(l,e)) printf("NO ");
else printList(l);
}