Data Structure practice project-linked list and data structure practice project

Source: Internet
Author: User

Data Structure practice project-linked list and data structure practice project

This group of projects targets section 8-15 of the Basic Data Structure series (2): Linear tables
8. Chain storage of linear tables
9. Create a single-chain table
10. Implementation of basic Single-chain table operations
11. Examples of single-chain table applications
12. double-stranded table
13. Circular linked list
14. Linear table Application
15. ordered table

[Project 1-create a single-chain table]
Defines the storage structure of a single-chain table, creates a single-chain table by using the header plug-in and tail plug-in, and displays the created results.
Please carry out the work on the basis of the following code:

# Include <stdio. h> # include <malloc. h> typedef int ElemType; typedef struct LNode // defines the node type {ElemType data; struct LNode * next; // points to the next node} LinkList; void CreateListF (LinkList * & L, ElemType a [], int n); // create a single-chain table void CreateListR (LinkList * & L, ElemType a [], int n); // create a single-chain table void DestroyList (LinkList * & L); // destroy the single-chain table void DispList (LinkList * L) // output single-chain table int main () {LinkList * L1, * L2; ElemType a [8] = {7, 9, 8, 2, 0, 4, 6, 3}; CreateListF (L1, a, 8); printf ("result of table creation by header insertion:"); DispList (L1); CreateListR (L2, a, 6 ); printf ("result of table creation by the end plug method:"); DispList (L2); DestroyList (L1); DestroyList (L2); return 0 ;} // write the code of the custom function (implementing the relevant algorithm) below

[Reference]

[Project 2-create a single-chain table algorithm library]
Follow the recommended method in "0207 change the algorithm program" to build your own professional infrastructure algorithm library. This week, we created an algorithm library for a single-chain table.
The algorithm library contains two files:
Header file: linklist. h, which contains code for defining the sequence table data structure, macro definition, and Declaration of functions to implement algorithms;
Source File: linklist. cpp, including the definition of functions that implement various algorithms
  
Use the multi-file organization form of the program to create the above two files, create another source file (such as main. cpp), compile the main function, and complete relevant testing.
The idea of "Gradual" can be used for testing. The number of functions involved each time should be as small as possible.
For example, to design a test function, you can initialize a linear table, destroy a linear table, output a linear table, and insert a function corresponding to a data element.

#include "linklist.h"int main(){    LinkList *L;    InitList(L);    ListInsert(L, 1, 15);    ListInsert(L, 1, 10);    ListInsert(L, 1, 5);    ListInsert(L, 1, 20);    DispList(L);    DestroyList(L);    return 0;}

[Reference]

Project 3-single-chain table Application]
When completing the following applications, in addition to the special requirements given in the project, other work can be supported by the algorithm completed in Project 2.
1. design an algorithm to set the data domains of a leading node to a1, a2 ,..., All nodes in an (n ≥ 3) single-chain table are reversed, that is, the data domain of the first node is changed to ,..., The data domain of the last node is a1. Implement this algorithm and complete the test.

Tip: when implementing an algorithm, you can design the following function: void Reverse (LinkList * & L)

2. It is known that L1 and L2 point to the header nodes of two single-chain tables, and their lengths are m and n respectively. design an algorithm to connect L2 to the back of L1. Implement the algorithm, complete the test, and analyze the complexity of the algorithm.

Tip: when implementing an algorithm, you can design the following function: void Link (LinkList& L1, LinkList& L2)

3. Design an algorithm to determine whether the L of a single-chain table is incremental. Implement this algorithm and complete the test.

[Reference]

[Project 4-create a double-stranded Table Algorithm Library (selected )]
The algorithm library contains two files:
Header file: dlinklist. h, which contains code for defining the sequence table data structure, macro definition, and Declaration of functions to implement algorithms;
Source File: dlinklist. cpp, including the definition of functions that implement various algorithms
Use the multi-file organization form of the program to create the above two files, create another source file (such as main. cpp), compile the main function, and complete relevant testing.
[Reference]

Project 5-Monkey King election]
A group of monkeys numbered 1, 2, 3... M, this group of monkeys (m) sat around in 1-m order. The number starts from 1st and the number of monkeys to the nth is about to leave the circle. In this way, the monkey is the king until only the last monkey is left in the circle. Input m and n, and the number of monkeys that output as the king is.

