美文网首页程序员
数据结构和算法之一——线性表_3_链式存储结构_3_双链表

数据结构和算法之一——线性表_3_链式存储结构_3_双链表

作者: Eric_Hunter | 来源:发表于2018-02-04 16:16 被阅读0次
  1. 双向链表概念
    双向链表也叫双链表,是链表的一种,它的每个数据结点中都有两个指针,分别指向直接后继和直接前驱。所以,从双向链表中的任意一个结点开始,都可以很方便地访问它的前驱结点和后继结点。(引用于:百度百科)

2.代码部分
1)双向链表节点结构:

//例1.
typedef struct node
{
    Elemtype data;
    struct node *previous;
    struct node *next;
}Node;

2)双向链表的初始化和创建:

//例2
Status InitLinkList(Node **node)
{
    *node = (Node *)malloc(sizeof(Node));
    (*node)->next = *node;
    (*node)->previous = *node;
}

Status CreateLinkList(Node **node)      //这里创建的是循环双向链表
{
    char i;
    Node *target, *temp, *temp2;

    target = *node;

    printf("Please input the elements you want in the LinkList(input 0 means stop input):");
    scanf("%c", &i);
    getchar();
    target->data = i;
    
    while(i)
    {
        temp = (Node *)malloc(sizeof(Node));
        scanf("%c", &i);
        if(i == '0') break;
        getchar();
        temp->data = i;
        temp2 = target;
        target->next = temp;
        target = target->next;
        target->previous = temp2;
        
    }

    target->next = *node;
    (*node)->previous = target;
}

3)双向链表的插入:

//例3.
Status Insert(Node **node, int i, Elemtype e)
{
    Node *target, *temp;
    int j;
    target = *node;

    temp = (Node *)malloc(sizeof(Node));
    temp->data = e;

    if(i == 1)
    {
        temp->previous = target->previous;
        target->previous->next = temp;
        temp->next = target;
        target->previous = temp;
        *node = temp;//(target = temp;)?
    }
    else
    {
        for(j = 1; j < i; j++)
        {
            target = target->next;
        }
        temp->next = target;
        temp->previous = target->previous;
        target->previous->next = temp;
        target->previous = temp;
        //target->previous->next = temp;
        //temp->previous = target->previous;
        //temp->next = target;
        //target->previous = temp;
    }
}

4)双向链表的删除:(注意,这里创建的是循环双向链表)

//例4.
Status Delete(Node **node, int i)
{
    Node *target, *temp;
    int j;
    target = *node;
        
    if(i == 1)
    {
        temp = target->next;
        temp->previous = target->previous;
        target->previous->next = temp;
        *node = temp;
    }   
    else
    {
        for(j = 1; j < i; j ++)
        {
            target = target->next;
        }
        target->previous->next = target->next;
        target->next->previous = target->previous;
    }
}
//例5,总体框架代码(注意,这里创建的是循环双向链表)
#include<stdio.h>
#include<stdlib.h>

typedef void Status;
typedef int Elemtype;

typedef struct node
{
    Elemtype data;
    struct node *previous;
    struct node *next;
}Node;

Status InitLinkList(Node **node)
{
    *node = (Node *)malloc(sizeof(Node));
    (*node)->next = *node;
    (*node)->previous = *node;
}

Status CreateLinkList(Node **node)  
{
    char i;
    Node *target, *temp, *temp2;

    target = *node;

    printf("Please input the elements you want in the LinkList(input 0 means stop input):");
    scanf("%c", &i);
    getchar();
    target->data = i;
    
    while(i)
    {
        temp = (Node *)malloc(sizeof(Node));
        scanf("%c", &i);
        if(i == '0') break;
        getchar();
        temp->data = i;
        temp2 = target;
        target->next = temp;
        target = target->next;
        target->previous = temp2;
        
    }

    target->next = *node;
    (*node)->previous = target;
}

Status CreateLinkList2(Node **node)
{
    Elemtype i;
    Node *target, *temp, *temp2;

    target = *node;
    target->data = 65;
    for(i = 66; i <= 90; i++)
    {
        temp = (Node *)malloc(sizeof(Node));
        temp->data = i;
        target->next = temp;
        temp2 = target;
        target = target->next;
        temp2->next = target;
        target->previous = temp2;
    }
    target->next = *node;
    (*node)->previous = target;
}

Status Visit(Node *node)
{
    if(node)
    {
        printf("%c", node->data);
    }
}

Status TraversalLinkList(Node **node)       //正着看
{
    Node *target;
    target = *node;

    Visit(target);
    target = target->next;
    while(target != *node)
    {
        Visit(target);
        target = target->next;
    }
    printf("\n");
}

