读书人

C 对单链表进展打印

发布时间: 2012-12-20 09:53:21 作者: rapoo

C 对单链表进行打印

#include <stdio.h>#include <stdlib.h>struct node{int data;struct node *next;};node * InitLink(){node *p,*head,*newNode;head = (node*)malloc(sizeof(node));p = head;int array[] = {122,133,313,122,11,12,22,85,52};int i = 0;while(i<sizeof(array)/sizeof(int)){newNode = (node*)malloc(sizeof(node));newNode->data = array[i]; p->next = newNode;p = p->next;p->next = NULL;i++;}head = head->next;return head;}void print(node *head){node *p = head;if(head!=NULL){while(p!=NULL)   {   printf("%d\n",p->data);//122,133,313,122,11,12,22,85,52p = p->next;   }   }};int main(){node *head = InitLink();print(head);return 0;}

读书人网 >编程

热点推荐