Using arrays to implement various operations on a linear table (C Language) is only part of the function.

Source: Internet
Author: User

 

To be continued: D

<Pre name = "code" class = "c">

/*

Implementation of linear structure Arrays

*/

# Include & lt; stdio. h & gt;

# Include & lt; malloc. h & gt; // contains the malloc Function

# Include & lt; stdlib. h & gt; // contains the exit function

 

// First define the struct type that describes the array information

Struct Arr

{

Int * pBase; // the pointer variable that stores the first address of the array.

Int len; // array Length

Int cnt; // number of elements in the array

};

 

// Define the function declaration for the basic operation of the array

Void init_arr (struct Arr * pArr, int length); // array Initialization

Bool append_arr (struct Arr * pArr, int val); // append Element

Bool insert_arr (struct Arr * pArr, int index, int val); // insert an element

Bool delete_arr (); // deletes an element.

Int get (); // get the element

Bool is_empty (struct Arr * pArr); // determines whether it is null.

Bool is_full (); // determines whether it is full.

Void sort_arr (); // sort

Void show_arr (struct Arr * pArr); // traverses the Array

Void inversion_arr (); // array Inversion

 

Int main (void)

{

Struct Arr arr;

Init_arr (& amp; arr, 6); // test the initialization function.

// Show_arr (& amp; arr );

Append_arr (& amp; arr, 3 );

Append_arr (& amp; arr, 2 );

Append_arr (& amp; arr, 9 );

Insert_arr (& amp; arr, 2, 7 );

Show_arr (& amp; arr );

Return 0;

}

 

// Initialize the Array Function. pArr is the pointer of the struct variable arr.

Void init_arr (struct Arr * pArr, int length)

{

PArr-& gt; pBase = (int *) malloc (sizeof (int) * length); // malloc () function header file Declaration

If (NULL = pArr-& gt; pBase)

{

Printf ("dynamic memory allocation failed! \ N ");

Exit (-1); // declare it in the header file

}

Else

{

PArr-& gt; len = length;

PArr-& gt; cnt = 0;

}

 

}

// Array traversal function implementation

Void show_arr (struct Arr * pArr)

{

If (is_empty (pArr ))

{

Printf ("the array is empty \ n ");

}

Else

{

For (int I = 0; I & lt; pArr-& gt; cnt; I ++)

{

Printf ("% d", pArr-& gt; pBase [I]);

}

}

}

// Determine whether the array is empty

Bool is_empty (struct Arr * pArr)

{

If (pArr-& gt; cnt = 0)

Return true;

Else

Return false;

}

// Append an array element

Bool append_arr (struct Arr * pArr, int val)

{

If (pArr-& gt; cnt & lt; pArr-& gt; len)

{

PArr-& gt; pBase [pArr-& gt; cnt] = val;

(PArr-& gt; cnt) ++;

Return true;

}

Else

Printf ("array full \ n ");

Return false;

}

// Insert element

Bool insert_arr (struct Arr * pArr, int index, int val)

{

If (pArr-& gt; cnt & lt; pArr-& gt; len & amp; index & lt; = pArr-& gt; cnt)

{

For (int I = pArr-& gt; cnt-1; I & gt; = index-1; I --)

{

PArr-& gt; pBase [I + 1] = pArr-& gt; pBase [I];

 

 

}

PArr-& gt; pBase [index-1] = val;

(PArr-& gt; cnt) ++;

Return true;

}

Else

{

Printf ("insertion failed \ n ");

Return false;

}

 

 

} </Pre>

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.