设为首页 加入收藏

TOP

链表的头文件及简单的应用(二)
2013-10-17 09:08:05 来源: 作者: 【 】 浏览:491
Tags:文件 简单 应用

 

  int temp;

  nodetype *cur, *next;

  if (num <= 0)

  error(2);

  cur = list->head;

  if (cur == NULL)

  {

  scanf("%d", &temp);

  cur = (nodetype *)malloc(sizeof(nodetype));

  cur->data = temp;

  num--;

  list->length = 1;

  list->head = cur;

  } else{

  while (cur->next != NULL)

  cur = cur->next;

  }

  while (num--)

  {

  scanf("%d", &temp);

  next = (nodetype *)malloc(sizeof(nodetype));

  next->data = temp;

  cur->next = next;

  cur = next;

  list->length++;

  }

  cur->next = NULL;

  return list;

  }

  listtype * list_insert(listtype *list, int location, int data)

  {

  nodetype *temp, *cur;

  cur = list->head;

  if (location > 0 && location <= list->length)

  {

  while(--location)

  cur = cur->next;

  node_insert(cur, data);

  }

  else if(location == 0)

  {

  temp = (nodetype *)malloc(sizeof(nodetype));

  temp->data = data;

  temp->next = list->head;

  list->head = temp;

  }

  else

  {

  error(3);

  }

  list->length++;

  return list;

  }

  nodetype * node_delete(nodetype *cur, nodetype *pre)

  {

  pre->next = cur->next;

  free(cur);

  return pre;

  }

  int list_delete(listtype *list, int data)

  {

  nodetype *cur, *pre;

  for (cur = list->head, pre = NULL;

  cur->data != data && cur != NULL;

  pre = cur, cur = cur->next)

  ;

  if (cur == NULL)

  {

  printf("Can't find the data!\n");

  return 0;

  }

  else if(cur != list->head)

  {

  node_delete(cur, pre);

  }

  else if(cur == list->head)

  {

  list->head = cur->next;

  free(cur);

  }

  list->length--;

  return data;

  }

  listtype * list_delete_node(listtype *list, nodetype *node)

  {

  nodetype *pre;

  if (node == list->head)

  {

  list->head = node->next;

  free(node);

  list->length--;

  }

        

首页 上一页 1 2 3 下一页 尾页 2/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇C++小知识之如何看工程类型 下一篇C++小知识之wsprintf使用

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: