69,382
社区成员
发帖
与我相关
我的任务
分享
#include<stdio.h>
#include<stdlib.h>
struct node
{
int num;
struct node *next;
};
void pop(struct node *p)
{
struct node *p1 = p, *p2 = p;
while(p1 != NULL)
{
//printf("%d",p->num);
p2 = p1;
p1 = p1->next;
free( p2);
}
p1 = NULL;
p2 = NULL;
}
void main()
{
int n=0;
struct node ww;
struct node *p1,*p2,*head;
p1=(struct node *)malloc(sizeof(struct node ));
//p2=(struct node *)malloc(sizeof(struct node ));
head=NULL;
scanf("%d",&p1->num);
while(p1->num!=0)
{
n+=1;
if(n==1) head=p1;
else p2->next=p1;
p2=p1;
p1 = NULL;
p1=(struct node *)malloc(sizeof(struct node ));
scanf("%d",&p1->num);
}
p2->next=NULL;
p1=head;
while(p1!=NULL){
//head=push(p1->num);
printf("%d\n",p1->num);
p1=p1->next;
}
head = NULL;
pop(head);
}
[root@bogon temp]# ./temp
1
2
3
0
1
2
3
#include<stdio.h>
#include<stdlib.h>
struct node
{
int num;
struct node *next;
};
int w=0;
struct node *push(int i)
{
w+=1;
struct node *head,*end,*p1,*p2;
p1=(struct node *)malloc(sizeof(struct node ));
p2=(struct node *)malloc(sizeof(struct node ));
head=(struct node *)malloc(sizeof(struct node ));
end=(struct node *)malloc(sizeof(struct node ));//end 只是一个指针,赋值前申请空间
end->next=NULL;
p1->num=i;
if(w==1)p1=end;//申请多于空间
else p2=p1;//申请多于空间
head=p2;
return head;
}
void pop(struct node *p)
{
struct node *p1 = p, *p2 = p;
while(p1 != NULL)
{
//printf("%d",p->num);
p2 = p1;
p1 = p1->next;
free( p2);
}
p1 = NULL;
p2 = NULL;
}
void main()
{
int n=0;
struct node ww;
struct node *p1,*p2,*head;
p1=(struct node *)malloc(sizeof(struct node ));
//p2=(struct node *)malloc(sizeof(struct node ));
head=NULL;
scanf("%d",&p1->num);
while(p1->num!=0)
{
n+=1;
if(n==1) head=p1;
else p2->next=p1;
p2=p1;
p1 = NULL;
p1=(struct node *)malloc(sizeof(struct node ));
scanf("%d",&p1->num);
}
p2->next=NULL;
p1=head;
while(p1!=NULL){
//head=push(p1->num);
printf("%d\n",p1->num);
p1=p1->next;
}
head = NULL;
//pop(head);
}
//input 1 2 3 4 0
//output 1 2 3 4
// Segmentation fault (core dumped) ??????
//linxu gcc