链表的创建和遍历

include

include

include

typedef struct Node{
int data;
struct Node pNext;
}NODE,
PNODE;

PNODE create_list(void);
void traverse_list(PNODE pHead);

int main(void){
PNODE pHead=NULL;
pHead=create_list();
traverse_list(pHead);
return 0;
}
PNODE create_list(void){
int len;
int i;
int val;
PNODE pHead=(PNODE)malloc(sizeof(NODE));
if(pHead==NULL){
printf("分配失败!\n");
exit(-1);
}
PNODE pTail =pHead;
pTail->pNext=NULL;
printf("请输入您需要生成的链表节点的个数: len =");
scanf("%d",&len);
for(i=0;i printf("请输入第%d个节点的值: ",i+1);
scanf("%d",&val);
PNODE pNew=(PNODE)malloc(sizeof(NODE));
if(pNew==NULL){
printf("分配失败!\n");
exit(-1);
}
pNew->data=val;
pTail->pNext=pNew;
pNew->pNext=NULL;
pTail=pNew;
}
return pHead;
}
void traverse_list(PNODE pHead){
PNODE p=pHead->pNext;
while(NULL!=p){
printf("%d ",p->data);
p=p->pNext;
}
printf("\n");
return;
}

你可能感兴趣的:(链表的创建和遍历)