Fourth time job 6-2 insertion of sequential order table __ Data structure

Source: Internet
Author: User
6-2 insertion of ordered order table (10 points)

This requires the orderly insertion function of an ascending sequential table to be implemented. L is an ascending sequential order table, where the function status listinsert_sortedsq (SqList &l, Elemtype e) is used to insert a data in an ascending order in the order table. For example: The original data is: 2 5, to insert an element of 3, then insert the following sequence table is 2 3 5. To consider the issue of expansion. function Interface Definition:

Status listinsert_sortedsq (sqlist &l, elemtype e);
Sample Referee Test procedure:
The library function header file contains #include <stdio.h> #include <malloc.h> #include <stdlib.h>//Function status Code definition #define TRUE 1 #def INE FALSE 0 #define OK 1 #define ERROR 0 #define INFEASIBLE-1 #define OVERFLOW-2 typedef int ST

ATUs;  The storage structure of the sequential table is defined #define LIST_INIT_SIZE #define Listincrement elemtype;   It is assumed that the elements in the linear table are integral type typedef struct{elemtype* elem;       Storage space base address int length;     The number of elements in the table int listsize;    Table capacity size}sqlist;

Sequential table type definition//function declaration Status LISTINSERT_SORTEDSQ (sqlist &l, elemtype e); Sequential table initialization function Status initlist_sq (sqlist &l) {//open up a space L.elem = (elemtype*) malloc (list_init_size * sizeof Elemtyp
    e)); Detection Open whether success if (!
    L.elem) {exit (OVERFLOW);
    }//Assignment l.length = 0;

    L.listsize = list_init_size;
return OK; }//sequential table output function void listprint_sq (SqList L) {elemtype *p = l.elem;//traversal element with pointers for (int i = 0; i < l.length; + +i) {if (i = = l.length-1) {printf ("%d", * (P+i));
        else{printf ("%d", * (P+i));
    int main () {//Declare a sequential table sqlist L;

    Initialization Order table INITLIST_SQ (L);
    int number = 0;

     Elemtype e; scanf ("%d", &number);//insert number of data for (int i = 0; i < number; ++i) {scanf ("%d", &e);//Enter Data L
    ISTINSERT_SORTEDSQ (L, E);

    } listprint_sq (L);
return 0;
 /* Please fill in the answer here/*

Input format: The first line enters the number of numbers that you want to insert next. Enter a digital output format: A sample of digital input after the output is inserted:

5
2 3 9 8 4
Output Sample:
2 3 4 8 9
Function:
&NBSP;STATUS&NBSP;LISTINSERT_SORTEDSQ (sqlist &l, elemtype e) {     elemtype *newbase;       if (l.length >= l.listsize) {         newbase =  (elemtype *) realloc (L.elem, (l.listsize+listincrement) *sizeof ( Elemtype));         if (!newbase) exit (OVERFLOW);         L.elem = newbase;         L.listsize += LISTINCREMENT;       }       int number,i;       for (i = 0;i < l.length&&* (L.elem+i)   < e;i++)       {}       number = i ;       int k ;       foR (int j = number,k= 0 ; j<l.length ;j++ ,k++)        {          * (L.elem + L.length-k  )  = * (l.elem + l.length-k-1 ); &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP}         * (L.elem +  number )  = e;         L.length ++; }

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.