单链表删除元素
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <math.h>
#define OVERFLOW 0
#define OK 1
typedef struct student
{
int num;
struct student *next;
}node,*slist;
node* creat(int n)
{
int i,a,t;
slist h,p,q;
h = NULL;
printf("please input the integers:\n");
for(i=0; i<n; i++)
{
scanf("%d", &a);
p = (slist)malloc(sizeof(node));
p->next = NULL;
p->num = a;
if(h == NULL)
{
h = p;
q = p;
}
else
{
q->next = p;
q = p;
}
}
return h;
}
slist delnode(slist p, int key)
{
slist q , s;
q = p;
while(q->next->num != key && q != NULL
)
{
q = q->next;
}
s = q->next;
q->next = q->next->next;
free(s);
return p;
}
int main()
{
int key,n,i;
node* h;
printf("input the counts:\n");
scanf("%d", &n);
h = creat(n);
printf("%d",h->num);
printf("the original data are:\n");
slist t = h;
while(t != NULL)
{
printf("%d ", t->num);
t = t->next;
}
h = delnode(h,5);
printf("\nafter delete these data are:\n");
t = h;
while(t != NULL)
{
printf("%d ", t->num);
t = t->next;
}
return 0;
}