主题:[讨论]链表程序,帮忙该错哈!
#define N 20
#include <stdio.h>
struct list
{
int data;
struct list *next;
}*head,*p;
void creat (struct list *head,int n)
{struct list *p,*s;
int i;
p=head;
printf("\nplease input the elem of linklist:\n");
for(i=1;i<=n;i++)
{s=(struct list *)malloc(sizeof(struct list));
scanf("%4d",&s->data);
p->next=s;p=s;s->next=NULL;}
}
void printout (struct list *head)
{struct list *p;
p=head->next;
printf("\nthe elem of linklist are:\n");
while(p)
{printf("%5d",p->data);
p=p->next;}
}
void insert (struct list *head,struct list *p,struct list *s)
{struct list *q;
if(p==head)
{
s->next=head;
head=s;
}
else
{
q=head;
while(q->next!=p) q=q->next;
q->next=s;
s->next=p;
}
}
void delete (struct list *head,int *e)
{
struct list *q,*p;
p=(struct list *)malloc(sizeof(struct list));
if(p==head)
head=p->next;
else
{
q=head;
while(q->next!=p)
q=q->next;
q->next=p->next;
}
e=p->data;
free(p);
}
struct list *locate(struct list *head,int e)
{
struct list*p;
p=head;
while(p&&p->data!=e)
p=p->next;
return p;
}
main()
{struct list *head,*p,*q,*s;
int n=10;
head=(struct list *)malloc(sizeof(struct list));
creat(head,n);
printout(head);
q=(struct list *)malloc(sizeof(struct list));
q->data=10;
q->next=NULL;
s=(struct list *)malloc(sizeof(struct list));
p=(struct list *)malloc(sizeof(struct list));
insert(head,p,s);
printout(head);
delete(head,5);
printout(head);
locate(head,3);
}
#include <stdio.h>
struct list
{
int data;
struct list *next;
}*head,*p;
void creat (struct list *head,int n)
{struct list *p,*s;
int i;
p=head;
printf("\nplease input the elem of linklist:\n");
for(i=1;i<=n;i++)
{s=(struct list *)malloc(sizeof(struct list));
scanf("%4d",&s->data);
p->next=s;p=s;s->next=NULL;}
}
void printout (struct list *head)
{struct list *p;
p=head->next;
printf("\nthe elem of linklist are:\n");
while(p)
{printf("%5d",p->data);
p=p->next;}
}
void insert (struct list *head,struct list *p,struct list *s)
{struct list *q;
if(p==head)
{
s->next=head;
head=s;
}
else
{
q=head;
while(q->next!=p) q=q->next;
q->next=s;
s->next=p;
}
}
void delete (struct list *head,int *e)
{
struct list *q,*p;
p=(struct list *)malloc(sizeof(struct list));
if(p==head)
head=p->next;
else
{
q=head;
while(q->next!=p)
q=q->next;
q->next=p->next;
}
e=p->data;
free(p);
}
struct list *locate(struct list *head,int e)
{
struct list*p;
p=head;
while(p&&p->data!=e)
p=p->next;
return p;
}
main()
{struct list *head,*p,*q,*s;
int n=10;
head=(struct list *)malloc(sizeof(struct list));
creat(head,n);
printout(head);
q=(struct list *)malloc(sizeof(struct list));
q->data=10;
q->next=NULL;
s=(struct list *)malloc(sizeof(struct list));
p=(struct list *)malloc(sizeof(struct list));
insert(head,p,s);
printout(head);
delete(head,5);
printout(head);
locate(head,3);
}