美文网首页
list 数据结构与算法

list 数据结构与算法

作者: my_passion | 来源:发表于2021-06-07 23:56 被阅读0次

1 list

单链表-带头结点.png 单链表-带头结点.png 不带 头结点.png 不带 头结点.png 不带头结点 + 删除 重构 为 去 if-else.png 不带头结点 + 删除 重构 为 去 if-else.png
1   单链表

1.1 带头结点

// 单链表-带头结点
#include <stdio.h>
#include <stdlib.h> // malloc / free

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

void init_empty_list(node** p)
{   
    *p = (node*)malloc( sizeof(node) );
    // blank_node
    (*p)->next = NULL;
}

node* createNode(int _data)
{
    node* pnode = (node*)malloc( sizeof(node) );
    pnode->data = _data;
    pnode->next = NULL;

    return pnode;
}

void insert_head(node* head, node* p)
{
    p->next = head->next;
    head->next = p;
}

void delete_node(node* head, int _elem)
{
    // (1) find the specified elem
    // NULL <=> tail_off_one_node
    node* current = head->next;
    node* prev = head;
    while(current != NULL && current->data != _elem) 
    {
        prev = current;
        current = current->next;
    }
    // (2) delete_node if found
    if(current != NULL) // found
    {
        prev->next = current->next;
        free(current);
    }
}

void print_list(node* head)
{
    for(node* p = head->next; p != NULL; p = p->next)
        printf("%d ", p->data);
    printf("\n");
}

clear_list(node* head)
{
    node* prev = head;
    while(head != NULL)
    {   
        head = head->next;
        free(prev);
        prev = head;
    }
}

int main()
{
    node* head = NULL;
    
    //(1) modify head => &head is passed
    // => para: node**
    init_empty_list(&head);

    //(2) create a list with 3 nodes: 2<-1<-0
    node* pnode = NULL;
    for(int i = 0; i < 3; ++i)
    {
        pnode = createNode(i);  
        //(3) head insert
        insert_head(head, pnode);
    }
    print_list(head); // 2 1 0

    //(4)
    delete_node(head, 1);

    print_list(head); // 2 0
    //(5) 
    clear_list(head);       
}

1.2 不带 头结点

//  单链表-不带 头结点
#include <stdio.h>
#include <stdlib.h> // malloc / free

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

void init_empty_list(node** p)
{   
    *p = NULL;
}

node* createNode(int _data)
{
    node* pnode = (node*)malloc( sizeof(node) );
    pnode->data = _data;
    pnode->next = NULL;

    return pnode;
}

void insert_head(node** phead, node* p)
{
    if (*phead == NULL)
        *phead = p;
    else
    {
        p->next = *phead;
        *phead = p;
    }
}

void delete_node(node** phead, int _elem)
{
    node* current = *phead;
    node* prev = current;
    while(current != NULL && current->data != _elem) 
    {
        prev = current;
        current = current->next;
    }
    if(current != NULL) // found
    {
        if (current == *phead)
            *phead = current->next;
        else
            prev->next = current->next;
        free(current);
    }
}

void print_list(node* head)
{
    for(node* p = head; p != NULL; p = p->next)
        printf("%d ", p->data);
    printf("\n");
}

clear_list(node* head)
{
    node* prev = head;
    while(head != NULL)
    {   
        head = head->next;
        free(prev);
        prev = head;
    }
}

int main()
{
    node* head = NULL;
    
    //(1) modify head => &head is passed
    // => para: node**
    init_empty_list(&head);

    //(2) create a list with 3 nodes: 2<-1<-0
    node* pnode = NULL;
    for(int i = 0; i < 3; ++i)
    {
        pnode = createNode(i);  
        //(3) head insert
        insert_head(&head, pnode);
    }
    print_list(head); // 2 1 0

    //(4)
    delete_node(&head, 1);

    print_list(head); // 2 0
    //(5) 
    clear_list(head);       
}

1.3 不带头结点 + 删除 重构 为 去 if-else

// 1.3 delete_node 去 if-else
#include <stdio.h>
#include <stdlib.h> // malloc / free

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

void init_empty_list(node** p)
{   
    *p = NULL;
}

node* createNode(int _data)
{
    node* pnode = (node*)malloc( sizeof(node) );
    pnode->data = _data;
    pnode->next = NULL;

    return pnode;
}

void insert_head(node** phead, node* p)
{
    if (*phead == NULL)
        *phead = p;
    else
    {
        p->next = *phead;
        *phead = p;
    }
}

void delete_node(node** phead, int _elem)
{
    node** indirect;
    indirect = phead;
    
    while (*indirect != NULL && (*indirect)->data != _elem )
    {
        indirect = &(*indirect)->next;
    }

    if(*indirect != NULL) // found
    {
        node* tmp = *indirect;
        *indirect = (*indirect)->next;
        free(tmp);
    }
}

void print_list(node* head)
{
    for(node* p = head; p != NULL; p = p->next)
        printf("%d ", p->data);
    printf("\n");
}

clear_list(node* head)
{
    node* prev = head;
    while(head != NULL)
    {   
        head = head->next;
        free(prev);
        prev = head;
    }
}

int main()
{
    node* head = NULL;
    
    //(1) modify head => &head is passed => para: node**
    init_empty_list(&head);

    //(2) create a list with 3 nodes: 2<-1<-0
    node* pnode = NULL;
    for(int i = 0; i < 3; ++i)
    {
        pnode = createNode(i);  
        
        //(3) head insert
        insert_head(&head, pnode);
    }
    print_list(head); // 2 1 0

    //(4)
    delete_node(&head, 1);

    print_list(head); // 2 0
    //(5) 
    clear_list(head);       
}

相关文章

  • 算法与数据结构(1),List

    算法与数据结构(1),List 算法与数据结构(2),Map 算法与数据结构(3),并发结构 习惯了,深夜更新博客...

  • 算法与数据结构(3),并发结构

    算法与数据结构(1),List 算法与数据结构(2),Map 算法与数据结构(3),并发结构 本来已经合上电脑了,...

  • 算法与数据结构(2),Map

    算法与数据结构(1),List 算法与数据结构(2),Map 算法与数据结构(3),并发结构 睡了不到六个小时,被...

  • 代码时间优化

    数据结构,循环 改进算法,选择合适的数据结构 字典(dictionary) 与 列表(list) 用途:多数据成员...

  • 算法与数据结构-链表((linked-list)-Java实现单

    title: 算法与数据结构-链表((linked-list)-Java实现单向链表 date: 2019-02-...

  • 数据结构与算法 - 查找

    数据结构与算法系列文章数据结构与算法 - 时间复杂度数据结构与算法 - 线性表数据结构与算法 - 树形结构数据结构...

  • python面试算法

    python内置数据结构算法常考 常用的内置算法和数据结构 sorted排序函数 dict/list/set/tu...

  • 思维导图之数据结构+算法

    数据结构+算法 = 程序 数据结构比较 参考文章 数据结构与算法数据结构与算法(java)

  • 数据结构与算法 - 树形结构

    数据结构与算法系列文章数据结构与算法 - 时间复杂度数据结构与算法 - 线性表数据结构与算法 - 树形结构 目录 ...

  • 学习清单

    当前学习 provider redux websocket socket 算法 数据结构:List Map...

网友评论

      本文标题:list 数据结构与算法

      本文链接:https://www.haomeiwen.com/subject/useqeltx.html