Linked List {singly linked list -> doubly linked list -> circular linked list}

Linked List

单向链表 Singly linked list

/*********************************************************
Code writer : EOF
Code file   : single_linked_list.c
Code date   : 2015.01.15
e-mail      : [email protected]

Code description:
        Here is a implementation for singly linked list.

    If there is something wrong with my code please touch me
by e-mail, thank you.
**********************************************************/
#include <stdio.h>
#include <stdlib.h>

#define ARRAY_SIZE 10

// Abstraction for our node which is in list.
struct node
{
    int num;
    struct node *next;
};

struct node *list_search(struct node* p_head, int k)
{
    if(!p_head)
    {
         printf("Empty list!\n");
         return NULL;
    }

    struct node* p_tmp = p_head->next;
    for(;p_tmp != NULL; p_tmp = p_tmp->next)
    {
        if(p_tmp->num == k)
        {
            break;
        }
    }

    return p_tmp;
}

int list_insert(struct node *p_head, int num)
{
    if(!p_head)
    {
         printf("Empty list!\n");
         return -1;
    }

    struct node *p_node = malloc(sizeof(struct node));

    if(!p_node)
    {
        printf("malloc failed!\n");
        return -1;
    }

    p_node->num  = num;
    p_node->next = p_head->next;
    p_head->next = p_node;

    return 0;
}

int list_delete(struct node *p_head, int num)
{
    struct node *p_node = p_head;
    struct node *p_tmp  = NULL;

    if(!p_head || !p_node)
    {
        printf("Empty list!\n");
        return -1;
    }

    for(;p_node->next != NULL; p_node = p_node->next)
    {
        if(p_node->next->num == num)
        {
            p_tmp = p_node->next;
            p_node->next = p_tmp->next;
            free(p_tmp);
            p_tmp = NULL;
            return 0;
        }
    }

    printf("There is node element %d in the list\n", num);

    return -1;
}

struct node *init_list(void)
{
    struct node *p_head = malloc(sizeof(struct node));

    if(!p_head)
    {
        printf("malloc failed!\n");
        return NULL;
    }

    p_head->next = NULL;
    /*
    **    just garbage number, we don't use this
    **  member in header node
    */
    p_head->num  = -1;

    return p_head;

}

void release_list(struct node* p_head)
{
    if(!p_head)
    {
       return ;
    }

    struct node *p_node = NULL;
    struct node *p_tmp  = NULL;

    for(p_node = p_head ;
        p_node->next != NULL;
        p_node = p_node->next)
    {
        p_tmp = p_node->next;
        p_node->next = p_tmp->next;
        free(p_tmp);
    }

    free(p_node);
}

void list_show(struct node *p_head)
{
    if(!p_head)
    {
        return ;
    }

    struct node *p_node = p_head;
    for(; p_node->next != NULL; p_node = p_node->next)
    {
         printf("\t%d", p_node->next->num);
    }

    printf("\n");
}

/*-------------------------test our API---------------------------------*/
int main()
{
    struct node *p_head = init_list();
    int array[ARRAY_SIZE] = {1,2,3,4,5,6,7,8,9,10};

    int i = 0;
    for(i = 0; i < ARRAY_SIZE; i++)
    {
        if(list_insert(p_head, array[i]) < 0)
        {
            goto failed;
        }
    }

    list_show(p_head);
failed:
    release_list(p_head);
    return 0;
}

双向链表 Doubly linked list

/*********************************************************
Code writer : EOF
Code file   : doubly_linked_list.c
Code date   : 2015.01.15
e-mail      : [email protected]

Code description:
        Here is a implementation for doubly linked list.

    If there is something wrong with my code please touch me
by e-mail, thank you.
**********************************************************/
#include <stdio.h>
#include <stdlib.h>

#define ARRAY_SIZE 10

// Abstraction for our node which is in list.
struct node
{
    int num;
    struct node *next;
    struct node *prev;
};

struct node *list_search(struct node* p_head, int k)
{
    if(!p_head)
    {
         printf("Empty list!\n");
         return NULL;
    }

    struct node* p_tmp = p_head->next;
    for(;p_tmp != NULL; p_tmp = p_tmp->next)
    {
        if(p_tmp->num == k)
        {
            break;
        }
    }

    return p_tmp;
}

int list_insert(struct node *p_head, int num)
{
    if(!p_head)
    {
         printf("Empty list!\n");
         return -1;
    }

    struct node *p_node = malloc(sizeof(struct node));

    if(!p_node)
    {
        printf("malloc failed!\n");
        return -1;
    }

    p_node->num  = num;
    p_node->next = p_head->next;
    p_node->prev = p_head;

    if(p_head->next != NULL)
    {
        p_head->next->prev = p_node;
    }

    p_head->next = p_node;

    return 0;
}

int list_delete(struct node *p_head, int num)
{
    struct node *p_node = p_head;
    struct node *p_tmp  = NULL;

    if(!p_head || !p_node)
    {
        printf("Empty list!\n");
        return -1;
    }

    for(;p_node->next != NULL; p_node = p_node->next)
    {
        if(p_node->next->num == num)
        {
            p_tmp = p_node->next;
            if(p_node->next->next != NULL)
            {
                p_node->next->next->prev = p_node;
            }
            p_node->next = p_tmp->next;

            free(p_tmp);
            p_tmp = NULL;
            return 0;
        }
    }

    printf("There is node element %d in the list\n", num);

    return -1;
}

