The DS sequence table reverts input data and ds sequence data.

Source: Internet
Author: User

The DS sequence table reverts input data and ds sequence data.

There are two very similar processes to achieve the inverse of input data and the ordering of order tables. Therefore, the basic operations of order tables are the same: preparations before the basic operations of 0, 1 initialization of order tables, 6. insert data elements into the sequence table.

To reverse the input data element, you also need a reverse function, which has already been used in C ++ and C # languages, so it is no stranger, I remember that I wrote a lot of questions about C ++ program code, but I can't write them for dozens of times. It's easy to master, that is, half of the number of input data, so the code of the reverse function is:

<Span style = "font-size: 18px;"> // The void nizhi (SqList & L) {for (int I = 0; I <L. length/2; I ++) {int temp; temp = L. elem [I]; L. elem [I] = L. elem [L. length-1-i]; L. elem [L. length-1-i] = temp ;}</span>

In the main function, you only need to declare a sequence table, enter the number and data of the data elements in the sequence table, and call the inverse function. The complete sequence table provides the following code to reverse the input data:

<Span style = "font-size: 18px;" >#include <iostream> using namespace std; # include <malloc. h> # include <stdlib. h> # define LIST_INIT_SIZE 100 # define LISTINCREMENT 10 # define OK 1 # define TRUE 1 # define FALSE 0 # define ERROR 0 # define OVERFLOW-2 typedef int ElemType; typedef int Status; typedef struct {ElemType * elem; int length; int listsize;} SqList; // defines a struct type, and name it Sqlist // 1. initialize the sequence table Status InitList (SqList & L) {L. el Em = (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;} // void nizhi (SqList & L) {for (int I = 0; I <L. length/2; I ++) {int temp; temp = L. elem [I]; L. elem [I] = L. elem [L. length-1-i]; L. elem [L. length-1-i] = temp;} int main () {SqList La; InitList (La); ElemType e; int na; cout <"Enter the number of data in La :"; cin> na; for (int I = 1; I <= na; I ++) {cin> e; ListInsert (La, I, e );} nizhi (La); // call the reverse function for (I = 0; I <La. length; I ++) {cout <La. elem [I] <"," ;}cout <endl; return 0 ;}</span>

Input data: the number of sequential tables is 10.

Input data element: 0 1 2 3 4 5 6 7 8 9

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: 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.