7th Week C language and programming improvement routines-26 functions for basic operation of linear tables

Source: Internet
Author: User
Tags function definition

1, delete the data at the specified location;

Questions and Answers:

/* Delete data at the specified location */#include <stdio.h> #define SIZE   //define a character constant int deletedate (int[],int,int);   Declares a delete function int n=10;  The data in the array is actually useful int main () {    int a[size]={25,45,36,48,89,57,65,25,26,16};    int i,locate;    scanf ("%d", &locate);   Deletion of data at locate; n=deletedate (a,10,locate);    Function arguments: array name, length, position. The function returns the length n;for (i=0;i<n;i++) printf ("%d\n", A[i]); Output each element of return 0;   }  /* Function: Removes the element entry parameter at the specified position in the array: array name, array size, return value for the position of the element to delete: The size of the deleted array */int deletedate (int arr[],int len,int loc) {int i=loc;   This assignment indicates that the value of I is to be deleted;//the deleted position is written after the value of the while     (i<len-1)  //delete data after Len length decreased    {    arr[i]=arr[i+1]; The face value is covered with the preceding values    i++;} len--;  After deletion, the array size minus 1 return len; }

Operation Result:


Learning experience:

Delete data from one location, and each subsequent data will overwrite the previous data;

Knowledge Summary:
The number of deleted I loops becomes smaller;

The data length becomes smaller after deletion;

The function definition takes into account which parameters are used and whether there are return values;


2, insert a data at the specified position;

Questions and codes:

/* Insert a data at the specified location */#include <stdio.h> #define SIZE   //define a character constant int inserdate (int[],int,int,int);   declares an inserted function int n=10;  The data in the array is actually useful int main () {    int a[size]={25,45,36,48,89,57,65,25,26,16};    int i,locate,value;    scanf ("%d", &locate);   inserting data at locate; n=inserdate (a,10,locate,value);    Function arguments: array name, length, position. Insert value, function return length n;for (i=0;i<n;i++) printf ("%d\n", A[i]); Output each element of return 0;   }  /* Function: Inserts the element entry parameter at the specified position in the array: array name, array size, location to insert the element, and its value return value: The size of the array after inserting the value */int inserdate (int arr[],int len,int loc,int val) {int i =len;   Assigns the length of an array to I,//moves the value behind the Loc backward, while     (I>loc)      {    arr[i]=arr[i-1];//arr[i-1] When I takes the minimum, it represents the original value that will be inserted, Assign it to arr[], the original position will be vacated;     i--;} A value is written at the I==loc, i.e. the space vacated above; arr[i]=val; len++  ; After inserting the array size minus 1 return len; }
Operation Result:

Learning experience:

Similar to delete, the parameter entry adds an inserted value.

Knowledge Summary:

The number after which the group moves back, that is, a number after the previous one is overwritten, and the space at the insertion is vacated;


3, combined with an ordered array;

Questions and codes:

/* Merge two ordered arrays */#include <stdio.h> #define SIZE 100//define a character constant int mergedate (int[],int,int[],int,int[]);  a function int n1=10,n2=8,n3=0 that declares a merge function;    The data in the array is actually useful int main () {int a1[size]={25,45,36,48,89,57,65,25,26,16};    int a2[size]={46,85,77,16,85,48,95,25};       int a3[size*2];    int i;n3=mergedate (A1,10,A2,8,A3); Function arguments: Two sets of array name and length, new array name, function return new array length n3; for (i=0;i<n3;i++) printf ("%d\n", A3[i]);   Outputs each element of a new array return 0; }/* Function: Merge the array a1, A2, and close it to the new array A3; Entry parameters: Array name a1,a2,a3, array size n1,n2;   Return value: The size of the new array after merging; */int mergedate (int arr1[],int l1,int arr2[],int l2,int arr3[]) {int i=0,j=0,k=0; Initializes three loops;//i and J both start with the minimum value, compare the values in two arrays, assign the small value to the new array, while (I&LT;L1&AMP;&AMP;J&LT;L2) {if (Arr1[i]<arr2[j])//ar    R[i-1] When I take the minimum to indicate that the original value will be inserted, assign it to arr[], the original position will be vacated; arr3[k++]=arr1[i++]; else arr3[k++]=arr1[j++];} /* After the comparison above, the two arrays may be left with some elements, assigning the remaining number of an array to the new array; */while (I&LT;L1) arr3[k++]=arr1[i++];while (J&LT;L2) arr3[k++]=arr2[j++] ; return k; }
Operation Result:

Learning experience:

To merge an array, you need to know two arrays and their lengths to find a new array;

Knowledge Summary:

A new array is assigned by comparing the elements of two arrays.

The last remaining elements are assigned to the array together, but the elements are not sorted;


7th Week C language and programming improvement routines-26 functions for basic operation of linear tables

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.