Use arrays to implement linear table operations (in C)

Source: Internet
Author: User

 

Sort using simple insert sorting:

C code

Void one_sort (int * sqList, int val, int len)

{

Int pos = len;

Int temp = val;

While (val <sqList [pos-1] & pos> 0)

{

SqList [pos] = sqList [pos-1];

Pos --;

}

SqList [pos] = temp;

}

// Directly insert sorting

Void straisort (struct Arr * pArr)

{

For (int I = 1; I <pArr-> cnt; I ++)

{

One_sort (pArr-> pBase, pArr-> pBase [I], I); // call the single-step sorting

}

}

 

Insert the General sorting algorithm directly:

C code

Void one_sort (int * sqList, int val, int len)

{

Int pos = len;

Int temp = val;

While (val <sqList [pos-1] & pos> 0)

{

SqList [pos] = sqList [pos-1];

Pos --;

}

SqList [pos] = temp;

}

// Directly insert sorting

Void straisort (int * arr, int len)

{

Int I;

For (I = 1; I <len; I ++)

{

One_sort (arr, arr [I], I); // call insert sort directly

}

}

 

 

The code for the Array Operation of a linear table is as follows. Of course, the function is not comprehensive and will be collected later.

 

C code

/*

Implementation of linear structure Arrays

*/

# Include <stdio. h>

# Include <malloc. h> // contains the malloc Function

# Include <stdlib. h> // 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 (struct Arr * pArr, int pos, int * pVal); // delete an element

Int get (struct Arr * pArr, int index); // obtain the element

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

Bool is_full (struct Arr * pArr); // determines whether it is full.

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

Void inversion_arr (struct Arr * pArr); // array Inversion

Void one_sort (int * sqList, int val, int len); // One-Step sorting statement

Void straisort (struct Arr * pArr); // Insert the sorting statement directly.

 

Int main (void)

{

Struct Arr arr;

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

// Show_arr (& arr );

Append_arr (& arr, 3 );

Append_arr (& arr, 2 );

Append_arr (& arr, 9 );

Insert_arr (& arr, 2, 7 );

Show_arr (& 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-> pBase = (int *) malloc (sizeof (int) * length); // malloc () function header file Declaration

If (NULL = pArr-> pBase)

{

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

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

}

Else

{

PArr-> len = length;

PArr-> 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 <pArr-> cnt; I ++)

{

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

}

}

}

// Determine whether the array is empty

Bool is_empty (struct Arr * pArr)

{

If (pArr-> cnt = 0)

Return true;

Else

Return false;

}

// Append an array element

Bool append_arr (struct Arr * pArr, int val)

{

If (pArr-> cnt <pArr-> len)

{

PArr-> pBase [pArr-> cnt] = val;

(PArr-> cnt) ++;

Return true;

}

Else

Printf ("array full \ n ");

Return false;

}

// Insert element

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

{

If (pArr-> cnt <pArr-> len & index <= pArr-> cnt)

{

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

{

PArr-> pBase [I + 1] = pArr-> pBase [I];

}

PArr-> pBase [index-1] = val;

(PArr-> cnt) ++;

Return true;

}

Else

{

Printf ("insertion failed \ n ");

Return false;

}

 

 

}

// Insert sorting in one step

 

Void one_sort (int * sqList, int val, int len)

{

Int pos = len;

Int temp = val;

While (val <sqList [pos-1] & pos> 0)

{

SqList [pos] = sqList [pos-1];

Pos --;

}

SqList [pos] = temp;

}

// Directly insert sorting

Void straisort (struct Arr * pArr)

{

For (int I = 1; I <pArr-> cnt; I ++)

{

One_sort (pArr-> pBase, pArr-> pBase [I], I); // call the single-step sorting

}

}

// Array Inversion

Void inversion_arr (struct Arr * pArr)

{

Int I = 0;

Int j = pArr-> cnt-1; // relationship between the first and the end of the downlink

Int t;

 

While (I <j)

{

T = pArr-> pBase [I];

PArr-> pBase [I] = pArr-> pBase [j];

PArr-> pBase [j] = t;

I ++;

J --;

}

Return;

}

// Delete an element

Bool delete_arr (struct Arr * pArr, int pos, int * pVal)

{

Int I;

 

If (is_empty (pArr ))

Return false;

If (pos <1 | pos> pArr-> cnt)

Return false;

 

* PVal = pArr-> pBase [pos-1];

For (I = pos; I <pArr-> cnt; I ++)

{

PArr-> pBase [I-1] = pArr-> pBase [I];

}

PArr-> cnt --;

Return true;

}

// Determine if it is full

Bool is_full (struct Arr * pArr)

{

If (pArr-> cnt = pArr-> len)

Return true;

Else

Return false;

}

 

// Search for elements

Int get (struct Arr * pArr, int index)

{

For (int I = 0; I <pArr-> cnt; I ++)

{

If (index = I)

{

Return pArr-> pBase [I];

}

}

}

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.