您的位置:首页 > 编程语言 > C语言/C++

关于单链表插入函数设计问题/c&c++/code/

2016-11-19 14:32 323 查看
#include<stdio.h>

#include<stdlib.h>

typedef struct node

{

 int data;

 node *next;

}node;

void add(node * &l)   //void add(node *l)这样无法改变head指向。

{

 node *temp = l;

 int i;

 for(i = 1; i<=10; i++)

 {

  temp = l;

  node *p = (node *)malloc(sizeof(node)); 

  p->next = NULL;

  p->data = i;

  if(l == NULL) l = p;

  else

  {

   l = p;

   p->next = temp;

  }

 }

}

int main()

{

 node *head = NULL;    //注意赋值NULL

 add(head);

 node *temp = head;

 for(; temp!= NULL; temp = temp->next)

 {

  printf("%d ",temp->data);

 }

 return 0;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