struct node *init_list(void)
{
    struct node *p_head = malloc(sizeof(struct node));

    if(!p_head)
    {
        printf("malloc failed!\n");
        return NULL;
    }

    p_head->next = NULL;
    p_head->prev = NULL;
    /*
    **    just garbage number, we don't use this
    **  member in header node
    */
    p_head->num  = -1;

    return p_head;

}

void release_list(struct node* p_head)
{
    if(!p_head)
    {
       return ;
    }

    struct node *p_node = NULL;
    struct node *p_tmp  = NULL;

    /*------------------------------
           Use this is OK, but we are
    operating doubly linked list, so
    I try to demo use @prev.

    for(p_node = p_head ;
        p_node->next != NULL;
        p_node = p_node->next)
    {
        p_tmp = p_node->next;
        p_node->next = p_tmp->next;
        free(p_tmp);
    }
    ------------------------------*/

    for(;p_node != NULL; p_node = p_node->next)
    {
        p_tmp = p_node->prev;
        p_tmp->next = p_node->next;
        free(p_node);
    }
    free(p_node);
}

void list_show(struct node *p_head)
{
    if(!p_head)
    {
        return ;
    }

    struct node *p_node = p_head;
    for(; p_node->next != NULL; p_node = p_node->next)
    {
         printf("\t%d", p_node->next->num);
    }

    printf("\n");
}

/*-------------------------test our API---------------------------------*/
int main()
{
    struct node *p_head = init_list();
    int array[ARRAY_SIZE] = {1,2,3,4,5,6,7,8,9,10};

    int i = 0;
    for(i = 0; i < ARRAY_SIZE; i++)
    {
        if(list_insert(p_head, array[i]) < 0)
        {
            goto failed;
        }
    }

    list_show(p_head);
failed:
    release_list(p_head);
    return 0;
}

环形链表 Circle linked list

具体的有点懒了,直接看约瑟夫环的问题吧,典型的环形链表搞定的问题。

http://blog.csdn.net/cinmyheart/article/details/19207097

时间: 2024-08-07 03:38:06

Linked List {singly linked list -> doubly linked list -> circular linked list}的相关文章

循环链表(Circular Linked List)

循环链表(Circular Linked List) 1. 循环链表的概念 循环链表是另一种形式的表示线性表的链表. 循环链表的结点结构与单链表相同,与单链表不同的是链表中表尾结点的link域中不是NULL,而是存放了一个指向链表开始结点的指针. 循环链表与单链表一样,可以有附加头结点,这样能够简化链表操作的实现,统一空表与非空表的运算. 循环链表的示意图: 2. 循环链表的特点 循环链表的特点是无须增加存储量,仅对表的链接方式稍作改变,即可使得表处理更加方便灵活. 循环链表中没有NULL指针.

cc150 Chapter 2 | Linked Lists 2.6 Given a circular linked list, implement an algorithm which returns node at the beginning of the loop.

2.6Given a circular linked list,  implement an algorithm which returns the node at the beginning of the loop. 快指针和慢指针一起在头指针开始移动,快指针每次移动两步,慢指针每次移动一步,直到相遇, 相遇后,把慢指针指向头指针,两个指针一起往后移动一步.直到相遇,相遇的地方就是循环的开始(如果相遇,说明是有循环的节点,这时快指针走的路程是慢指针的两倍,快:开始的k距离,和一圈(或者n圈)循

circular linked list&amp;CLL(循环链表)

循环链表,和苯一样,一条蛇咬住了自己的尾巴.在 操作系统 给 进程 分配运行 资源 时,有体现. 1 #include<stdio.h> 2 #include<stdlib.h> 3 4 struct node 5 { 6 int data; 7 struct node * next; 8 }; 9 //插入动作,在头节点之前插入 10 void insert(struct node ** head_ref, int data) 11 { 12 struct node * new_

707. Design Linked List

题目链接:https://leetcode.com/problems/design-linked-list/ Design your implementation of the linked list. You can choose to use the singly linked list or the doubly linked list. A node in a singly linked list should have two attributes: val and next. val

707. Design Linked List - Easy

Design your implementation of the linked list. You can choose to use the singly linked list or the doubly linked list. A node in a singly linked list should have two attributes: val and next. val is the value of the current node, and next is a pointe

【LeetCode】链表 linked list(共34题)

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica } [2]Add Two Numbers (2018年11月30日,第一次review,ko) 两个链表,代表两个整数的逆序,返回一个链表,代表两个整数相加和的逆序. Example: Input: (2 -> 4 -> 3) + (5 -> 6 -> 4) Output: 7 -> 0 -> 8 Explanation: 342 + 465 =

[LeetCode]77. Reverse Linked List反转链表

Reverse a singly linked list. click to show more hints. Hint: A linked list can be reversed either iteratively or recursively. Could you implement both? Subscribe to see which companies asked this question 解法1:一个最简单的办法就是借助栈的后进先出功能,先扫描一遍链表保存每个节点的值,然后再

Lettcode_206_Reverse Linked List

本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/45739753 Reverse a singly linked list. click to show more hints. Hint: A linked list can be reversed either iteratively or recursively. Could you implement both? 思路: (1)题意为给定链表,将其(逆

leetcode_206题——Reverse Linked List(链表)

Reverse Linked List Total Accepted: 1726 Total Submissions: 4378My Submissions Question Solution Reverse a singly linked list. click to show more hints. Hide Tags Linked List Have you met this question in a real interview? Yes No Discuss 这道题比较简单,主要是给