【2015/11/15】 数据结构学习日志_Day16 链表 我的<LinkList.c>

/*************************************************************************
    > File Name: class.c
    > Author: khalil
    > Mail: hsgwpj@gmail.com
    > Created Time: Sun 15 Nov 2015 09:09:48 AM CST
************************************************************************/
//如何让rand()更随机?

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

#if 0

int main(int argc, char **argv)
{
    // 数组的缺点:
    //
    // 1.一旦数组定义,大小固定,无法修改数组大小
    // 2.数组插入和删除的效率太低,时间复杂度O(n)
    //
    // 数组的优点:
    //
    // 1.下标访问,速度快,时间复杂度时O(1)
    //
    //
    //
    // 链表:
    //
            typedef struct Node{
                //数据域
                int data;
                //指针域(链域)
                struct Node *next;
            }Node;
    //
    // 链表的优点:
    //
    //  1.资源允许的情况下,规模可以不断的增长
    //  2.删除添加很方便
    //
    // 链表的缺点:
    //
    //  1.遍历效率很低
    //
    int *p_int = NULL;
    p_int = (int *)malloc(100);

    Node node1 = {0};
    //申请一个链表节点
    Node *p_node = (Node *)malloc(sizeof(Node));
    node1.next = p_node;

    return 0;
}
#endif

#if 0
typedef int datatype;
typedef struct Node{
    datatype data;
    struct Node *next;
}Node;

void add_node(Node* head, datatype data);

void add_node(Node* head, datatype data)
{
    Node *tmp = head->next;
    Node *new = (Node *)malloc(sizeof(Node));

    head->next = new;
    new->next  = tmp;
}

void create_node()
{
    printf("Welcome to Create node!\n");
}


#endif

#if 1

#include"list.c"
//我所编写的文件所提供的接口:
//    List_head   init_list(void)                                             ;//初始化
//    void        destory_list(List_head *head)                               ;//销毁
//    boolean     push_front(List_head ,int value)                            ;//头插
//    boolean     push_back(List_head, int value)                             ;//尾插(效率低)
//    boolean     pop_front(List_head)                                        ;//头删
//    boolean     pop_back(List_head)                                         ;//尾删
//    boolean     insert_node(List_head head, List_node *node, int value)      ;//插入节点
//    void        show_list(List_head head)                                   ;//显示信息
//    boolean     find_node(List_head head, int value, List_node **node)      ;//查找
//    void        sort_list_ascend(List_head head)                            ;//升序排列
//    void        sort_list_descend(List_head head)                           ;//降序排列
//    int         get_list_len(List_head head)                                ;//得到长度
//    void        modift_node(List_node *node, int value)                     ;//节点的修改

    #if 1

    // My version with UI

    int main (int argc, char *argv)
    {
        int run = 1;
        int o = 0;
        List_node *head = init_list();
        while(run){
            printf("\n\n----Khalil----\n");
            printf("|  Welcome to List node UI:\n");
            printf("|  1.Push_Back\n");
            printf("|  2.Push_Front\n");
            printf("|  3.Pop_Front\n");
            printf("|  4.Pop_Back\n");
            printf("|  5.Sort_Ascend\n");
            printf("|  6.Sort_Descend\n");
            printf("|  7.Insert\n");
            printf("|  8.Find\n");
            printf("|  9.Show\n");
            printf("|  0.Quit\n");
            printf("----Khalil----\n");
            printf("Please input ur choice:");

            scanf("%d",&o);
            int num = 0;
            int tmp = 0;
            switch(o){
                case 0: run = 0;break;
                case 1: printf("Input a num:");
                        scanf("%d",&num);
                        push_back(head, num);
                        break;
                case 2: printf("Input a num:");
                        scanf("%d",&num);
                        push_front(head, num);
                        break;
                case 3: pop_front(head);break;
                case 4: pop_back(head);break;
                case 5: sort_list_ascend(head);break;
                case 6: sort_list_descend(head);break;
                case 7: printf("Input where do you want to insert and what to insert:\n");
                        scanf("%d%d", &tmp, &num);
                        insert_node(head, tmp, num);
                        break;
                case 8: printf("What do you want to find?\n");
                        scanf("%d", &num);
                        printf("Result is (1 for found):%d",find_node(head, num, NULL));
                        break;
                case 9: show_list(head);break;
            }
        }
        printf("Thanks for using!\n");
        //防止内存泄露!
        destory_list(&head);
        return 0;
    }
    #endif


    #if 0
    //Tcr's Test Ver
    int main(int argc, char **argv)
    {
        List_node *tmp = NULL;
        List_node *head = init_list();

        int i = 0;
        for(i = 0; i < 10; ++i){
            push_front(head, rand() % 100);
        }
        show_list(head);
        printf("len=%d\n",get_list_len(head));
        sort_list_ascend(head);
        printf("\nAfter ascend Sort:\n");
        show_list(head);

        printf("Find 83 ( 1 for success ):%d\n",find_node(head, 83, &tmp));
        printf("What you find is :%d\n", tmp->data);

        printf("Insert result:%d\n",insert_node(head, 11, 3));
        printf("After insert 3 to 11:\n");
        show_list(head);

        sort_list_descend(head);
        printf("\nAfter descend Sort:\n");
        show_list(head);
        sort_list_ascend(head);
        printf("\nAfter ascend Sort:\n");
        show_list(head);


        //防止内存泄露!
        destory_list(&head);
        return 0;
    }
    #endif
