Data structure routine-linear table sequential storage application, data structure routine

Source: Internet
Author: User

Data structure routine-linear table sequential storage application, data structure routine

This article is a series of basic network courses on data structures (2): Examples in the Application of Linear table sequence storage in linear tables in 6th class hours.

Example: delete an element

Problem: Linear Table A with A known length of n adopts A sequential storage structure and design algorithms to delete all data elements with A value of x in A linear table.
Requirements: algorithms with time complexity O (n) and space complexity O (1)

Solution 0: Implemented using basic operations, not meeting complexity requirements
(Note: For the list. h and list. cpp required in this article, click here for reference ...)

# Include "list. h "# include <stdio. h> void delnode1 (SqList * & L, ElemType x) {int I; ElemType e; while (I = LocateElem (L, x)> 0) {ListDelete (L, i, e) ;}// use main to write the test code int main () {SqList * sq; ElemType a [10] = {5, 8, 7, 0, 2, 4, 9, 6, 7,3}; CreateList (sq, a, 10); printf ("before deletion"); DispList (sq); delnode1 (sq, 7); printf ("after deletion "); dispList (sq); return 0 ;}

Solution 1: Copy the elements to be retained

# Include "list. h "# include <stdio. h> void delnode2 (SqList * & L, ElemType x) {int k = 0, I; // k records the number of non-x elements for (I = 0; I <L-> length; I ++) if (L-> data [I]! = X) {L-> data [k] = L-> data [I]; k ++;} L-> length = k ;} // use main to write the test code int main () {SqList * sq; ElemType a [10] = {5, 8, 7, 0, 2, 4, 9, 6, 7, 3}; CreateList (sq,, 10); printf ("before deletion"); DispList (sq); delnode2 (sq, 7); printf ("after deletion"); DispList (sq); return 0 ;}
For example, separating elements

Problem: a sequence table has 10 elements whose element type is integer. Design an algorithm, with the first element as the dividing line, move all the elements smaller than it to the front of the element, move all the elements greater than it to the back of the element

Solution 1:

# Include "list. h "# include <stdio. h> void move1 (SqList * & L) {int I = 0, j = L-> length-1; ElemType struct = L-> data [0]; // use data [0] as the benchmark ElemType tmp; while (I <j) // alternate scanning from the two ends of the interval to the center, until I = j {while (I <j & L-> data [j]> blocks) j --; // scan right to left, find 1st while (I <j & L-> data [I] <= bytes) I ++; // scan from left to right, find 1st if (I <j) {tmp = L-> data [I] elements greater than limit; // L-> data [I] and L-> data [j] are exchanged L-> data [I] = L-> data [j]; l-> data [j] = tmp;} tmp = L-> data [0]; // L-> data [0] and L-> data [j] are exchanged. L-> data [0] = L-> data [j]; l-> data [j] = tmp;} // use main to write the test code int main () {SqList * sq; ElemType a [10] = {5, 8, 7, 0, 2, 4, 9,6, 7,3}; CreateList (sq, a, 10); printf ("before moving"); DispList (sq); move1 (sq); printf ("after moving "); dispList (sq); return 0 ;}

Solution 2:

# Include "list. h "# include <stdio. h> void move2 (SqList * & L) {int I = 0, j = L-> length-1; ElemType struct = L-> data [0]; // use data [0] as the benchmark while (I <j) // alternate scanning from the two ends of the sequence table to the center, until I = j {while (j> I & L-> data [j]> bytes) j --; // scan right to left, find a data [j] L-> data [I] = L-> data [j]; // find such data [j], place data [I] At I ++; while (I <j & L-> data [I] <= bytes) I ++; // scan from left to right, find a data [I] L-> data [j] = L-> data [I]; // find such data [I], place data [j] at j --;} L-> data [I] = cursor;} // use main to write the test code int main () {SqList * sq; elemType a [10] = {5, 8, 7, 0, 2, 4, 9, 6, 7, 3}; CreateList (sq, a, 10); printf ("before moving"); DispList (sq ); move2 (sq); printf ("move"); DispList (sq); return 0 ;}

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.