#include"ArrayList.h"

int main(void){
        int list[SIZE]={0};
        int loc,x, i, n;
        do{
                printf("\nInput how many numbers to be inserted? ");
                scanf("%d", &n);
                if(n>SIZE)
                        printf("\nYour input exceeds the allowable 
array size...\nPlease try again..\n");
        }while(n>SIZE);
        for(i=0; i<n; i++){
                printf("Enter a number %d: ",i+1);
                scanf("%d",&list[i]);
        }
        

        printf("\nThere are %d elements in the array.\n",count
(list));
        display(list);
        insert(list,15,0);
        printf("\nThere are %d elements in the array.\n",count
(list));
        display(list);
        insert(list,5,6);
        printf("\nThere are %d elements in the array.\n",count
(list));
        display(list);
        insert(list, 20, 5);
        printf("\nThere are %d elements in the array.\n",count
(list));
        display(list);
        remove(list,1);
        printf("\nThere are %d elements in the array.\n",count
(list));
        display(list);
        remove(list,7);
        printf("\nThere are %d elements in the array.\n",count
(list));
        display(list);
        remove(list,4);
        printf("\nThere are %d elements in the array.\n",count
(list));
        display(list);
        x=6;

        loc=binarySearch(list,x);
        if(loc!=0)
                printf("\nNumber %d is found at position %d.",x,loc);
        else
                printf("\nNumber %d is not found!",x);
        printf("\nThe sorted list in ascending order: \n");
        selectionSort(list); /*try test using insertionSort(list) or 
selectionSort(list) */
        display(list);
        printf("\n\nThat's all folks!!!\n\n");
}










/*
ArrayList.h
NOTE: You may add other utility functions or or make changes on the 
given declarations below..
*/



#ifndef A_H
#define A_H

#include<stdio.h>
#include<string.h>
#define SIZE 10

typedef int * List;

void insert(List l, int item, int pos); /* Inserts item int the the 
list l at position pos */
void remove(List l, int pos); /* Removes item from the list l given 
position pos */

int linearSearch(List l, int x); /* Search for the item and returns 
position if item is found and 0 otherwise */
int binarySearch(List l, int x); /* Search for the item and returns 
position if item is found and 0 otherwise */

void bubbleSort(List l); /* Sorts the items in the list l from 
ascending to descending */
void insertionSort(List l); /* Sorts the items in the list l from 
ascending to descending */
void selectionSort(List l); /* Sorts the items in the list l from 
ascending to descending */

void display(List l); /* Displays the list of items */
int count(List l); /* Returns the number of items in the list l */

#endif A_H














Reply via email to