Data structure Learning notes--Application of linear table

Source: Internet
Author: User

Data structure Learning notes--Application of linear table

Application of Linear table

Natural Connections for linear tables

The simple natural join process of calculating any two tables discusses the application of linear tables. Suppose there are two tables A and B, M1 rows, n1 columns, and M2 rows, N2 columns, they are simple natural connection results c=a*b (i==j), where I represents the column number in table A, J represents the column number in table B, and C is a and B for all the groups of records in the Cartesian product that meet the specified join conditions, The connection condition is that column I of table A is equal to column J of Table B.
Such as:
1 2 3) 3 5
A = 2 3 3 B = 1 6
1 1 1) 3 4

1 2 3) 3 5
1 2 3) 3 4
A*b (3==1) = 2 3 3 3 5
2 3 3) 3 4
1 1 1) 1 6
Data Organization

Because the number of rows per table is not deterministic, a single-linked list is used as the table's storage structure, with each row as a data node. In addition, the number of data in each row is indeterminate, but because the data in the row is provided randomly, the data in each row is stored in a sequential storage structure, where the data for each row is stored in an array of length maxcol. Therefore, the data node type in the single linked list is defined as follows:

#indefine Maxcol 10//Maximum number of columns
typedef struct NODEL
{
Elemtype Data[maxcol];
Struct Nodel *next; Point to subsequent nodes
}dlist; Defining Data Node types

In addition, you need to specify the number of rows and columns for each table, which defines the head node type of the single-linked list as follows:

typedef struct NODE2/* Defines the head node type */
{
int row,col; /* Number of rows and columns */
Dlist *next; /* point to the First Data node */
} hlist;

The head node here has additional information

Establishing a linear table

Code

Join table algorithm

In order to achieve a simple natural connection between the two tables H1 and H2, first enter two table concatenated column ordinal F1 and F2, and then scan the single-linked list H1, for each node of H1, from start to finish scanning single-linked list H2, if the natural connection condition is established, that is H1 current node *p and H2 current node *q meet:

P->data[f1-1]==q->data[f2-1]

Adds a new node in the new single-linked list H.

The new single-linked list H is also created by using the method of tail interpolation to build a table.

1 void Link (hlist *h1,hlist *h2,hlist *&h)
2 {
3 int F1,f2,i;dlist *p=h1->next,*q,*s,*r;
4 printf ("Connection field is: 1th", "2nd": ");
5 scanf ("%d%d", &F1,&F2);
6 h= (Hlist *) malloc (sizeof (hlist));
7 h->row=0;
8 h->col=h1->col+h2->col;
9 h->next=null;
Ten while (P!=null)
11 {
q=h2->next;
while (Q!=null)
if (p->data[f1-1]==q->data[f2-1)///corresponding field values are equal */
s= (Dlist *) malloc (sizeof (dlist));
16/* Create a data node */
i=0;is->data[i]=p->data[i];
for (i=0;is->data[h1->col+i]=q->data[i];/* Copy the current row of table 2 */
if (h->next==null) h->next=s;
r->next=s; Else
R=s; /*r always points to the last data node */
h->row++; /* Table row count increased 1*/
25}
q=q->next; /* Table 2 Move down one record */
27}
p=p->next; /* Table 1 Move down one record */
29}
r->next=null;/* footer node Next field empty */
31}

Ordered table

The so-called ordered table refers to such a linear table, where all elements are arranged in an ascending or descending manner, and that there is no element of the same element value in an ordered table. This is still stored in a sequential table.

Only the Listinsert () basic operation differs from the previous sequential table, and the remainder is the same. The Listinsert () operation of an ordered table corresponds to the following algorithm:


Code

Example: Designing an algorithm to merge two ordered tables into an ordered table

Example: a single-linked list of three leading nodes la,lb and the nodes in the LC are arranged according to the elements from childhood to large non-increment (each linked list does not have the same number of points), but there may be 3 links in the list of points. Write an algorithm to do the following:

Causes LA to leave only the nodes of the elements that are contained in the 3 tables, with no nodes of the same data value, and free all useless nodes in LA. The algorithm is required to have a time complexity of O (m+n+p) and the length of the m\n\p is 3 tables respectively.


Calculation of a unary polynomial (not evaluated)

For a unary polynomial:

P=p0+p1x+.......+pnxn

In a computer, you can use a linear table to represent:

P = (P0, p1, ..., PN)

But for a shape like

S (x) = 1 + 3x10000–2x20000

Polynomial, is the above representation appropriate?

In general, one-element sparse polynomial can be written as

Pn (x) = P1xe1 + P2xe2 + ... + Pmxem

Where: Pi is a non-0 coefficient of an exponent of EI,

0 E1 < E2 < < EM = n

You can represent the following linear table:

(p1, E1), (P2, E2), , (pm,em) )

For example :

P999 (x) = 7x3-2x12-8x999

Available linear tables

( (7, 3), ( -2, a), ( -8, 999))

Said

So the data can be organized into:

