|
发表于 2013-11-30 15:04:27
|
显示全部楼层
#include<stdio.h>
#include<malloc.h>
#define NULL 0
#define LEN sizeof(List)
typedef struct node
{
int data;
struct node * next;
}List;
int n=0;
List *create(void)
{
List * head;
List * p1,* p2;
p1=(List*)malloc(LEN);
p2=(List*)malloc(LEN);
head=NULL;
puts("请输入要插入链表的数据(输入0结束):\n");
scanf("%d",&p1->data);
while(p1->data!=0)
{
n++;
if(n==1)
head=p1;
else
p2->next=p1;
p2=p1;
p1=(List*)malloc(LEN);
scanf("%d",&p1->data);
}
free(p1);
p2->next=NULL;
return head;
}
void Print(List * head)
{
List * p;
p=head;
if(head!=NULL)
do
{
printf("[%d]->",p->data);
p=p->next;
}while(p!=NULL);
}
int main()
{
int m;
List * head;
List * pa=(List*)malloc(LEN), *pb=(List*)malloc(LEN);
List * p1,*p2;
head=create();
Print(head);
printf("\n");
p1=head;
pa=p1;
p2=p1->next;
pb=p2;
while(p2->next!=NULL)
{
p1->next=p1->next->next;
p1=p2->next;
if(p1->next==NULL)
break;
p2->next=p2->next->next;
p2=p1->next;
}
p1->next=NULL;
p2->next=NULL;
Print(pa);
printf("NULL\n");
Print(pb);
printf("NULL\n");
//return 0;
scanf("%d", &m);
} |
|