黑马程序员技术交流社区

标题: C语言链表 模型 [打印本页]

作者: talent123    时间: 2015-6-3 13:26
标题: C语言链表 模型
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. typedef struct _node{
  4.         int value;
  5.         struct _node *next;
  6. }Node;

  7. typedef struct list{
  8.         Node *head;
  9.         Node *tail;
  10. }List;
  11. int main(){
  12.         int number;
  13.         List list;
  14.         list.head = list.tail = NULL;
  15.        
  16.         do{
  17.                 scanf("%d", &number);
  18.                 //接收一个Number
  19.                 if (number != -1){
  20.                         Node *p = (Node *)malloc(sizeof(Node));
  21.                         p->value = number;
  22.                         p->next = NULL;        
  23.                 //如果头存在
  24.                         if (list.tail){
  25.                                 list.tail->next = p;
  26.                                 list.tail = p;
  27.                         }else{
  28.                                 list.head = list.tail = p;
  29.                         }
  30.                 }
  31.         }while (number != -1);
  32.         Node *p = list.head;
  33.         do{
  34.                 printf("%d\t", p->value);
  35.                 p = p->next;
  36.         }while(p);
  37.         return 0;
  38. }
复制代码


很适合有顺序的一组数据搜索之类的功能,都可以套用
作者: 白昼那知夜的黑    时间: 2015-6-3 13:45
支持下!!!




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2