zoukankan      html  css  js  c++  java
  • 链表笔记之1

    #include <stdlib.h>/*标准库函数*/
    #include <stdio.h>/*I/O函数*/
    #include <string.h>/*字符串函数*/
    #include <ctype.h>/*字符操作函数*/
    #include "linkedlist.h"
    #include "hashtable.h"
    #include "queue.h"


    typedef struct student
    {
        int id;
        char name[15];
    } student; //节点定义

    //链表的遍历
    void print_linked_list(LinkedList* list)
    {
        if(list->head == NULL)
        {
            printf("print_link函数执行,链表为空\n");
            return;
        }
        while(list->head!=NULL)
        {
            student* st=(student*)list->head->data;
            printf("%d %s\n",st->id,st->name);
            list->head=list->head->next;
        }
        printf("\n");
    }

    //链表的结点删除
    void delete_linked_list_node(LinkedList* list,int no)
    {
        if(list->head == NULL)
        {
            printf("print_link函数执行,链表为空\n");
            return;
        }

        LinkedListNode *node = NULL, *tmp = NULL;
        node=tmp=list->head;

        int id=((student*)node->data)->id;
        if(id==no)
        {
            list->head=node->next;
            free(node);
        }
        else
        {
            while((id!=no)&&(node->next!=NULL))
            {
                id=((student*)node->data)->id;
                tmp = node;
                node = node->next;
            }
            if(id==no)
            {
                tmp->next=node->next;
                free(node);
            }
        }
    }

    int main(int argc, char *argv[])
    {
        /*
        LinkedList *list = NULL;
        list=linked_list_construct();
        int i=0;
        while(i<5)
        {
         LinkedListNode *node=NULL;
         node=(LinkedListNode *)malloc(sizeof(LinkedListNode));
         memset(node,0,sizeof(LinkedListNode));

         student* st=NULL;
         st=(student *)malloc(sizeof(student));
         memset(st,0,sizeof(student));

         st->id=i;
         sprintf(st->name,"%s%d","方欣_",i);
         node->data=st;
         linked_list_append_node(list,node);
         i++;
        }

        //linked_list_remove_node(list,node);
        //linked_list_destroy(list);
        //int ret=linked_list_is_empty(list);
        //printf("%s\n",ret==1?"链表为空":"链表非空");
        delete_linked_list_node(list,2);
        print_linked_list(list);*/


        /*
         HashTable* ht=NULL;
            ht=hash_table_construct(5);
            int j=0;
         while(j<5)
         {
          student* st=NULL;
          st=(student *)malloc(sizeof(student));
          memset(st,0,sizeof(student));
          st->id=j;
          sprintf(st->name,"%s%d","方欣_",j);
          hash_table_add_element(ht,st,j);
          j++;
         }

         int k;
         for(k=0;k<5;++k)
         {
          student* st=NULL;
             st=(student*)hash_table_get_element(ht,k);
          printf("%d %s\n",st->id,st->name);
         }*/

        /*
         hash_table_remove_element(ht,2);
         student* stu=NULL;
            stu=(student*)hash_table_get_element(ht,3);
            if(stu!=NULL)
            {
             printf("%d %s\n",stu->id,stu->name);
            }*/


        //int index=hash_table_get_index(ht,4);
        //printf("%d\n",index);

        //char* code="E:\\fxd\\hd_mw\\src\\dg_ip_program";
    //    int index=hash_table_get_hash_code_from_string(code);
    //    printf("%d\n",index);

        //hash_table_destroy(ht);

        Queue * qu=queue_construct();//初始化队列
        int j=0;
        while(j<5)
        {
            student* st=NULL;
            st=(student *)malloc(sizeof(student));
            memset(st,0,sizeof(student));
            st->id=j;
            sprintf(st->name,"%s%d","方欣_",j);
            queue_enqueue(qu,st);//入队
            j++;
        }
        int ret;
        ret=queue_length(qu);
        printf("%d\n",ret);

        /*
         queue_destroy(qu);//销毁队列
         ret=queue_is_empty(qu);
         printf("%s\n",ret==1?"队列为空":"队列非空"); */
        /*
        int k;
        for(k=0;k<5;++k)
        {
         student* st=NULL;
         st=(student *)malloc(sizeof(student));
         memset(st,0,sizeof(student));
         st=(student *)queue_dequeue(qu);//出队
         printf("%d %s\n",st->id,st->name);
        }*/
        return 0;
    }

  • 相关阅读:
    [编译器] GCC编译过程 [ISO > ESc]
    《计算机网络 4》 应用层
    [编译器] cc、gcc、g++、CC的区别概括
    这个VS2010 技巧 折磨了我好久。留个贴纪念下。
    C#设置系统日期和时间的代码
    C# string格式的日期时间字符串转为DateTime类型
    中文普通图书著者号码的取号规定
    汉语著者号自动生成系统的设计与实现
    网络环境下提高图书编目工作效率搞高的方法
    jquery 超级select 插件 selectsearch v3.0.0.0插件 支持汉字、拼音、英文快速定位查询的超级select插件。可方向键、tab 键快速选择。
  • 原文地址:https://www.cnblogs.com/fx2008/p/2203419.html
Copyright © 2011-2022 走看看