Chinaunix首页 | 论坛 | 博客
  • 博客访问: 838659
  • 博文数量: 90
  • 博客积分: 766
  • 博客等级: 军士长
  • 技术积分: 1867
  • 用 户 组: 普通用户
  • 注册时间: 2011-12-18 08:42
个人简介

linux

文章分类

全部博文(90)

文章存档

2021年(2)

2020年(2)

2017年(1)

2015年(11)

2014年(1)

2013年(53)

2012年(16)

2011年(4)

分类: C/C++

2012-11-09 15:42:45

        最近一个人在做一个项目,对模块化有了新的感觉,另外再次感觉到基础的重要性,所以在今后长期一段时间内就会将基础知识作为学习的重心,所以刚把项目功能实现将链表实现一下,希望亡羊补牢,为时不晚。我觉得所有的事情都是因果循环,有因才有果,必须现在学习链表,就应该了解我们为什么要使用链表,也可以说使用链表有什么好处,只有了解了事物的本质,我们才能更好的使用它。这算是我给在校生了一个建议。

         在我看来,单链表是链接相同事物的一种机制,即链接的事物必须具有相同的属性,为什么链接他们是因为把他们组合起来成为一个集合,便于分类管理,这是使用它的原因,另外有正比有反,创建链表集群管理是好处,那么它就肯定有副作用,它的副作用是什么?负作用就是我们需要使用其中的某一事物时,不能立即得到它,而必须先在链表中来找它。,需要时间。

         然后我读了百度百科上的介绍,关键字:空间占用大,时间复杂度高,数组,不需要预先知道事物属性需要的空间大小。
         分析有两个缺点:1.没有和所有的数据结构联系起来思考。2.在集群的时候需要链接指针我却没有考虑到,这就是我目前思维的缺陷,希望以后能改正。

先考虑单链表应该有些什么样的操作:
        1.创建链表,2.销毁链表,3.插入元素,4.删除元素,5.遍历链表,6.链表逆置借助外界,7.链表逆置不借助外界
        8.显示所有链表
其次考虑各种操作的接口参数:
链表标识:字符串
1.创建链表
        目的:创建一个链表,这意味着我们可以创建多个链表,所以应该有不同的标识,同时我们需要为链表创建多少一个元素(此单链表数据类型必须一致)。
int creat_linklist(const char *,unsigned int)

2.销毁链表
        目的:删除一个链表。
int del_linklist(const char *,int)

3.插入元素
        目的:在某个链表中插入某个元素,每个元素的小属性不同(这是链表的重要特性:不需要预先知道事物的属性大小,必须实现)这里存在一个问题,每个元素的属性不同,我们怎么知道在使用它的时候,它是个什么东西,需要知道它是什么,我们必须要有另外的标识,这样不好,我们这里只实现不同长度的字符串(基本属性相同)。
int insert_linklist(const char *, int, const char*, int)

4.删除元素
        目的:删除某个链表中的某个元素。
int del_linkelement(const char*,int)

5.遍历链表
        目的:通过遍历来查找具有某一个属性的事物。
int traverse_linklist(const char*,const char*)

6.链表逆置
        目的:将某一个链表中的所有元素全部反转
int inverse(const char*)

下面是具体实现:

