单链表的建立程序 没想到自己用的是最笨的尾插法 不要忘记了p从第一个节点开始而不是从头结点开始计数,因为头结点数据值为空

  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <conio.h>
  5. //using namespace std;
  6. typedef struct node 
  7. int data; 
  8.        node * next; 
  9. }*linklist; 
  10. void creat(linklist head,int a) 
  11.      linklist p; 
  12. for(p=head;p->next!=NULL; p=p->next) 
  13.      {} 
  14.       linklist newnode=(linklist)malloc(sizeof(node));//竟然包含在malloc中。
  15.       newnode->data=a; 
  16.       newnode->next=NULL; 
  17.       p->next=newnode; 
  18. int main() 
  19.     node* head; 
  20.     head=(linklist)malloc(sizeof(node)); 
  21.     head->next=NULL; 
  22.     creat(head,1); 
  23.     creat(head,2); 
  24.     creat(head,3); 
  25.     creat(head,4); 
  26.     creat(head,5); 
  27. for(linklist p=head->next;p->next!=NULL; p=p->next) 
  28.      { 
  29.                   printf("%d ",p->data); 
  30.      } 
  31.      getch(); 
  32. return 0;  
  33. }  
posted @ 2011-06-22 11:28  thinking and coding  阅读(662)  评论(0编辑  收藏  举报