Chinaunix首页 | 论坛 | 博客
  • 博客访问: 4264875
  • 博文数量: 1148
  • 博客积分: 25453
  • 博客等级: 上将
  • 技术积分: 11949
  • 用 户 组: 普通用户
  • 注册时间: 2010-05-06 21:14
文章分类

全部博文(1148)

文章存档

2012年(15)

2011年(1078)

2010年(58)

分类: C/C++

2011-05-10 23:09:59



附件代码: link.rar  

初始化时,我们 只malloc 了 head 头结点,并且 用list

指向 头结点

list.h
  1. #ifndef _LIST_H
  2. #define _LIST_H

  3. /*
  4.     这里我们也可以 插入结构体数组
  5. 我们需要将 下面 定义为
  6. typedef struct student{

  7. }ELemType;

  8.     struct student{
  9.         char sno[5];
  10.         char name[21];
  11.         int age;
  12.         float score;
  13.     }s[3]={
  14.         {"s001","lin wu",12,90},
  15.         {"s002","xiao ming",13,80},
  16.         {"s003","wang wu",11,100}
  17.         };
  18. */
  19. typedef char ElemType;

  20. typedef struct _list //定义一个 链表
  21. {
  22.     //NODE *head; //头结点
  23.     ElemType data;
  24.     struct _list *next;
  25. }LIST;

  26. LIST *init_list();
  27. int get_length(LIST *l);
  28. int insert_elem(LIST *l,ElemType data,int i);
  29. int delete_elem(LIST *l,int i);
  30. int get_elem(LIST *l,int i,ElemType *e);
  31. int locate(LIST *l,ElemType *e);
  32. void display_list(LIST *l);
  33. #endif
list.c
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include "list.h"


  4. LIST *init_list()
  5. {
  6.     LIST *l=(LIST *)malloc(sizeof(LIST));
  7.     if(l==NULL)
  8.         exit(1);
  9.     //memset(l,0,sizeof(LIST));
  10.     l->next=NULL;            
  11.     return l;
  12. }
  13. int insert_elem(LIST *l,ElemType data,int i)
  14. {
  15.     int j=1;    
  16.     LIST *p=l; //定向到 head 0 节点
  17.     LIST *new=NULL;
  18.     
  19.     if(i<1 || i>get_length(l)+1)
  20.         return 1;

  21.     new=(LIST *)malloc(sizeof(LIST));
  22.     if(new==NULL)
  23.         printf("malloc new fail\n");
  24.     new->data=data;
  25.     new->next=NULL;

  26.     while(j<i) //定位到 i-1 个结点,l 指向 线性表的head头,它表示是第 0 个结点
  27.     {
  28.         p=p->next;
  29.         j++;
  30.     }

  31.     new->next=p->next;
  32.     p->next=new;
  33.     
  34.     return 0;
  35. }

  36. int delete_elem(LIST *l,int i)
  37. {
  38.     LIST *p=l; //指向 第0个 结点 head 结点
  39.     LIST *tmp=NULL;

  40.     int j=1;

  41.     if(i<1 || i> get_length(l)) //假如超出范围
  42.         return 1;

  43.     while(j<i) //定位 到 i-1 个结点
  44.     {
  45.         p=p->next;
  46.         j++;
  47.     }

  48.     tmp=p->next; //指向第 i 个结点
  49.     p->next=tmp->next; //指向 第 i 个结点后面的结点
  50.     free(tmp);
  51.     return 0;
  52. }

main.c
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include "list.h"

  4. int main()
  5. {
  6.     ElemType ch1='a';
  7.     ElemType ch2='b';
  8.     ElemType ch3='c';    
  9.     ElemType ch;
  10.     LIST *list;

  11.     list=init_list(); //初始化 head 头结点
  12.  
  13.     insert_elem(list,ch1,1); //插入 结点
  14.     insert_elem(list,ch2,2);
  15.     insert_elem(list,ch3,2);

  16.     display_list(list); //显示结点

  17.     printf("locate 'c' =%d\n",locate(list,&ch3)); //查找 c 字符 位置
  18.  
  19.     printf("delete 2th\n"); //删除 第 2 个结点
  20.     delete_elem(list,2);
  21.     

  22.     display_list(list); //显示 所有结点

  23.     return 0;
  24. }

  1. ywx@yuweixian:~/yu/data-struct/link$ ./main
  2. data=a
  3. data=c
  4. data=b

  5. locate 'c' =2
  6. length=3
  7. delete 2th
  8. data=a
  9. data=b
























阅读(630) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~