Chinaunix首页 | 论坛 | 博客
  • 博客访问: 379036
  • 博文数量: 165
  • 博客积分: 436
  • 博客等级: 下士
  • 技术积分: 887
  • 用 户 组: 普通用户
  • 注册时间: 2011-11-10 02:49
文章分类

全部博文(165)

文章存档

2012年(95)

2011年(70)

分类:

2011-12-01 00:22:03

原文地址:链表的c语言实现 作者:风吹过夏天

 

  1. typedef struct node
  2. {
  3.     int element;
  4.     node *next;
  5. }node;

  6. //创建空表头
  7. node *createhead()
  8. {
  9.     node *head;
  10.     head=(node *)malloc(sizeof(node));
  11.     head->next=NULL;
  12.     return head;
  13. }

  14. //链表尾
  15. node *getend(node *head)
  16. {
  17.     if (head->next==NULL)    //空链表
  18.     {
  19.         return head;
  20.     }
  21.     node *p=head;
  22.     while (p->next!=NULL)
  23.     {
  24.         p=p->next;
  25.     }
  26.     return p;
  27. }

  28. //在链表尾插入
  29. int inserttail(node *head,int element)
  30. {
  31.     if (head==NULL) //非法表头
  32.     {
  33.         return 0;
  34.     }
  35.     node *p1,*p2;
  36.     p2=getend(head);
  37.     p1=(node *)malloc(sizeof(node));
  38.     if (p1==NULL)
  39.     {
  40.         printf("no space for node\n");
  41.         return 0;
  42.     }
  43.     p1->element=element;
  44.     p1->next=NULL;
  45.     p2->next=p1;
  46.     return 1;
  47. }

  48. //在链表头插入
  49. int inserthead(node *head, int element)
  50. {
  51.     if (head==NULL)
  52.     {
  53.         return 0;
  54.     }
  55.     node *p;
  56.     p=(node *)malloc(sizeof(node));
  57.     if (p==NULL)
  58.     {
  59.         printf("no space for node\n");
  60.         return 0;
  61.     }
  62.     p->element=element;
  63.     p->next=head->next;
  64.     head->next=p;
  65.     return 1;
  66. }
  67. //在指定位置插入
  68. int insert(node *head,int n,int pos)
  69. {
  70.     if (head==NULL||pos<1)
  71.     {
  72.         return 0;
  73.     }
  74.     node *p=head;
  75.     int count=1;
  76.     while (p->next!=NULL&&count!=pos)
  77.     {
  78.         p=p->next;
  79.         count++;
  80.     }
  81.     if (count!=pos) //没有那么多节点,所以···找不到相应的位置,插入表尾吧
  82.     {
  83.         inserttail(head,n);
  84.         return 0;
  85.     }
  86.     node *pin=(node *)malloc(sizeof(node));
  87.     pin->element=n;
  88.     pin->next=p->next;
  89.     p->next=pin;
  90.     return 0;
  91. }
  92. //遍历链表
  93. int printlisk(node *head)
  94. {
  95.     if (head->next==NULL)
  96.     {
  97.         printf("empty list\n");
  98.         return 0;
  99.     }
  100.     node *p=head;
  101.     while (p->next!=NULL)
  102.     {    
  103.         p=p->next;
  104.         printf("%d \n",p->element);
  105.     }
  106.     return 0;
  107. }

  108. //链表翻转
  109. node* reverselist(node *head)
  110. {
  111.     if (head->next==NULL) //空链表
  112.     {
  113.         return head;
  114.     }
  115.     node *p1,*p2,*tmp;
  116.     p2=head->next; //指向第一个数据节点
  117.     if (p2->next==NULL) //一个数据节点,不必翻转
  118.     {
  119.         return head;
  120.     }

  121.     p1=NULL; //新链表尾
  122.     while (p2->next!=NULL)
  123.     {
  124.         tmp=p2->next;
  125.         p2->next=p1;
  126.         p1=p2;
  127.         p2=tmp;
  128.     }
  129.     p2->next=p1;
  130.     head->next=p2; //重新定位表头
  131.     return head;
  132. }
  133. //判空
  134. bool isempty(node *head)
  135. {
  136.     if (head->next==NULL)
  137.     {
  138.         return true;
  139.     }
  140.     else
  141.         return false;
  142. }
  143. //清空链表
  144. int emptylist(node *head)
  145. {
  146.     if (head->next==NULL)
  147.     {
  148.         return 0;
  149.     }
  150.     node *p1,*p2;
  151.     p1=head->next;    
  152.     while (p1!=NULL)
  153.     {
  154.         p2=p1;
  155.         p1=p1->next;
  156.         free(p2);
  157.     }
  158.     head->next=NULL;
  159.     return 0;
  160. }

  161. //查找
  162. node* find(node *head,int n)
  163. {
  164.     if (isempty(head))
  165.     {
  166.         return NULL;
  167.     }
  168.     node *p=head->next;
  169.     while(p!=NULL&&p->element!=n) //如果写成p->element!=n&&p!=NULL,当p==NULL时候p->element就会访问出错了
  170.     {
  171.         p=p->next;
  172.     }
  173.     return p;
  174. }
阅读(637) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~