Data structure is one of the core courses of computer science, and object-oriented methods have become the mainstream models of system development and program design, c ++ is one of the most widely used object-oriented programming languages.
Knowledge Point
1. Concept of sequence table
An ordered table is the sequential storage structure of a linear table. It is stored in a sequential storage structure.
Note: sequence table A of n elements can be expressed as a [1 .. n] (suitable for Pascal). Its subscript ranges from 1 to n. A [1] is called 1st elements, and a [2] is called 2nd elements ,......, A [n] is called the nth element. It can also be expressed as a [0... n-1] (suitable for C/C ++), where 0 to n-1 subscripts, a [0] is called 1st elements, and a [1] is called 2nd elements ,......, A [n-1] is called the nth element.
The latter is used in this tutorial.
2. Storage Structure of the sequence table
The sequence table node type is defined as follows:
# Define Max 50 // maximum number of elements in a sequence table
Typedef int elemtype; // defines elemtype as int
Typedef elemtype sqlist [Max];
In an ordered table, the storage address of each node AI is a linear function of the node's position in the table I, as long as you know the base address and the size of each node, you can find the storage address of any node within the same time. Therefore, a sequence table is a random storage structure.
Assume that each node in the table occupies C storage units and the storage address (Basic address for short) of the starting node A1 is LOC (AI ), the storage address LOC (AI) of node AI can be calculated as follows:
LOC (AI) = LOC (A1) + (I-1) * C (1 ≤ I ≤ n)
3. Sequence Table operations
● Create (sqlist A): Create sequence table A and return the number of elements.
● Disp (sqlist A, int N): output an ordered table A with n elements.
● Ins (sqlist A, int I, elemtype X): insert an element x before the I element of Sequence Table. If I = 0, the new element is used as 1st elements. If I = N, It is inserted at the end of the sequence table.
● Del (sqlist A, int N, int I): Delete the I-th element in sequence table.
● Find (sqlist A, int N, elemtype X): searches for the elements whose element value is X in an ordered table A with n elements.
Question: merge two linked lists
(1) create two linked lists A and B. The number of linked list elements is M and N, respectively.
(2) assume that the elements are (x1, x2 ,......, XM), and (Y1, Y2 ,......, YN ). Merge them into a linear Table C so that:
When M> = N, C = x1, Y1, X2, Y2 ,......, XN, YN ,......, XM
When N> = m, c = Y1, X1, Y2, X2 ,......, Ym, XM ,......, YN
Output Linear Table C
(3) Sort C in ascending order by direct insert sort, generate linked list D, and output Linked List D
Test data:
(1) Table)
Table B (, 55)
(2) Table A (, 34)
Table B (, 12)
Report requirements:
1. solution design
2. program diagram
3. program code
4. program debugging process and result
5. Summary
Analysis of Typical Examples
[Example 1] design the basic algorithm of the sequence table.
[Solution] a sequence table is a linear table that uses a continuous region to store data. The sq. cpp file that implements this question function is as follows:
Program code # include "iostrea. H"
# Define Max 50 // maximum number of elements in a sequence table
Typedef int elemtype;
Typedef elemtype sqlist [Max];
Int create (sqlist A) // create an ordered table
{
Int I, N;
Cout <"Create an ordered table" <Endl;
Cout <"Number of input elements :";
Cin> N;
For (I = 0; I <n; I ++)
{
Cout <"Enter the" <I + 1 <"element value ";
Cin> A [I];
}
Return N;
}
Void disp (sqlist A, int N) // output an ordered table
{
Int I;
Cout <"output an ordered table" <Endl;
If (n = 0)
Cout <"empty table ";
For (I = 0; I <n; I ++)
Cout <A [I] <"";
Cout <Endl;
}
Int ins (sqlist A, int N, int I, elemtype X)
// Insert an element x before the I element of the sequence table
// If I = 0, the new element is used as the first element. If I = N, the new element is inserted at the end.
{
Int J;
If (I <0 | I> N)
Cout <"I value overflow or overflow" <Endl;
Else
{
For (j = n-1; j> = I; j --)
{
A [J + 1] = A [J]; // move the I-th element behind it
A [I] = x; n ++; // The length of the sequence table increases by 1.
}
Return N;
}
Int del (sqlist A, int N, int I) // Delete the I-th element in Sequence Table
{
Int J;
If (I <0 | I> N) cout <"I value overflow or overflow" <Endl;
Else
{
For (j = I-1; j <n; j ++)
A [J] = A [J + 1]; // move the element after element I to overwrite a [I]
N --; // The length of the sequence table minus 1
}
Return N;
}
Int find (sqlist A, int N, elemtyep X)
// Find the element whose element value is X in the table
{
Int I = 0;
While (I <= N & A [I]! = X)
I ++;
If (I <n) return 1;
Else reutn 0 ;}
[Example 2] known array a [0 .. the element type of n-1] is an integer. The design algorithm adjusts a so that all elements on the left are less than 0, and all elements on the right are greater than or equal to 0.
[Solution] The Program for implementing this function is as follows: program code # include "sq. cpp"
Void adjust (sqlist A, int N)
{
Sqlist B;
Int I, x = 0; y = n-1;
For (I = 0; I <n; I ++)
{
If (A [I] <0)
{
B [x] = A [I]; X ++;
}
Else
{
B [y] = A [I]; y --;
}
For (i-0; I <n; I ++)
A [I] = B [I];
}
Void main ()
{
Sqlist;
Int N;
N = create ();
Disp (A, N );
Adjust (A, N );
Disp (A, N );
}
[Example 3] compile an algorithm to merge two ordered (from small to large) ordered tables into one ordered table. The merged results are placed in the first ordered table, there is no new sequence table Storage (assuming the two sequence tables do not have the same elements ).
[Solution] the merging process of two sequence tables A and B is to compare the last elements in A and B one by one, and locate the larger elements in a first. Program code # include "sq. cpp"
Int comb (sqlist A, int Na, sqlist B, int Nb)
{
Int n = Na, M = Nb;
If (Na + NB> MAX)
Return 0; // sequential table Overflow
While (NB> 0)
If (NA = 0) | (A [NA-1] <B [nb-1])
{
A [Na + nb-1] = B [nb-1]; // description a [nb-1] is the largest element of Na + Nb
NB --;
}
Else
{
A [Na + nb-1] = A [NA-1]; // description a [NA-1] is the largest element of Na + Nb
Na --;
}
NA = N + m;
Return NA;
}
Void main ()
{
Sqlist A, B;
Int Na, NB;
NA = create ();
NB = create (B );
Disp (A, Na );
Disp (B, Nb );
NA = comb (A, Na, B, Nb );
Disp (A, Na );
}
[Example 4] An ordered table A contains n elements and requires an algorithm to reverse the table, only one additional unit of work can be added to the bucket of the original table.
[Solution] For a with n elements, set K to n/2 (that is, an integer after N Division 2 ). Exchange a [0] with a [n-1], a [1] with a [N-2 ,......, If a [k] exchanges with a [n-k-1], all elements are reversed. The program that implements the function is as follows (only one additional work unit temp is used): program code # include "sq. cpp"
Void inver (sqlist A, INT) // reverse
{
Int M = n/2, I; // m is half the length
Elemtype temp;
For (I = 0; I <n; I ++) // exchange a [I] with a [n-i-1]
{
Temp = A [I]; A [I] = A [n-i-1]; A [n-i-1] = temp;
}
}
Void main ()
{
Sqlist;
Int N;
N = create ();
Disp (A, N );
Invert (A, N );
Disp (A, N );
}
[Example 5] assume that N linear tables are stored in the sequence table s [1... in M], place f [I] and R [I] to point to the position of the first 1st elements and the last 1 element in S, set R [I] <F [I + 1]. F [n + 1] = m + 1. Write the implementation algorithm as required
(1) Insert an element after entry J in Table I. The insert operation is not allowed only when the entire [1. m] space is full.
(2) Delete the J-th element in the I-th table. After the J-th element is deleted, the table is still in the sequential storage structure.
[Solution] In essence, this question is to put n linear tables (the length may be different) in a continuous space (the length is m) to solve the insertion and deletion of these linear tables.
(1) algorithm: program code void ins (I, j, X)
{
Int P, K;
If (R [N] = m)
Cout <"overflow" <Endl;
Else
{
For (P = N; P> = I + 1; p --) // move an element behind a linear table from I + 1 to n.
{
For (k = R [p]; k> = f [p]; k --) // remove an element after the P linear table
{
S [k + 1] = s [k];
R [p] ++;
F [p] ++; // adds 1 to the position of the first and end elements of the P linear table.
}
For (P = R [I]; P> = J + 1; p --) // move the elements from position J of the I-th linear table
S [p + 1] = s [p];
S [p] = X;
R [p] ++;
}
}
(2) The algorithm is as follows:
Void del (I, j)
{
For (P = f [I] + J; P <= m; P ++) // move the element forward to overwrite the element to be deleted
S [p] = s [p + 1];
For (P = I; P <= N; P ++) // subtract 1 from the R [I] value of the I and subsequent linear tables.
R [p] --;
For (P = I + 1; P <= N; P ++) // The F [I] value of the linear table after and after I + 1 minus 1
F [p] --;
}
[Example 6] Set A = (A1, A2,... Am) and B + (B1, B2,... bn) as sequence tables. If n = m and AI = Bi (I = 1, 2 ,.. n), it is called a = B; If AI = Bi (I = 1, 2 ,... j) and a (J + 1) <B (J + 1) is called a <B. In other cases, it is A> B. write an algorithm. When a <B, A = B, or A> B, output-1, 0, or 1.
[Solution] the idea of the algorithm comp () is to first find the nodes with the same values in the front of a and B. I and j are the Subscripts for comparing different elements in A and B, respectively, then, compare the results of a and B Based on I and j. The program to implement this function is as follows: the program code # include "sq. cpp"
Int comp (sqlist A, int Na, sqlist B, int Nb)
{
Int I = 0, j = 0;
While (I <Na & J <NB & A [I ++] = B [J ++]); // compare the same part
I --; j --;
If (I = Na & J = Nb) return 0;
If (I = Na & J! = Nb) Return-1;
If (I! = Na & J = Nb) return 1;
If (A [I]> B [J])
Return 1;
Else return-1;
}
Void main ()
{
Sqlist;
Int Na, Nb, N;
NA = create (NA );
NB = create (NB );
N = comp (A, Na, B, Nb );
Switch (N)
{
Case 0: cout <"A = B" <Endl; break;
Case 1: cout <"A> B" <Endl; break;
Case 3: cout <"A <B" <Endl; break;
}
}
After reading so many examples, I should have a few exercises.
[Question 1] Set "A" as an ordered table, and write an algorithm to delete k elements starting with "I" in ".
[Question 2] compile an algorithm to split a Sequence Table A (with n elements) into two sequence tables, so that the elements larger than 0 in a are stored in B, elements smaller than 0 are stored in C.
[Question 3] It is known that the elements in an ordered table A are in a non-descending order by value. Write a function and insert an element x to keep the ordered table.
[Question 4] compile an algorithm to merge M (M> 2) ordered (from small to large) ordered tables into an ordered table. No new sequence table storage is set during the merge process.
Question 5: Set the integer represented by a machine to no more than 5 decimal digits. Design a data structure that represents any long integer, and use the data structure you designed to write an algorithm that calculates the sum of any given two integers.
[Question 1] Set "A" as an ordered table, and write an algorithm to delete k elements starting with "I" in ".
The program to implement this function is as follows: the program code # include "sq. cpp"
Int Delk (sqlist A, int * n, int I, int K)
{
Int J;
If (I <= 0 | I + k-1> * n)
{
Cout <"I, k parameter incorrect" <Endl;
Return 0;
}
Else
{
For (j = I + k-1; j <* n; j ++)
A [J-K] = A [J];
(* N)-= K;
Return 1;
}
}
Void main ()
{
Sqlist;
Int N, I, K;
N = create ();
Disp (A, N );
Cout <"input I, K :";
Cin> I> K;
If (Delk (A, & N, I, K) = 1)
Disp (A, N );
}
[Question 2] compile an algorithm to split a Sequence Table A (with n elements) into two sequence tables, so that the elements larger than 0 in a are stored in B, elements smaller than 0 are stored in C.
[Solution] The algorithm idea of this question is: traverse the elements in a in sequence, compare the current element value, assign to B if it is greater than 0, and assign to C if it is less than 0. The program to implement this function is as follows: the program code # include "sq. cpp"
Void split (sqlist A, int Na, sqlist B, int * NB, sqlist C, int * NC)
{
Int I, j = 0, K = 0;
For (I = 0; I <Na; I ++ 0
{
If (A [I]> 0)
B [J ++] = A [I];
Else
C [k ++] = A [I];
(* NB) = J;
(* C) = K;
}
Void main ()
{
Sqlist A, B, C;
Int Na, Nb, NC;
NA = create ();
Disp (A, Na );
Split (A, Na, B, & nb, C, & NC );
Disp (B, Nb );
Disp (C, NC );
}
[Question 3] It is known that the elements in an ordered table A are in a non-descending order by value. Write a function and insert an element x to keep the ordered table.
[Solution] first find the appropriate position, then move the element to an empty position, and then insert X. The program to implement this function is as follows: the program code # include "sq. cpp"
Int insert (sqlist A, int N, elemtype X) // The length of the sequence table is N.
{
Int I, J;
If (x> = A [n-1])
A [n] = x; // If X is greater than the last element, it is used as the last element.
Else
{
I = 0;
While (x> A [I]) I ++; // query the insert position
For (j = N; j> = I; j --) A [J + 1] = A [J]; // remove the position where X is inserted.
A [I] = X;
}
Return (n + 1); // The length of the sequence table is increased by 1.
}
Void main ()
{
Sqlist;
Int N;
N = create (N );
Disp (A, N );
N = insert (A, N, 10); // insert element 10
Disp (A, N );
}
[Question 4] compile an algorithm to merge M (M> 2) ordered (from small to large) ordered tables into an ordered table. No new sequence table storage is set during the merge process.
[Solution] the merging process of two sequence tables A and B is to compare them forward and backward from the last element of A and the 1st element of B, respectively, first, locate the large element in. The program to implement this function is as follows: the program code # include "sq. cpp"
Int comb (sqlist A, int Na, sqlist B, int Nb)
{
Int n = Na, m = 0;
While (M <Nb)
If (n = 0 | A [n-1] <B [m])
{
A [n + nb-m-1] = B [m]; // B [m] is the element of N + Nb-m
M ++;
}
Else
{
A [A [n + nb-m-1] = A [n-1]; // it indicates that a [n-1] is an element of N + Nb-M.
N --;
}
Return Na + NB;
}
Void main ()
{
Sqlist A, B;
Int Na, NB;
NA = create ();
Disp (A, Na );
NB = create (B );
Disp (B, Nb );
NA = comb (A, Na, B, Nb );
Disp (A, Na );
}
Question 5: Set the integer represented by a machine to no more than 5 decimal digits. Design a data structure that represents any long integer, and use the data structure you designed to write an algorithm that calculates the sum of any given two integers.
[Solution] store the positive numbers entered by the user in an ordered table, and then add the numbers in the two ordered tables. The program to implement this function is as follows: the program code # include "sq. cpp"
Int input (sqlist)
{
Int I;
For (I = 0; I <Max; I ++)
A [I] = 0;
Cout <"users who enter a positive integer (ended with-1)" <Endl;
I = 0;
While (1)
{
Cin> A [I];
If (A [I] <0) break;
I ++;
}
Return I;
}
Void output (sqlist A, int low, int high)
// Output a [low] to a [High] In Sequence Table A
{
Int I; for (I = low; I Cout <A [I];
Cout <Endl;
}
Void move (sqlist A, int Na)
// Move the numbers in a to the back
{
Int I;
For (I = 0; I <Na; I ++)
A [max-i-1] = A [na-i-1];
}
Int add (sqlist A, int Na, sqlist B, Nb)
// Add a and B, and put the result in.
{
Int NC, I, j, length;
If (NA> NB) NC = Na;
Else
NC = Nb;
Move (A, Na );
Move (B, Nb );
For (I = max-I; I> = max-nc; I --)
{
J = A [I] + B [I];
If (j> 9)
{
A [I-1] = A [I-1] + 1;
A [I] = J-10;
}
Else
A [I] = J;
If (I = max-NC)
{
If (j> 9)
{
A [I-1] = 1;
Length = nC + 1;
}
Else
Length = NC;
}
}
Return length;
}
Void sqlist A, B;
Int Na, Nb, NC;
NA = input ();
NB = input (B );
Cout <"integer :";
Output (A, 0, Na );
Cout <"integer B :";
Output (B, 0, Nb );
NC = add (A, Na, B, Nb );
Cout <"addition :";
Output (A, Max-NC, max );
}