Data Structure Study Notes (3) Linear Tables (3)

Data Structure Study Notes (3) Linear Tables (3)

circular linked list

The concept of circular linked list

  The first node and the last node of the circular linked list are linked together. The first node of a circular linked list is before the last node, and vice versa.

Circular linked list implementation in C language

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

typedef struct LNode
{
    
    
    int data;
    struct LNode *next;
}LinkList, LNode;
LinkList *creat()
{
    
    
    LinkList *head, *p1, *p2;
    int i;
    if ((head = (LinkList *)malloc(sizeof(LNode))) == NULL)
    {
    
    
        printf("Error");
        exit(0);
    }
    p1 = p2 = head;
    printf("输入创建链表的长度:");
    scanf("%d, &head->data");//创建链表,带头结点,头结点数据域表示输入的个数
    if(head->data==0)
    {
    
    
        head->next = NULL;
        printf("已创带头结点的空链表");
    }
    else
    {
    
    
        printf("输入数据:\n");
        for (i = 0; i < head->data;i++)
        {
    
    
            if((p1=(LinkList*)malloc(sizeof(LNode)))==NULL)
            {
    
    
                printf("Error");
                exit(0);
            }
            scanf("%d", &p1->data);
            p2->next = p1;
            p2 = p1;
        }
        p1->next = head;
    }
    return (head);
}

void print(LinkList *head)
{
    
    
    LinkList *p;
    p = head->next;
    while(p!=head)
    {
    
    
        printf("%d", p->data);
        p = p->next;
    }
    printf("\n");
}
void main()
{
    
    
    LinkList *head;
    head = creat();
    print(head);
}

Doubly linked list

Doubly linked list concept

  In the doubly linked list, there is not only a pointer to the next node, but also a pointer to the previous node. In this way, the previous node can be accessed from any node, or the next node can be accessed, or even the entire linked list.

  Each node of the doubly linked list has two pointer fields and one data field.

C Language Implementation of Doubly Linked List

#include <stdio.h>
#include <malloc.h>

typedef struct DuLNode
{
    
    
    int i;
    struct DuLNode *next, *prior;
} DuLNode, *DuLinkList;

DuLNode *create_list()
{
    
    
    int a[] = {
    
    1, 2, 3, 4, 5};
    int j;
    DuLNode *head, *p1, *p2;
    p2 = head = (DuLinkList)malloc(sizeof(DuLNode));
    head->next = head->prior = NULL;
    for (j = 4; j >= 0;j--)
    {
    
    
        p1 = (DuLinkList)malloc(sizeof(DuLNode));
        p1->prior = head;
        p1->next = head->next;
        head->next = p1;
    }
    return head;
}

DuLNode *insert_list(DuLNode *head, int i, int num)
{
    
    
    DuLNode *p, *q;
    int j;
    for (j = 1, p = head->next; j < i && p->next;j++)
    {
    
    
        q = p->next;
        q->prior = p;
        p = q;
    }
    q = (DuLinkList)malloc(sizeof(DuLNode));
    q->i = num;
    q->prior = p->prior;
    q->next = p;
    p->prior->next = q;
    p->prior = q;
    return head;
} 

Guess you like

Origin blog.csdn.net/I_m_Gagaga/article/details/127750286