C ++ data structure and algorithm _ 2 _ linear table, data structure and algorithm _ 2

Source: Internet
Author: User

C ++ data structure and algorithm _ 2 _ linear table, data structure and algorithm _ 2
Application Example of sequence table-parallel operation and intersection operation of sequence table

# Include "SeqList. h "using namespace std; // calculate void unionSL (SeqList <int> & LA, SeqList <int> & LB) {int m = LA. length (); int n = LB. length (); for (int I = 1; I <= n; ++ I) {int ival; LB. getData (I, ival); // retrieves int searchVal = LA from the LB table. search (ival); if (searchVal = 0) // if the element does not exist in the LA table {LA. insert (m, ival); // Insert to + + m ;}}// submit the void intersectionSL (SeqList <int> & LA, SeqList <int> & LB) {int m = LA. length (); in T I = 1; while (I <= m) {int ival; LA. getData (I, ival); // retrieves int searchVal = LB from LA. search (ival); if (searchVal = 0) // if the data is not found in LB {LA. remove (I, ival); // delete it -- m; // shorten the table length [do not forget!]} Else {++ I ;}}// test program int main () {freopen ("input", "r", stdin); SeqList <int> L1, L2; l1.input (); L2.input (); unionSL (L1, L2); L1.output (); cout <endl; SeqList <int> L3; L3.input (); intersectionSL (L3, l2); L3.output ();}

/** Test data ** 5 1 2 3 4 5 6*4 11 2 3 4 9 ** 4 11 12 13 4 19 */


/* Output result (because redirection is used, the input data is not displayed on the screen )*/





Data Structure algorithm 21 two linear tables la and lb, the new set that, how to write the C language version of the complete program

I wrote it. compile it and change it.

Void Union (LinkList * L1, LinkList * L2, LinkList * & L3) // Intersection
{
LinkList * p = L1-> next, * q = L2-> next, * s, * c;
L3 = (LinkList *) malloc (sizeof (LinkList ));
L3-> next = NULL;
C = L3;
While (p! = NULL & q! = NULL)
{If (p-> data <q-> data)
{S = (LinkList *) malloc (sizeof (LinkList); // copy Node
S-> data = p-> data;
C-> next = s; c = s;
P = p-> next;
}
Else if (p-> data> q-> data)
{S = (LinkList *) malloc (sizeof (LinkList ));
S-> data = q-> data;
C-> next = s; c = s;
Q = q-> next;
}
Else
{
S = (LinkList *) malloc (sizeof (LinkList ));
S-> data = p-> data;
C-> next = s; c = s;
P = p-> next;
Q = q-> next;
}
}

While (q! = NULL)
{
S = (LinkList *) malloc (sizeof (LinkList ));
S-> data = q-> data;
C-> next = s; c = s;
Q = q-> next;
}
C-> next = NULL;

While (p! = NULL)
{
S = (LinkList *) malloc (sizeof (LinkList ));
S-> data = p-> data;
C-> next = s; c = s;
P = p-> next;
}
C-> next = NULL;

}


Intersection of two linear tables A and B in the data structure Algorithm

Sort A and B respectively, and then submit.
For example, sort A and B in ascending order, Set A to P, and B to Q. If A [P]> B [Q], Q ++, if A [P] <B [Q], then P ++; if A [P] = B [Q], Q ++, P ++, Count ++, and [Count] = B [Q-1]; when one of P or Q reaches the end of A or B, the algorithm ends.

The following is a reference program:
//----------------------------------------

# Include <stdio. h>

Int A [100001], B [100001];
Int Ans [100001], Count, N, M;

Void Swap (int & a, int & B)
{
Int Temp =;
A = B;
B = Temp;
}

Void Sort (int L, int R, int A [])
{
Int p, q, Mid;
If (L = R) return;
Mid = A [(L + R)/2];
P = L-1; q = R + 1;
Do
{
P ++; q --;
While (A [p] <Mid) p ++;
While (A [q]> Mid) q --;
If (p <q) Swap (A [p], A [q]);
} While (p <q );
Sort (L, q, );
Sort (q + 1, R, );
}

Void Init ()
{
Int p, q;
P = q = 1;
For (int I = 2; I <= N; I ++)
{
If (A [I]! = A [p])
{
P ++;
A [p] = A [I];
}
}
For (int I = 2; I <= M; I ++)
{
If (A [I]! = B [q])
{
Q ++;
B [q] = A [I];
}
}

}

Int main (void)
{
Int p, q;
Scanf ("% d", & N, & M); // enter the number of elements in the Two Sets
For (int I = 1; I <= N; I ++)
Scanf ("% d", & A [I]); // read A set
For (int I = 1; I <= M; I ++)
Scanf ("% d", & B [I]); // read the B set
Sort (1, N, A); // Sort A set
Sort (1, M, B); // Sort the Set B
Init (); // removes the same elements from the same set
P = q = 1;
Count = 0;
While (p <= N & q <= M) // solves the problem.
{
If (A [p] <B [q])
{
P ++;
Continue;
}
If (A [p]> B [q])
{
Q ++;
Continue;
}
If (A [p] = B [q])
{
P ++; q ++;
Count ++;
Ans [Count] = B [q-1];
Continu ...... remaining full text>

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.