Array Maintenance


Time limit: 5000ms
Memory limit: 256mb

Description:
In this exercise, you are required to maintain an array of sorted integers in ascending order, and support the following operations.

1.	Insert an integer
2.	Delete an integer
3.	Update an integer
4.	Print all integers of the array in order

The code template has been provided. You need to complete four functions.

You can assume that there are 100 integers at most, so an array A[100] is enough to store all the integers. What's more, all the integers of A[] are distinct, which means that A[0] < A[1] < ... < A[n-1].

The program firstly reads an integer n, which represents the number of integers in the array A, and then reads n integers and store them as A[0], A[1], ..., A[n-1]. 

Then the program performs Insert, Delete, Update and Print operations. The user will be prompted to input the integer(s) for insertion, deletion and update.

int Insert(int value, int A[], int n); 
Insert the value into A[] of length n and return the new length. You could assume that the value doesn't exist in A[] before insertion. After insertion, A[] should still be in ascending order.

int Delete(int value, int A[], int n); 
Delete value from A[] of length n and return the new length. You could assume that the value exists in A[] before deletion. After deletion, A[] should still be in ascending order. 

int Update(int old_value, int new_value, int A[], int n);
Change old_value to new_value in A[] of length n and return the new length. You could assume that the old_value exists in A[] but the new_value does not. After the update, A[] should still be in ascending order.

void Print(int A[], int n);
Print A[0], A[1], ..., A[n-1] in one line. Every two integers are separated by a space.

Sample Input 1:
4
1 4 7 10
5
4
7 2

Sample Output 1:
Input the value for insertion:
Input the value for deletion:
Input the old and new values for update:
1 2 5 10

Sample Input 2:
3
1 5 6
0
5
0 5

Sample Output 2:
Input the value for insertion:
Input the value for deletion:
Input the old and new values for update:
1 5 6


Hint:
You may use Insert() and Delete() to implement Update() and do not forget to update n after insertion and deletion.

Code Template

#include <stdio.h>

int Insert(int value, int A[], int n)
{
    // insert value into A[], return new length n

}

int Delete(int value, int A[], int n)
{
    // delete value from A[], return new length n

}

int Update(int old_value, int new_value, int A[], int n)
{
    // change old_value to new_value, return new length n

}

void Print(int A[], int n)
{
    // print A[0], A[1], ..., A[n-1] in one line, every two integers are separated by a space

}

int main()
{
    int A[100];
    int n; // the number of integers in A
    int value, new_value;

    scanf("%d", &n);
    for(int i=0; i<n; i++)
        scanf("%d", &A[i]);

    printf("Input the value for insertion:\n");
	scanf("%d", &value);
	n = Insert(value, A, n);

	printf("Input the value for deletion:\n");
	scanf("%d", &value);
	n = Delete(value, A, n);

	printf("Input the old and new values for update:\n");
	scanf("%d%d", &value, &new_value);
	n = Update(value, new_value, A, n);

	Print(A, n);
	return 0;
}
Submit