当前位置:网站首页>Double linked list -- tail interpolation construction (C language)

Double linked list -- tail interpolation construction (C language)

2022-06-26 07:52:00 Don't hit me, it hurts!

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

// Parameter description :data Store the data
//         prior Pointer to direct precursor
//           next Pointer to the successor node
typedef struct DNode
{
    int data;
    DNode *prior;
    DNode *next ;
}DNode;

void create_tail_DLinklist(DNode *&head)// Using tail interpolation method to build double linked list
{
    head = (DNode*)malloc(sizeof(DNode) );
    head->data = 0 ;
    head->prior= NULL ;
    head->next = NULL ;// Create a header node

    DNode * p = head;  // Point to the head node
    int n;
    printf(" Please enter the length of the double linked list :");
    scanf("%d",&n);

    while(n--)
    {
        DNode* q = (DNode*)malloc(sizeof(DNode));
        scanf("%d",&q->data);
        q->next = NULL ; // Can not write q->next = head->next; otherwise , It's a dead cycle ?

        q->prior = p;
        p->next = q ;
        p = q ;
    }

}

void Print_DLinklist(DNode *&head)
{
        DNode * p = head ;
    DNode * q = NULL ;
    p = p->next ;
    while (p != NULL)
    {
        printf("%d ",p->data); // Output from front to back
        q = p ;
        p = p->next ;
    }

}
int main()
{
    DNode * head ;
    create_tail_DLinklist(head);
        Print_DLinklist(head);

    return 0;
}
 

Running results :

 

原网站

版权声明
本文为[Don't hit me, it hurts!]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202170609120231.html