Data Structure-Loop List

Posted by gcussi on Thu, 06 Jun 2019 00:55:26 +0200

One

Circular linked list is another form of chain storage structure. Its characteristic is that the pointer field of the last node in the list points to the head node, and the whole list forms a ring. From any node in the table, other nodes in the table can be found. Its operation is basically the same as that of single linked list. The difference is that the cyclic condition is not whether P or p-> next is empty, but whether it is equal to the head pointer.

Two

The node definition of a circular list is the same as that of a single list. In order to generate a circular list, R - > next = head (that is, the last pointer field of a node points to the head node)

typedef struct node
{
    int data;
    struct node *next;
}sqlist, *linkList;

Three

Latin Square Matrix:

#include "stdio.h"
#include "stdlib.h"

#define OK 1
#define ERROR 0

typedef int Status;

typedef struct node
{
    int data;
    struct node *next;
} *linkList, sqlist;

//output data
Status visit(int e)
{
    printf("%d ", e);
    return OK;
}

//Output the entire list
Status ListTraverse(linkList L)
{
    linkList p = L;
    while(1)
    {
        visit(p->data);
        p = p->next;
        if(p == L)
        {
            return OK;
        }
    }
}

//Initialize loop list
Status InitList(linkList *L)
{
    *L = (linkList)malloc(sizeof(sqlist));
    if(!(*L))
    {
        return ERROR;
    }
    (*L)->next = *L;
    //printf("Initialization% D%,* L, (* L) - > next);
    return OK;
}

//Generating circular linked list
Status CreateList(linkList *L, int i)
{
    linkList p, r;
    int k=1;
    *L = (linkList)malloc(sizeof(sqlist));
    (*L)->next = NULL;
    r = *L;
    (*L)->data = k;
    //printf("(*L) element value:% d", (* L) - > data);
    for(k=2; k<=i; k++)
    {
        p=(linkList)malloc(sizeof(sqlist));
        p->data = k;
        //printf("element value:% d", p - > data);
        r->next = p;
        r = p;
    }
    p->next = *L;
    return OK;
}

//Cyclic list length
int ListLength(linkList L)
{
    linkList p = L;
    int i=0;
    while(1)
    {
        p = p->next;
        i ++;
        if(p==L)
            return i;
    }
}

//Latin matrix output
Status LatinOut(linkList L)
{
    int i = ListLength(L);
    int j;
    linkList p, r;
    p = L;
    for(j=0; j<i; j++)
    {
        ListTraverse(p);
        printf("\n");
        r = p->next;
        p = r;//Exchange the position of P and r to make p - > next the starting point of the new list
    }
    return OK;
}

int main(void)
{
    linkList L;
    int i;
    InitList(&L);
    printf("\n Please enter the number (1)-9): ");
    scanf("%d", &i);
    CreateList(&L, i);

    printf("Arrange them as follows:\n");
    ListTraverse(L);
    printf("\n\n");
    printf("Latin Square Matrix:\n");
    LatinOut(L);

    return 0;
}

Magician's licensing: The magician uses 13 spades arranged in order. The first one opens the spade A and puts it on the table. Then he continues. Count 1 and 2 to put the first one at the bottom of the card. The second one is just spade 2, put it on the table, and then turn all the cards out. So how do magicians place their cards in order?

#define CardNumber 13

//Create a circular list
linkList CreateLinkList()
{
    linkList head = NULL;
    linkList s, r;
    int i;
    r = head;
    for(i=1; i<=CardNumber; i++)
    {
        s = (linkList)malloc(sizeof(sqlist));
        s->data = 0;

        if(head == NULL)
            head = s;
        else
            r->next = s;

        r = s;
    }
    r->next = head;
    return head;
}

void Magician(linkList head)
{
    linkList p;
    int j;
    int CountNumber = 2;
    p = head;
    p->data = 1; //First card
    while(1)
    {
        for(j=0; j<CountNumber; j++)
        {
            p = p->next;
            if(p->data != 0)
            {
                p->next;
                j--;
            }
        }
        if(p->data == 0)
        {
            p->data = CountNumber;
            CountNumber ++;
            if(CountNumber == 14)
                break;
        }
    }
}

void DestroyList(linkList *list)
{
    linkList ptr = *list;
    linkList buff[CardNumber];
    int i = 0;
    while(i < CardNumber)
    {
        buff[i++] = ptr;
        ptr = ptr->next;
    }
    for(i=0; i<CardNumber; ++i)
        free(buff[i]);
    *list = 0;
}

int main(void)
{
    linkList p;
    int i;

    p = CreateLinkList();
    Magician(p);

    printf("Arrange them as follows:\n");
    for(i=0; i<CardNumber; i++)
    {
        printf("Spade%d ", p->data);
        p = p->next;
    }
    DestroyList(&p);

    return 0;
}

When dealing with magicians using circular lists, we must consider a very realistic problem. Every card is turned out and put on the desktop, there will be one less card in the stack, and the length of circular lists is 13, and the elements in the list remain unchanged. Therefore, when arranging the cards in order, we should "remove" the cards that have been turned out beforehand, that is to say:

    if(p->data != 0) //The accessed node has elements that are "skipped"
    {
         p->next;
         j--;
    }

Topics: less