Sequence Table in C language and sequence in C Language

Source: Internet
Author: User

Sequence Table in C language and sequence in C Language

There are two types of data structures: a linear structure (including sequence tables, linked lists, stacks, and queues), a non-linear structure (tree, graph ),

The sequence table is actually a dynamic array in the memory, and the ArrayList in Java is a typical sequence table. It adds a resizing mechanism based on the sequence table.

The following are three steps,

1. Create an ordered table structure,

2. paste the code for all operations

3. Final Test

First, create the sequence table structure

Struct Arr {int * pBase; int len; // total length int cnt; // current length };

All declarations

Void init_arr (struct Arr * arr, int lens); // 1. initialize bool is_empty (struct Arr * arr); // 2. empty bool is_full (struct Arr * arr); // 3. void show_arr (struct Arr * arr); // 4. display the array bool append (struct Arr * arr, int num); // 5. append value bool insert (struct Arr * arr, int pos, int data); // 6. insert the specified position bool remove (struct Arr * arr, int post, int * pVal); // 7. delete the value of the specified index int get_arr (struct Arr * arr, int pos); // 8. get the element bool inversion_arr (struct Arr * arr); // 9. inverted Sequence Table void sort_arr (struct Arr * arr); // 10. sort

  

 

Second, all operation Definitions

1. Initialization

// 1. initialize the sequence table void init_arr (struct Arr * arr, int lens) {arr-> pBase = (int *) malloc (sizeof (struct Arr) * lens ); if (NULL = arr-> pBase) {printf ("memory allocation failed \ n"); exit (-1) ;}arr-> len = lens; arr-> cnt = 0; printf ("initialize array arr, array length % d \ n", lens); return ;}

2. Check whether the table is empty.

// 2. Determine whether the empty table bool is_empty (struct Arr * arr) {if (arr-> cnt = 0) {return true;} return false ;}

3. Check whether the table is full.

// 3. determine whether the sequence table is full bool is_full (struct Arr * arr) {if (arr-> cnt = arr-> len) {return true;} return false ;}

4. Print the table

// 4. print out the sequence table void show_arr (struct Arr * arr) {if (is_empty (arr) {printf ("the array is empty \ r \ n "); exit (-1);} printf ("Print current array:"); for (int I = 0; I <arr-> cnt; I ++) {printf ("% d % s", arr-> pBase [I], I! = Arr-> cnt-1? ",": "\ N ");}}

5. append Elements

// 5. append element bool append (struct Arr * arr, int num) {if (NULL = arr) {printf ("array is NULL \ r \ n ");} if (is_full (arr) {return false;} int cnt = arr-> cnt; arr-> pBase [cnt ++] = num; arr-> cnt = cnt; return true ;}

6. Specify the index position to insert Elements

// 6. insert element bool insert (struct Arr * arr, int pos, int data) {if (is_full (arr) {return false ;} // pos cannot be greater than the actual position if (pos <0 | pos> arr-> cnt) {return false;} for (int I = arr-> cnt; i> = pos; -- I) {arr-> pBase [I + 1] = arr-> pBase [I];} arr-> pBase [pos] = data; arr-> cnt ++; return true ;}

7. Delete the number of specified index locations

bool remove(struct Arr* arr, int pos, int * pVal) {if (is_empty(arr)) {return false;}if (pos<0|| pos>arr->cnt-1){return false;}*pVal = arr->pBase[pos];for (int i = pos+1; i <= arr->cnt; ++i){arr->pBase[i - 1] = arr->pBase[i];}arr->cnt--;return true;}

8. Get Elements

// 8. Get the element int get_arr (struct Arr * arr, int pos) {if (is_empty (arr) {return NULL;} return arr-> pBase [pos];}

9. Inverted Arrangement

Bool inversion_arr (struct Arr * arr) {int I = 0; // The first element int j = arr-> cnt-1; // valid index value int temp; while (I <j) {temp = arr-> pBase [I]; arr-> pBase [I] = arr-> pBase [j]; arr-> pBase [j] = temp; I ++; j --;} return true ;}

10. Sort

Void sort_arr (struct Arr * arr) {// sort by bubble. The maximum value is pushed to the backend./* for (int I = 0; I <arr-> cnt; I ++) {for (int j = 0; j <arr-> cnt-i-1; j ++) {int temp = arr-> pBase [j]; if (arr-> pBase [j]> arr-> pBase [j + 1]) {arr-> pBase [j] = arr-> pBase [j + 1]; arr-> pBase [j + 1] = temp ;}} * // sort by bubble 2 and push the smallest value to the front for (int I = 0; I <arr-> cnt; I ++) {for (int j = I + 1; j <arr-> cnt; j ++) {if (arr-> pBase [I]> arr-> pBase [j]) {int temp = arr-> pBase [I]; arr-> pBase [I] = arr-> pBase [j]; arr-> pBase [j] = temp ;}}}}

Third, the final test result

# Include "stdafx. h "# include <stdlib. h> # include "mallocDemo. h "struct Arr {int * pBase; int len; // total length int cnt; // current length}; void init_arr (struct Arr * arr, int lens ); // 1. initialize bool is_empty (struct Arr * arr); // 2. empty bool is_full (struct Arr * arr); // 3. void show_arr (struct Arr * arr); // 4. display the array bool append (struct Arr * arr, int num); // 5. append value bool insert (struct Arr * arr, int pos, int data); // 6. insert bool remove (struc T Arr * arr, int post, int * pVal); // 7. delete the value of the specified index int get_arr (struct Arr * arr, int pos); // 8. get the element bool inversion_arr (struct Arr * arr); // 9. inverted Sequence Table void sort_arr (struct Arr * arr); // 10. sort int main () {struct Arr arr; init_arr (& arr, 6); append (& arr, 1); append (& arr, 2); append (& arr, 3); append (& arr, 4); append (& arr, 5); show_arr (& arr); // 1. insert bool is_insert = insert (& arr, 5, 6) at the specified position; // insert the specified position if (is_insert) {printf ("insert value at index % d: % d Success \ n ", 5, 6);} else {printf (" inserting value at index % d: % d failed! \ N ", 5, 6) ;}show_arr (& arr); // 2. delete the specified position int val; if (remove (& arr, 1, & val) {printf ("the value % d removed from index % d succeeded \ n", 1, val) ;}else {printf ("% d failed to remove Value % d at index % d") ;}; show_arr (& arr ); int num = get_arr (& arr, 0); printf ("Get index: 0 values: % d \ n", num ); printf ("Inverted array \ n"); inversion_arr (& arr); show_arr (& arr); printf ("start sorting \ n"); sort_arr (& arr ); show_arr (& arr); return 0 ;}

The test result is as follows:

 

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.