Farey sequence and doubly linked list

Farey sequence

Description

Farey sequence is a sequence: its first level sequence is defined as (0/1, 1/1), this sequence is extended to the second level to form a sequence (0/1, 1/2, 1/1), and the extension To the third pole to form a sequence (0/1, 1/3, 1/2, 2/3, 1/1), to expand to the fourth level to form a sequence (0/1, 1/4, 1/3, 1 /2, 2/3, 3/4, 1/1). In each level n, if any two adjacent scores a/c and b/d of the previous level satisfy (c+d)<=n, a new score (a+b)/(c+ d) Insert between two scores. For a given value of n, each score contained in its nth-level sequence is output in turn.

Input
input an integer n (0<n<=100)

Output
sequentially outputs each score contained in the nth-level sequence, each row outputs 10 scores, and two adjacent scores in the same row are separated by a tab.

Sample
Input

6

Output

0/1 1/6 1/5 1/4 1/3 2/5 1/2 3/5 2/3 3/4

4/5 5/6 1/1

Code

#include <stdio.h>
#include <stdlib.h>
typedef struct node
{
    
    
	int a;
	int b;
	struct node* next;
}chain;
void add(chain* head,int n)//插入操作
{
    
    
	chain* p, * q,*r;
	p= head->next;
	while (p->next)
	{
    
    
		q = p->next;
		if (q->b + p->b <= n)
		{
    
    
			r = (chain*)malloc(sizeof(chain));
			r->a = q->a + p->a;
			r->b = q->b + p->b;
			p->next = r;
			r->next = q;
		}
		p = p->next;
	}
}
void print(chain* head)//打印操作
{
    
    
	chain* q;
	q = head->next;
	int sum = 0;
	while (q)
	{
    
    
		sum++;
		if(sum!=10)
		printf("%d/%d\t", q->a, q->b);
		else
		{
    
    
			sum = 0;
			printf("%d/%d\n", q->a, q->b);
		}
		q = q->next;
	}
}
int main()
{
    
    
	chain* head, * p1, * p2;
	head = (chain*)malloc(sizeof(chain));
	head->next = NULL;
	int n;
	scanf("%d", &n);
	p1 = (chain*)malloc(sizeof(chain));
	p2 = (chain*)malloc(sizeof(chain));
	p2->next = head->next;
	head->next = p1;
	p1->next = p2;
	p1->a = 0;
	p1->b = p2->a = p2->b = 1;
	for (int i = 2; i <= n; i++)
		add(head, i);
	print(head);
	return 0;
}

Doubly linked list

Description

After learning a singly linked list, we have another ability to solve problems. The singly linked list can find the next position in the memory using a pointer. This is a chain that will not easily break. But the singly linked list has a weakness-it cannot be referred back. For example, there are two nodes A and B in the linked list. Their relationship is that B is the successor of A, and A points to B. You can easily find B through A, but you cannot find A from B. A simple idea can easily solve this problem-build a doubly linked list. In the doubly linked list, A has a pointer to node B, and at the same time, B has a pointer to A. In this way, not only can it traverse all the nodes of the entire linked list from the position of the head node of the linked list, but also can traverse all the nodes from the end node of the linked list. For a given column of data, build a doubly linked list in the given order, find the corresponding node according to the key, and output the key of the predecessor node and the key of the successor node of this node.
Input

In the first line, two positive integers n (representing the number of nodes) and m (representing the number of keywords to be found). The second line is n numbers (n numbers are not repeated), use these n numbers to build a doubly linked list. Next, there are m keywords, each on a line.
Output

For each given keyword, output the predecessor node keyword and successor node keyword of this keyword. If the given keyword has no predecessor or successor, it will not be output.
Note: The output for each given keyword occupies one line. There is a space between the output data of a line, and there is no space at the beginning and end of the line.

Sample
Input

10 3

1 2 3 4 5 6 7 8 9 0

3

5

0

Output

2 4

4 6

9

Code

#include <stdio.h>
#include <stdlib.h>
typedef struct node
{
    
    
    int data;
    struct node* qian;
    struct node* hou;
}chain;
chain* set(int n)//建表
{
    
    
    chain* head, * p, * q, * end;
    head = (chain*)malloc(sizeof(chain));
    head->hou = NULL;
    end = head;
    for (int i = 0; i < n; i++)
    {
    
    
        p = (chain*)malloc(sizeof(chain));
        scanf("%d", &p->data);
        p->hou = end->hou;
        end->hou = p;
        p->qian = end;
        end = p;
    }
    return head;
}
int main()
{
    
    
    int n, m, i, x;
    chain* head, * p, * q, * end;
    scanf("%d %d", &n, &m);
    head = set(n);
    for (i = 0; i < m; i++)
    {
    
    
        scanf("%d", &x);
        q = head->hou;
        q->qian = NULL;
        while (q)
        {
    
    
            if (q->data == x)
            {
    
    
                if (q->hou == NULL)
                    printf("%d\n", q->qian->data);
                else if (q->qian == NULL)
                    printf("%d\n", q->hou->data);
                else printf("%d %d\n", q->qian->data, q->hou->data);
            }
            q = q->hou;
        }
    }
    return 0;
}

Description

This time make up the two questions in the linked list, which are also written in C.Insert picture description here

Guess you like

Origin blog.csdn.net/rookie636/article/details/109038930