#endif

/*************************************************************************
    > File Name: list.c
    > Author: khalil
    > Mail: hsgwpj@gmail.com
    > Created Time: Sun 15 Nov 2015 09:56:45 AM CST
 ************************************************************************/
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

#define true  (1)
#define false (0)

typedef char bool;

typedef struct List_node{
    int                 data;
    struct List_node    *next;
}List_node;

// 常见操作:
//  (接口)
//     1.初始化
//     2.销毁
//     3.增加
//     4.删除
//     5.查找
//     6.修改
//     7.排序
//     8.显示

typedef List_node* List_head;

List_head   init_list(void)                                             ;//初始化
void        destory_list(List_head *head)                               ;//销毁
bool     push_front(List_head ,int value)                            ;//头插
bool     push_back(List_head, int value)                             ;//尾插(效率低)
bool     pop_front(List_head)                                        ;//头删
bool     pop_back(List_head)                                         ;//尾删
bool     insert_node(List_head head, int index, int value)      ;//插入节点
void        show_list(List_head head)                                   ;//显示信息
bool     find_node(List_head head, int value, List_node **node)      ;//查找
void        sort_list_ascend(List_head head)                            ;//升序排列
void        sort_list_descend(List_head head)                           ;//降序排列
int         get_list_len(List_head head)                                ;//得到长度
void        modift_node(List_node *node, int value)                     ;//节点的修改

//包裹函数
static void         *Malloc(size_t size);
static List_node    *create_node(void);

static void         *Malloc(size_t size)
{
    void *result = malloc(size);
    if (result == NULL){    //失败情况的处理
        fprintf(stderr, "Memory Full!\n");
        exit(1);
    }
    return result;
}

static List_node    *create_node(void)
{
    List_node *node = (List_node *)Malloc(sizeof(List_node));
    bzero(node, sizeof(List_node));
    return node;
}

///  接口实现  /

List_head   init_list(void)
{
    List_head head = NULL;
    head = create_node();
    return head;
}

void        destory_list(List_head *head)
{
    List_node *p = NULL;
    List_node *q = NULL;
    if (head == NULL || *head == NULL){
        return ;
    }

    p = *head;
    while(p != NULL){
        q = p;
        p = p->next;
        free(q);
    }

    free(p);
}

bool     push_front(List_head head,int value)
{
    if(head == NULL){ //判断链表是否存在
        return false;
    }
    //生成节点并且赋值
    List_node *node;
    node = create_node();
    node->data = value;

    node->next = head->next;
    head->next = node;
    head->data++;
    return true;
}

bool     push_back(List_head head, int value)
{
    if(head == NULL){ //判断链表是否存在
        return false;
    }
    //生成节点并且赋值
    List_node *tmp = head;

    while(tmp->next != NULL){
        tmp = tmp -> next;
    }
    tmp->next = create_node();
    tmp->next->data = value;
    head->data++;
    return true;
}