Status TraversalLinkList2(Node **node)      //倒着看
{
    Node *target;
    target = *node;

    target = target->previous;
    Visit(target);
    while(target != *node)
    {   
        target = target->previous;
        Visit(target);
    }
    printf("\n");
}

Status Insert(Node **node, int i, Elemtype e)
{
    Node *target, *temp;
    int j;
    target = *node;

    temp = (Node *)malloc(sizeof(Node));
    temp->data = e;

    if(i == 1)
    {
        temp->previous = target->previous;
        target->previous->next = temp;
        temp->next = target;
        target->previous = temp;
        *node = temp;//(target = temp;)?
    }
    else
    {
        for(j = 1; j < i; j++)
        {
            target = target->next;
        }
        temp->next = target;                //这里链接的次序需要格外注意
        temp->previous = target->previous;
        target->previous->next = temp;
        target->previous = temp;
        //target->previous->next = temp;
        //temp->previous = target->previous;
        //temp->next = target;
        //target->previous = temp;
    }
}

Status Delete(Node **node, int i)
{
    Node *target, *temp;
    int j;
    target = *node;
        
    if(i == 1)
    {
        temp = target→next;             //这里链接的次序需要格外注意
        temp->previous = target->previous;
        target->previous->next = temp;
        *node = temp;
    }   
    else
    {
        for(j = 1; j < i; j ++)
        {
            target = target->next;
        }
        target->previous->next = target->next;
        target->next->previous = target->previous;
    }
}

void main()
{
    Node *node;
    InitLinkList(&node);
    CreateLinkList(&node);  //使用该函数构造链表的话由于scanf会输入“\n”,所以会造成插入、删除错误(然而插入getchar()函数可以去除scanf函数产生的“\n”)
    //CreateLinkList2(&node);
    TraversalLinkList(&node);
    Insert(&node, 6, 'u');
    TraversalLinkList(&node);
    Delete(&node, 2);
    Delete(&node, 5);
    TraversalLinkList(&node);
    TraversalLinkList2(&node);
}
  1. 例题:
    要求实现用户输入一个数使得 26 个字母的排列发生变化,例如用户输入 3 ,输出结果:
    DEFGHIJKLMNOPQRSTUVWXYZABC
    同时需要支持负数,例如用户输入 -3 ,输出结果:
    XYZABCDEFGHIJKLMNOPQRSTUVW
//例6
#include<stdio.h>
#include<stdlib.h>

typedef char Elemtype;
typedef void Status;

typedef struct node 
{
    Elemtype data;
    struct node *next;
    struct node *previous;
}Node;

Status Traversal(Node **head)
{
    Node *target;

    target = *head;
    
    if (target)
    {
        printf("%c ", target->data);
        target = target->next;
        while(target && target != *head)
        {
            printf("%c ", target->data);
            target = target->next;
        }
    }
}

Status InitDoubleLinkList(Node **head)
{
    *head = (Node *)malloc(sizeof(Node));
    if(!(*head))
    {
        printf("ERROR! There is no more memory space!");
        exit(0);
    }
    (*head)->next = *head;
    (*head)->previous = *head;
}

Status CreateDoubleLinkList(Node **head)
{
    Node *target, *temp1, *temp2;
    char c = 65;
    int n;

    target = *head;
    target->data = c;
    
    for(n = 0, c = 66; n < 25; n++, c++)
    {
        temp1 = (Node *)malloc(sizeof(Node));
        temp1->data = c;
        temp2 = target;
        target->next = temp1;
        target = target->next;
        target->previous = temp2;
    }
    target->next = *head;
    (*head)->previous = target;
}

Status PlayWords(Node **head, int n)
{
    Node *target1, *target2;        //target1表示head, target2表示rear
    int i;
    target1 = *head;
    target2 = (*head)->previous;

    if(n > 0)
    {
        for(i = 1; i < n; i++)
        {
            target1 = target1->next;
        }
        *head = target1;
    }
    else if(n < 0)
    {
        for(i = -1; i > n; i--)
        {
            target2 = target2->previous;
        }
        *head = target2;
    }
}

void main()
{
    Node *L;
    int n;

    printf("Please input the number:");
    scanf("%d", &n);

    InitDoubleLinkList(&L);
    CreateDoubleLinkList(&L);
    Traversal(&L);
    printf("\n");
    PlayWords(&L, n);
    Traversal(&L);
}

相关文章

网友评论

    本文标题:数据结构和算法之一——线性表_3_链式存储结构_3_双链表

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