DS sequence tables implement output in disordered input sequence, ds

Source: Internet
Author: User

DS sequence tables implement output in disordered input sequence, ds

There are many instances of sequence tables. When learning other programming languages, you must have learned to input a series of numbers in disorder, which requires sorting to achieve ascending or descending output. Today, we will use an ordered table to implement out-of-order Input and Output (in ascending order ).

The basic operations required to implement the above functions include preparation before 0 basic operations, 1 initialization sequence table, and 6 insertion of data elements into sequence table.

You only need to write a sort function. The code of the sort function is:

<Span style = "font-size: 18px;"> // sorting function void paixu (SqList & L) {for (int I = 0; I <L. length; I ++) {for (int j = 0; j <L. length; j ++) {if (L. elem [j]> L. elem [I]) {int temp; temp = L. elem [I]; L. elem [I] = L. elem [j]; L. elem [j] = temp ;}}}</span>

Then, you only need to define an ordered table in the main function, input some data elements in disorder, call the sorting function, and traverse all the data elements in the sorted ordered table. The entire code is:

<Span style = "font-size: 18px;" >#include <iostream> using namespace std; # include <malloc. h> # include <stdlib. h> # define TRUE 1 # define FALSE 0 # define OK 1 # define ERROR 0 # define OVERFLOW-2 # define LIST_INIT_SIZE 100 # define LISTINCREMENT 10 typedef int ElemType; typedef int Status; typedef struct {ElemType * elem; // The base address of the bucket int length; // The current length int listsize; // currently allocated storage capacity} SqList; // defines a struct type, and name it Sqlist // 1. initialize the sequence table Status In. ItList (SqList & L) {L. elem = (ElemType *) malloc (LIST_INIT_SIZE * sizeof (ElemType); if (! L. elem) {exit (OVERFLOW);} L. length = 0; // The length is 0L. listsize = LIST_INIT_SIZE; return OK;} // 6 insert the data element Status ListInsert (SqList & L, int I, ElemType e) to the sequence table) {if (I <1 | I> L. length + 1) {return ERROR;} if (L. length> = L. listsize) {ElemType * newbase = (ElemType *) realloc (L. elem, (L. listsize + LISTINCREMENT) * sizeof (ElemType); if (! Newbase) {exit (OVERFLOW);} L. elem = newbase; L. listsize + = LISTINCREMENT;} ElemType * q = & (L. elem [I-1]); ElemType * p; for (p = & (L. elem [L. length-1]); p> = q; -- p) {* (p + 1) = * p;} * q = e; ++ L. length; return OK;} // The sorting function void paixu (SqList & L) {for (int I = 0; I <L. length; I ++) {for (int j = 0; j <L. length; j ++) {if (L. elem [j]> L. elem [I]) {int temp; temp = L. elem [I]; L. elem [I] = L. elem [j]; L. elem [j] = temp ;}}} int main () {SqList La; InitList (La); cout <"Enter the number of data in La :"; int na; ElemType e; cin> na; for (int I = 1; I <= na; I ++) {cin> e; ListInsert (La, I, e);} paixu (La); cout <"the data element of the sorted La is:" <endl; for (I = 0; I <na; I ++) {cout <La. elem [I] <"," ;}cout <endl; return 0 ;}</span>

The number of elements in the input sequence table is 10.

The data elements entered in disordered order are: 3 2 7 9 0 1 4 8 6 5

The output result is:

 

 

 


 

Related Article

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.