#include <stdio.h>
#include <malloc.h>
struct Node
{
int data;
struct Node *next;
};
//带头节点的 头插法
struct Node * init_link(struct Node * s)
{
struct Node * head=NULL;
for(int x=10;x<20;x++)
{
struct Node * node1=NULL;
node1=(struct Node*)malloc(sizeof(struct Node));/////////////////////////////////////////////是结构体的大小,不是结构体指针的大小
node1->data=x;
node1->next=head;
head=node1;
}
return head;
}
//带头节点的 头插法
struct Node * init_link2(struct Node * s)
{
struct Node * head=NULL;
for(int x=100;x<110;x++)
{
struct Node * node1=NULL;
node1=(struct Node*)malloc(sizeof(struct Node));////////////////////////////////////
node1->data=x;
node1->next=head;
head=node1;
}
return head;
}
int main()
{
struct Node * head=NULL;
head=(struct Node*)malloc(sizeof(struct Node));///////////////////////////
head->data=0;
head->next=NULL;
//输出链表一
struct Node * pmove=init_link(head);
while (pmove)
{
printf("%4d ",pmove->data);
if(pmove->next==NULL) break;
pmove=pmove->next;
}
printf("\n");
//输出链表二
struct Node * head2=NULL;
head2=(struct Node*)malloc(sizeof(struct Node));///////////////////////////////
head2->data=0;
head2->next=NULL;
struct Node * pmove2=init_link2(head2);
while (pmove2)
{
printf("%4d ",pmove2->data);
if(pmove2->next==NULL) break;
pmove2=pmove2->next;
}
printf("\n");
return 0;
}
|