#include<stdio.h>
#include<stdlib.h>
#define LEN sizeof(struct node)
struct node
{
int num;
struct node*next;
};
int n=0;
struct node*()
{
struct node*head,*p1,*p2;
head=NULL ;
p1=p2=(struct node*)malloc(LEN);
printf("please put in data:n");
scanf_s("%d",&p1->num);
while(p1->num!=0)
{
n++;
if(n==1)head=p1;
else
p2->next=p1;
p2=p1;
p1=(struct node*)malloc(LEN);
printf("please put in data:n");
scanf_s("%d",&p1->num);
}
p2->next=NULL;
return head;
}
void print(struct node*head)
{
struct node*p=head;
while(p!=NULL)
{
printf("%dt",p->num);
p=p->next;
}
}
void clear(struct node*head)
{
struct node*p1,*p2;
p1=p2=head;
while(p1!=NULL)
{
p1=p2->next;
free(p2);
p2=p1;
}
}
int _tmain(int argc, _TCHAR* argv[])
{
struct node*p=NULL;
int i;
printf("tt1.创建链表n");
printf("tt2.输出链表n");
printf("tt3.清空链表n");
while(1)
{
printf("请输入:n");
scanf_s("%d",&i);
switch(i)
{
case 1:p=create();printf("创建成功n");break;
case 2:print(p);break;
case 3:clear(p);break;
default:return 0;
}
}
return 0;
}
近期评论