typedef orderedlinklist POL;
Polynomial is represented by an ordered linked list with a header node

the data element type of the node is defined as :

Representation of typedef struct {//item
float Coef; Coefficient
int expn; Index
} term, elemtype;


The basic operations that should be implemented are:

Establish

Creatpolyn (&p, M)

operation result : Enter the coefficients and exponents of M-term,

Establishes a unary polynomial P.

Destroyed

Destroypolyn (&P)

Initial conditions : a unary polynomial P already exists.

operation result : Destroy the unary polynomial P.

Print

Printpolyn (&P)

Initial conditions : a unary polynomial P already exists.

operation result : Print out the unary polynomial P.

Number of items

Polynlength (P)

Initial conditions : a unary polynomial P already exists.

operation result : Returns the number of items in a unary polynomial P.

Add

Addpolyn (&PA, &PB)

Initial conditions : a unary polynomial Pa and Pb already exist.

operation result : completes the polynomial summation operation, namely:

Pa = PA+PB, and destroys a unary polynomial Pb.

Subtraction

Subtractpolyn (&PA, &PB)

Initial conditions : a unary polynomial Pa and Pb already exist.

operation result : Complete the polynomial subtraction operation, namely:

Pa = PA-PB, and destroys a unary polynomial Pb.

Code

Code

Code

Joseph Ring Question

Joseph Ring (Josephus) was raised by ancient Roman historian Joseph Josephus, who participated in and recorded the uprising of the Jews against Rome in the year 66-70. Joseph, as a general, managed to hold the city of Juda for 47 days, and after the fall of the city, he and 40 diehard soldiers took refuge in a nearby cave. There, the rebels voted to say "surrender or death." So Joseph suggested that everyone take turns to kill the person next to him, and this order was decided by lots. Joseph had deliberately caught the last sign and, as one of the two survivors of the cave, persuaded his former victim to surrender to Rome.
Joseph Ring the specific description of the problem is: with a number of 1,2,......,n N (n>0) individuals in a circle, from the 1th person began to count, report m when the stop Count, reported m of the people out of the circle, and then from his next person to re-count, report m stop Count, reported m out of the ring, ... Until everyone is out of the loop. After any given N and M, the design algorithm asks for the order of the n person out of the loop.

WORKAROUND: Set up a loop linked list with headless nodes (with head node also)

Another method (mathematical method) time complexity O (n) [go]


Whether using a linked list or an array implementation has one thing in common: to simulate the entire game process, not only the program is more annoying, and time is complex.
Degrees up to O (nm), when the n,m is very large (for example, millions, tens of thousands), there is almost no way to produce results in a short period of time.

In order to discuss the convenience, the problem is changed slightly, does not affect the original intention:

Problem description: N Person (number 0~ (n-1)), starting from 0 count, reporting (M-1) exit, the remainder continues to count off from 0. Find the winner's number


We know that the first person (the number must be m%n-1) after the dequeue, the rest of the n-1 individuals formed a new Joseph ring (with the number of k=m%n people open
Start):
K k+1 k+2 ... n-2, n-1, 0, 1, 2, ... k-2
And at the beginning of the K is reported 0.

Now let's do a conversion of their numbers:
K--0
K+1-1
K+2-2
...
...
K-2-N-2
K-1-N-1

After the transformation has completely become the (n-1) personal count of the sub-problem, if we know the solution of this sub-problem: for example, X is the final winner, then the root
According to the above table to turn this x back is not just the solution of n personal situation?! The formula to change back is very simple, I believe everyone can push it out: X '
= (x+k)%n

How to Know (n-1) The solution of the problem of personal count off? Yes, as long as you Know (n-2) the individual's solution. (n-2) A personal solution? Of course, the first to ask (N-3)
Situation----This is obviously a backward question! Okay, here's the idea, here's the recursive formula:

Make f[i] means I personally play the game reported M exit the last winner's number, the final result is naturally f[n]

Recursive formulas
f[1]=0;
f[i]= (f[i-1]+m)%i; (i>1)

With this formula, all we have to do is calculate the value of F[i] from the 1-n order, and the final result is f[n]. Because the real life number always starts from 1,
We output f[n]+1

Because it is a step-by-step recursive and does not need to save each f[i], the program is also exceptionally simple:

# include <stdio.h>
Main ()
{
int n, m, I, s=0;
printf ("N M ="); scanf ("%d%d", &n, &m);
for (i=2; i<=n; i++) s= (s+m)%i;
printf ("The winner is%d\n", s+1);
}


The time complexity of the algorithm is O (n), which has been greatly improved compared with the simulation algorithm. Count N,m equals 1 million, 10 million is not a problem. =============================================================================================================== ===== It is also stated that this method can only be found in the O (n) method to find out the position of any person out of the first. If the entire sequence is requested, this method is still O (n^2). Please go out of sequence, with the number of segments can be done O (Nlog (n)). MS is no faster than this.

Data structure Learning notes--Application of linear table

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.