点击(此处)折叠或打开

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <malloc.h>
  4. #include <string.h>

  5. #define NAME_LENGTH 20

  6. typedef struct node {
  7.     int value;
  8.     char name[NAME_LENGTH];
  9.     struct node *next;
  10. }node;

  11. node *link;
  12. char linklist_name[NAME_LENGTH];

  13. node* create_linklist(void);
  14. int print_linklist(node*);
  15. int insert_linklist(node*);
  16. int _insert_linklist(node*,node*);
  17. int del_linklist(node*);
  18. int inverse_linklist(node*);
  19. int get_input(void);
  20. int set_elementvalue(node*,const char*,int);
  21. /*
  22.  *get the input
  23.  */
  24. int get_input(void)
  25. {
  26.     int value,char_get;

  27.     while (1) {
  28.         char_get = scanf("%d",&value);
  29.         
  30.         if (0 == char_get) {
  31.             while (((char_get=getchar())!='\n') && (char_get!=EOF));
  32.             printf("wrong input,input again!");
  33.         } else {
  34.             break;
  35.         }
  36.     }

  37.     /*
  38.      *flush stdin
  39.      */
  40.     while (((char_get=getchar())!='\n') && (char_get!=EOF));
  41.     return value;
  42. }

  43. /*
  44.  *set the node value
  45.  */
  46. int set_elementvalue(node *current_node,const char *linklist_name,int element_value)
  47. {
  48.     if (NULL == current_node)
  49.     {
  50.         //...
  51.         return 1;
  52.     }
  53.     if (NULL == linklist_name)
  54.     {
  55.         //...
  56.         return 1;
  57.     }
  58.     
  59.     /*set*/
  60.     strcpy(current_node->name,linklist_name);
  61.     current_node->value = element_value;
  62.     
  63.     return 0;
  64. }

  65. /*
  66.  *insert
  67.  */
  68. int _insert_linklist(node *ahead_node,node *current_node)
  69. {
  70.     if (NULL==ahead_node || NULL==current_node)
  71.         return 1;

  72.     current_node->next = ahead_node->next;
  73.     ahead_node->next = current_node;
  74.     return 0;
  75. }

  76. /*
  77.  *create
  78.  */
  79. node* create_linklist(void)
  80. {
  81.     int i,elements_count,element_value;
  82.     node *first_node,*current_node,*ahead_node;

  83.     elements_count = -1;
  84.     first_node = current_node = NULL;

  85.     /*get value*/
  86.     printf("count:");
  87.     elements_count = get_input();
  88.     printf("linklist name:");
  89.     fgets(linklist_name,NAME_LENGTH,stdin);

  90.     for (i=0; i<elements_count; i++)
  91.     {
  92.         current_node = (node*)malloc(sizeof(node));
  93.         if (NULL == current_node)
  94.         {
  95.             //...
  96.             return NULL;
  97.         }

  98.         printf("value:");
  99.         element_value = get_input();

  100.         /*set value*/
  101.         set_elementvalue(current_node,linklist_name,element_value);

  102.         if (0 == i)
  103.         {
  104.             //...
  105.             current_node->next = NULL;
  106.             first_node = current_node;
  107.             ahead_node = first_node;
  108.         }
  109.         else
  110.         {
  111.             /*insert*/
  112.             _insert_linklist(ahead_node,current_node);
  113.             ahead_node = ahead_node->next;
  114.         }

  115.         
  116.     }

  117.     return first_node;
  118. }

  119. /*
  120.  *print
  121.  */
  122. int print_linklist(node *current_node)
  123. {
  124.     int i = 1;
  125.     
  126.     if (NULL == current_node)
  127.     {
  128.         //...
  129.         return 1;
  130.     }
  131.     while (current_node != NULL)
  132.     {
  133.         printf("the %d element ",i);
  134.         printf("name:%s " ,current_node->name);
  135.         printf("value:%d\n" ,current_node->value);
  136.         
  137.         i++;
  138.         current_node = current_node->next;
  139.     }

  140.     return 0;
  141. }

  142. /*
  143.  *insert function
  144.  */

  145. int insert_linklist(node *first_node)
  146. {
  147.     node *ahead_node,*current_node;
  148.     int place,i,element_value;

  149.     ahead_node = current_node = NULL;
  150.     place = -1;
  151.     element_value = 0xffffffff;
  152.     
  153.     printf("which place you want to insert:");
  154.     place = get_input();

  155.     if (place <=0)
  156.     {
  157.         //...
  158.         return 1;
  159.     }

  160.     printf("value:");
  161.     element_value = get_input();

  162.     current_node = (node*)malloc(sizeof(node));
  163.     if (NULL == current_node)
  164.     {
  165.         //...
  166.         return 1;
  167.     }

  168.     set_elementvalue(current_node,linklist_name,element_value);

  169.     /*first place*/
  170.     if (1 == place)
  171.     {
  172.         current_node->next = first_node;
  173.         first_node = current_node;
  174.         /*watch it*/
  175.         link = first_node;
  176.         return 0;
  177.     }

  178.     /*other place*/
  179.     ahead_node = first_node;
  180.     for (i=0; (i<place-2) && (ahead_node->next!=NULL); i++)
  181.     {
  182.         ahead_node = ahead_node->next;
  183.     }

  184.     _insert_linklist(ahead_node,current_node);

  185.     return 0;
  186. }

  187. /*
  188.  *delete element
  189.  */
  190. int del_linklist(node *first_node)
  191. {
  192.     int place,i;
  193.     node *del_node,*ahead_node;

  194.     del_node = ahead_node = NULL;
  195.     place = -1;

  196.     printf("which element you want to delete:");
  197.     place = get_input();

  198.     if (place <= 0)
  199.     {
  200.         //...
  201.         return 1;
  202.     }

  203.     if (1 == place)
  204.     {
  205.         del_node = first_node;
  206.         /*watch it*/
  207.         first_node = first_node->next;
  208.         link = first_node;
  209.         free(del_node);
  210.         return 0;
  211.     }

  212.     ahead_node = first_node;
  213.     /*find the ahead node*/
  214.     for (i=0; (i<place-2) && (ahead_node->next!=NULL); i++)
  215.     {
  216.             ahead_node = ahead_node->next;
  217.     }

  218.     if (NULL == ahead_node->next)
  219.     {
  220.         //...
  221.         return 1;
  222.     }
  223.     /*delete it*/
  224.     del_node = ahead_node->next;
  225.     ahead_node->next = del_node->next;
  226.     free(del_node);

  227.     return 0;
  228. }


  229. /*
  230.  *inverse
  231.  */
  232. int inverse_linklist(node *first_node)
  233. {
  234.     node *ahead_node,*current_node,*next_node;

  235.     ahead_node = current_node = next_node = NULL;

  236.     ahead_node = first_node;
  237.     if (NULL == ahead_node->next)
  238.         return 1;
  239.     
  240.     current_node = ahead_node->next;
  241.     ahead_node->next = NULL;
  242.     while ((next_node=current_node->next) != NULL)
  243.     {
  244.         /*inverse*/
  245.         current_node->next = ahead_node;
  246.         
  247.         /*move next*/
  248.         ahead_node = current_node;
  249.         current_node = next_node;
  250.     }
  251.     
  252.     /*the last two node*/
  253.     current_node->next = ahead_node;
  254.     link = current_node;

  255.     return 0;
  256. }
  257. /*
  258.  *
  259.  */
  260. int main(void)
  261. {
  262.     link = create_linklist();
  263.     print_linklist(link);

  264.     insert_linklist(link);
  265.     print_linklist(link);

  266.     del_linklist(link);
  267.     print_linklist(link);

  268.     inverse_linklist(link);
  269.     print_linklist(link);

  270.     return 0;
  271. }

    尽管写之前就进行了接口设计,编写过程中仍然有许多变动,另外首先应该进行系统设计,然后是数据结构分析,最后才是具体的模块设计。
        


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