Tip:
(1) linked list solution: a Circular Single-chain table can be used to represent this group of monkeys. There are two members in the structure of the node: one for saving the Monkey number, one for pointing to the next person, and the other for the node numbered m pointing to the node numbered 1, to form a ring chain. When the number is n, the node is deleted and continues until there is only one node.
(2) Use a structure array to represent the loop chain: Set a member in the struct to indicate whether the corresponding monkey has been eliminated. From the number of records not eliminated by the first person, the number of records in the structure is changed to 0, indicating that the monkey has been eliminated. When the number of m elements in the array is reached, the number starts from the first number again, so that the cyclic count continues until the number of m elements is eliminated.
(3) This is a classic problem in Computer Science. many practical problems can be abstracted to this model. If you are interested, search for "Joseph's question ".
[Reference]

Project 5-circular double-stranded table Application]
The non-empty linear table ha and hb are represented by the cyclic double-chain table of the lead node. Design an algorithm Insert (ha, hb, I ). When I = 0, the hb of the linear table is inserted to the beginning of the ha of the linear table. When I> 0, insert the hb of the linear table to the end of the I node of the linear table ha. When I is greater than or equal to the ha length of the linear table, insert the hb of the linear table to the end of the linear table ha.
When implementing the algorithm, except for the special requirements given in the project, other work can be supported by the algorithm completed in Project 4.
[Reference]

Project 6-polynomial summation]
A single-chain table is used to store a single-dimensional polynomial and add two polynomials.

Tip:
1. Data Structure for storing Polynomials
The general formula of polynomial is Pn (x) = anxn + an −1xn −1 +... + a1x + a0 . There are n + 1 polynomial in total. Intuitively, you can define an array to store the n + 1 coefficients. Take Polynomials P (x) = −3.4x10 − 9.6x8 + 7.2x2 + x For example, an array storing this polynomial is as follows:

We can see that this solution is suitable for processing certain polynomials. However, there is a waste of space when processing Polynomials with a high number of times but few items, and there will be a lot of idle zeros.
You can use the single-chain table structure defined below to store polynomials: each node in the linked list is one of the polynomials. The data field of the knots includes two parts: the index and the coefficient, and each item in the polynomial is connected by the pointer field.

Typedef struct pnode // defines the node type of a single-chain table, stores one of the polynomials, and the linked list forms a polynomial {
Double coef; // coefficient, floating point number
Int exp; // exponential, positive integer *
Struct pnode * next; // pointer to the next item
} PolyNode;

Shows the list used to represent polynomials. When a polynomial linked list is created, nodes are arranged in an order from large to small in an exponential order.

2. Implementation of Polynomial addition in the linked list Storage Structure
Under the linked list storage structure, the implementation of Polynomial addition is based on the single-chain table storage structure defined above, and the algorithm for realizing polynomial addition is discussed.
The rule for adding two polynomials is to add the coefficients to the items with the same index. Let's set the head pointers of the linked list of two polynomials to be added as head1 (first polynomial) and head2 (second polynomial) respectively, and save the sum of the two to the linked list head1. You only need to first use the first node of the head1 and head2 linked lists as the current node (pointing to p1 and p2 respectively) to start detection. In the process of the traversal chain table, the situation is as follows:
(1) If the exponent values of the current nodes in the two polynomials are the same, their coefficients are added, the results are saved to the p1 node, and the p2 node is deleted. If the coefficient after addition is not 0, p1 points to the next node of the first polynomial and prepares for subsequent work. Otherwise, if the coefficient is not saved as 0, the current p1 node is deleted.
(2) When the exponent values of the corresponding nodes in two polynomials are not equal, if p1 points to a node with a large index, p1 simply points to the next node; when p2 points to a large node, it is necessary to insert the p2 node before p1, and then p2 points back to the next node in the second polynomial to continue processing.
(3) The detection process ends until any of the linked lists. If p1 is not empty, the remainder of the first polynomial is already in the linked list and is not processed. If p2 is not empty, you only need to link p2 to the end of the first Polynomial After addition.
The above discussion assumes that the polynomial linked list has been sorted by exponent from large to small. Before addition, a variety of measures are taken to ensure this premise is true.

[Reference]

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.