#include
#include
struct link
{
int data;
struct link *next;
};
struct link *AppendNode(struct link *head);
void Display(struct link *head);
void Dele(struct link *head);
int main()
{
int i=0;
char a;
struct link *head=NULL;
printf("do you want to append a new node(y/n)?");
scanf(" %c",&a);
while(a=='y'||a=='Y')
{
head=AppendNode(head);
Display(head);
printf("do you want to append a new node(y/n)?");
scanf(" %c",&a);
i++;
}
printf("%d new node have been appended \n",i);
Dele(head);
return 0;
}
struct link *AppendNode(struct link *head)
{
struct link *p=NULL,*pr=head;
int data;
p=(struct link*)malloc(sizeof(struct link));
if(p==NULL)
{
printf("no enough memory");
exit(0);
}
if(head==NULL)
head=p;
else
while(pr->next!=NULL)
{
pr=pr->next;
}
pr->next=p;
printf("Input node data");
scanf("%d",&data);
p->data=data;
p->next=NULL;
return head;
}
void Display(struct link *head)
{
struct link *p=head;
int j=1;
while(p!=NULL)
{
printf("%5d%10d\n",j,p->data);
p=p->next;
j++;
}
}
void Dele(struct link *head)
{
struct link *p=head,*pr=NULL;
while(p!=NULL)
{
pr=p;
p=p->next;
free(pr);
}
}
谢谢大家了!