Insert e to the proper position of the sequence table to maintain the order of the table.

Source: Internet
Author: User

001 # define true 1

002 # define false 0

003 # define OK 1

004 # define error 0

005 # define infeasible-1

006 # define overflow-2

007 # define list_init_size 100

008 # define listincrement 10

009 # include <stdio. h>

010 # include <malloc. h>

011 typedef int ElemType;

012 typedef int Status;

013

014 typedef struct

015 {

016 ElemType * elem; // The base address of the bucket. The array pointer elem indicates the base address of the linear table.

017 int length;

018 int listsize; // defines the storage capacity allocated to the sequence table;

019} SqList;

020

021 Status InitList (SqList * L)

022 {

023 (* L). elem = (ElemType *) malloc (list_init_size * sizeof (ElemType ));

024 printf ("sequence table base address: % d \ n", (* L). elem );

025 if (! (* L). elem) exit (overflow );

026 (* L). length = 0;

027 (* L). listsize = list_init_size;

028 return OK;

029}

030

031 Status DeleteK (SqList * L, int I, int k)

032 {

033 // This process deletes k elements starting with I from linear Table a of the sequential Storage Structure

034 int j;

035 if (I <1 | k <0 | I + k> (* L). length) return infeasible; // The parameter is invalid.

036 else

037 {

038 for (j = I + K-2; j <= (* L ). length-1 & I <= I + K-2; j ++, I ++) (* L ). elem [I-1] = (* L ). elem [j + 1];

039 (* L). length = (* L). length-k;

040 return OK;

041}

042

043}

044

045 Status insert_suitable_position (SqList * L, ElemType e)

046 {

047 // insert e to the proper position of the sequence table to maintain the order of the table;

048 int I, j;

049 for (I = 0; I <= (* L). length-1; I ++) // locate a position greater than e;

050 {

051 if (* L). elem [I]> e) break;

052}

053 if (I> (* L). length-1) // insert to the tail;

054 {

055 (* L). elem [(* L). length] = e;

056 (* L). length ++;

057 return OK;

058}

059 // insert to I-1 position;

060 for (j = (* L). length-1; j >=i-1; j --)

061 {

062 (* L). elem [j + 1] = (* L). elem [j];

063}

064 (* L). length ++;

065 return OK;

066}

067

068 Status insert_suitable_position_better (SqList * L, ElemType e)

069 {

070 // insert e to the proper position of the sequence table to maintain the order of the table;

071 int j;

072 for (j = (* L). length; j> 0, e <(* L). elem [J-1]; j --)

073 {

074 (* L). elem [j] = (* L). elem [J-1];

075}

076 (* L). elem [j] = e;

077 (* L). length ++;

078 return OK;

079}

080

081 void main ()

082 {

083

084 SqList L;

085 int I, j, e, n, m, k;

086 InitList (& L );

087 L. length = 10; // set the length to 10 to facilitate subsequent tests;

088 for (I = 0; I <L. length; I ++)

089 {

090 L. elem [I] = I;

091}

092 for (j = 0; j <L. length; j ++)

093 {

094 printf ("% d \ t", L. elem [j]);

095}

096 printf ("\ n inserted number? \ N ");

097 scanf ("% d", & e );

098 insert_suitable_position_better (& L, e );

099 for (j = 0; j <L. length; j ++)

100 {

101 printf ("% d \ t", L. elem [j]);

102}

 


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.