链表的建立

来源:互联网 发布:flash编程视频教程 编辑:程序博客网 时间:2024/06/01 09:01
#include <stdio.h>#include <malloc.h>#include <string.h>typedef struct node{int data;struct node *next;}Node;void creatt(Node *head, int n){int k=0;for(int i=0;i<n;i++){Node *b=(Node *)malloc(sizeof(Node));b->next=head->next;head->next=b;//scanf("%d",&k);b->data=k++;}}void printt(Node *head){ Node *p; p=head->next; while(p!=NULL) {  printf("%d ",p->data);  p=p->next; }}int main(){Node * head=(Node *)malloc(sizeof(Node));head->next=NULL;creatt(head, 5);printt(head);}//注意:当时写这个程序的时候遇到了一个运行时错误,后来才发现是自己平时学语言太粗心了。注意事项在C\C++中的空指针错误中。
0 0
原创粉丝点击