bool     pop_front(List_head head)
{
    if(head == NULL ||  head->next == NULL){
        return false;
    }

    List_node *tmp = head->next;
    head->next = tmp->next;
    free(tmp);
    head->data--;
    return true;
}

bool     pop_back(List_head head)
{
    if(head == NULL ||  head->next == NULL){
        return false;
    }

    List_node *tmp = head;
    while(tmp->next->next != NULL ){
        tmp = tmp->next;
    }
    free(tmp->next);
    tmp->next = NULL;
    head->data--;
    return true;
}

bool     insert_node(List_head head, int index, int value)
{
    if(head == NULL || index <= 0 || index > head->data + 1){
        return false;
    }

    //构建新的节点
    List_node *tmp = create_node();
    tmp->data = value;

    List_node *p_node = head;
    List_node *q      = NULL;
    while(index--){
        q = p_node;
        p_node = p_node->next;
    }

    q->next = tmp;
    tmp->next = p_node;

    head->data++;
    return true;
}

void        show_list(List_head head)
{
    if(head == NULL ){
        fprintf(stderr,"The head node is NULL!\n");
        return;
    }

    List_node *p_node = head;
    int i = 1;
//    do{
//        printf("No.%d data:\t%d\n", i, p_node->data);
//        p_node = p_node->next;
//        i++;
//    }while( p_node->next != NULL )

    for(p_node = head->next; p_node != NULL; p_node = p_node->next, ++i){
        printf("No.%d:\t%d\n", i, p_node->data);
    }
}

bool     find_node(List_head head, int value, List_node **node)
{
    List_node *p_node = NULL;
    if(head == NULL ){
        fprintf(stderr, "The head node is NULL!\n");
        return false;
    }

    p_node = head->next;
    while(p_node != NULL){
        if(p_node->data == value){
            if(node != NULL){
                *node = p_node;
            }
            return true;
        }
        p_node = p_node->next;
    }

    return false;
}

void        sort_list_ascend(List_head head)
{
//      冒泡排序法
//    if (head == NULL){
//        return ;
//    }
//    int len = get_list_len(head);
//    int i = 0;
//    int j = 0;
//    List_node *p_node = NULL;
//    int tmp = 0;
//    for(i = 0; i < len - 1 ; ++i){
//        for(j = 0, p_node = head->next; j < len - 1; ++j, p_node = p_node->next){
//            if (p_node->data > p_node->next->data){
//                tmp = p_node->data;
//                p_node->data = p_node->next->data;
//                p_node->next->data = tmp;
//            }
//        }
//    }

//      选择排序法:(老师的版本)
    if (head == NULL){
        return ;
    }
    int len = get_list_len(head);
    List_node *p = NULL;
    List_node *q = NULL;
    int tmp = 0;
    for(p = head->next; p->next != NULL; p = p->next){
        for(q = p->next; q ; q = q->next){
            if (p->data > q->data){
                tmp = q->data;
                q->data = p->data;
                p->data = tmp;
            }
        }
    }
}

void        sort_list_descend(List_head head)
{
    if (head == NULL){
        return ;
    }
    int len = get_list_len(head);
    List_node *p = NULL;
    List_node *q = NULL;
    int tmp = 0;
    for(p = head->next; p->next != NULL; p = p->next){
        for(q = p->next; q ; q = q->next){
            if (p->data < q->data){
                tmp = q->data;
                q->data = p->data;
                p->data = tmp;
            }
        }
    }
}

int         get_list_len(List_head head)
{
    if(head == NULL){
        fprintf(stderr, "The head node is NULL!\n");
        return -1;
    }
    //List_node *p_node = head;
    //int len = 0;
    //while(p_node->next != NULL){
    //    len ++;
    //    p_node = p_node->next;
    //}
    //return len;

    //My ver!:
    return head->data;
}

void        modift_node(List_node *node, int value)     //节点的修改
{
//无意义 可以直接用find_list得到的node来修改!

}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值