Implementation of the sequence table function

Source: Internet
Author: User

Sequence Table creation, insertion, deletion, clearing, destruction, search, and output

# Include <stdio. h>

# Include <malloc. h>

# Include <stdlib. h>

 

# Definetrue 1

# Definefalse 0

# Defineok 1

# Defineerror 0

# Defineinfeasible-1

# Defineoverflow-2

 

Typedefint status;

 

Typedefint elemtype;

 

// The sequential storage structure for Dynamic Allocation of linear tables

 

# Definelist_init_size 100 // initial allocation of the linear table storage space

# Definelistintcrement 10 // increment of storage space allocation in a linear table

Typedefstruct

{

Elemtype * ELEM; // The base address of the bucket.

Int length; // The current length.

Int listsize; // The storage capacity currently allocated

} Sqlist;

 

// Initialize the sequence table

Statusinitlist_sq (sqlist & L)

{

L. ELEM = (elemtype *) malloc (list_init_size * sizeof (elemtype ));

If (! L. ELEM)

Exit (overflow); // failed to allocate the bucket

L. Length = 0; // The length of the empty table is 0.

L. listsize = list_init_size; // initial storage capacity

Return OK;

}

 

Voidcreatelist (sqlist & L) // create an ordered table

{

Int I;

Printf ("Enter the number of elements in the sequence table you want to create ");

Scanf ("% d", & L. Length );

Printf ("Enter the sequence table you want to create: \ n ");

For (I = 0; I <L. length; I ++)

Scanf ("% d", & L. ELEM [I]);

}

 

 

// Search

Statuslocation (sqlist & L, elemtype e) // locate the element

{

Int I = 0;

While (L. ELEM [I]! = E & I <L. length)

I ++;

If (I> L. Length) Return-1;

Else return I + 1; // because the C language starts from subscript 0, when I = 0, it is actually the I + 1 element of the sequence table.

}

 

 

// Insert a new element e at position I in the sequence table

Statuslistinsert_sq (sqlist & L, int I, elemtype E)

{

Elemtype * newbase, * q, * P;

If (I <1 | I> L. Length + 1)

Return Error; // I is invalid

If (L. length> = L. listsize)

{

// The current bucket is full and the allocation is increased

Newbase = (elemtype *) realloc (L. ELEM, (L. listsize + listintcrement) * sizeof (elemtype ));

If (! Newbase)

Exit (overflow); // An error occurred while allocating the address.

L. ELEM = newbase; // new base address

L. listsize + = listintcrement; // increase the storage capacity

}

Q = & (L. ELEM [I-1]); // Q is the insert position

For (P = & (L. ELEM [L. Length-1]); P> = Q; -- p)

* (P + 1) = * P; // right shift of the element after the bit is inserted

* Q = E; // insert E

+ + L. length; // The table length plus 1

Return OK;

}

 

// Delete the I-th element in the sequence table and return its value with E.

Statuslistdelete_sq (sqlist & L, int I, elemtype & E)

{

Elemtype * q, * P;

If (I <1 | I> L. Length + 1)

Return Error; // I is invalid

P = & (L. ELEM [I-1]); // P is the location of the deleted Element

E = * P; // The value of the deleted element is assigned to E.

Q = L. ELEM + L. Length-1; // position of the end element of the table

For (++ P; P <= Q; ++ P)

* (P-1) = * P; // move the element left after the deleted Element

-- L. length; // The table length minus 1.

Return OK;

}

 

// Determine whether the table is empty

Statuslistempty (sqlist & L)

{// If l is empty, true is returned; otherwise, false is returned.

If (L. Length = 0)

{

Printf ("This table is empty table \ n ");

Return true;

}

Else return false;

}

 

// Leave the table empty

Void clearlist (sqlist & L) // reset L to an empty table

{

For (INT I = 0; I <L. length; I ++)

{

L. ELEM [I] = NULL;

}

Printf ("the table has been cleared \ n ");

}

// Destroy the table

Voidburning_list (sqlist & L)

{

L. Length = 0;

// Free (L );

Printf ("the table has been destroyed \ n ");

}

 

Void print (sqlist & L) // output sequence table

{

Int I;

For (I = 0; I <L. length; I ++)

Printf ("% 3d", L. ELEM [I]);

Printf ("\ n ");

}

 

Voidmain ()

{

Sqlist la;

Int I, J;

Elemtype E;

Initlist_sq (LA );

Createlist (LA); // create a table

// Search

Printf ("Enter the element to be searched: \ n ");

Scanf ("% d", & E );

J = location (La, e );

Printf ("the position of this element is % d \ n", J );

// Insert

Printf ("Enter the position and element to insert: \ n ");

Scanf ("% d", & I, & E );

Listinsert_sq (La, I, e );

Printf ("output sequence table after insertion: \ n ");

Print (LA );

// Delete

Printf ("Enter the location to delete: \ n ");

Scanf ("% d", & I );

Listdelete_sq (La, I, e );

Printf ("the deleted element is: % d \ n", e );

Printf ("output sequence table after deletion: \ n ");

Print (LA );

// Clear

Clearlist (LA );

Print (LA );

// Destroy

Burning_list (LA );

Print (LA );

}


Implementation of the sequence table function

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.