forked from akij17/Data-Structures-in-Pure-C
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharraylist.c
53 lines (46 loc) · 1.26 KB
/
arraylist.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
//
// Created by Akshay on 10/27/2018.
//
#include "arraylist.h"
#include <stdio.h>
#include <malloc.h>
arrayList *newArrayList(int n){
arrayList *aList = malloc(sizeof(*aList) + sizeof(int)*n);
aList->size = 0;
aList->totalSize = n;
aList->data = malloc(sizeof(int)*n);
return aList;
}
void insert_arrayList(arrayList *aList, int item, int position){
if(aList->size < aList->totalSize && position>-1){
int endPos = aList->size;
for(int i = endPos; i>=position; i--){
(aList->data)[i+1] = (aList->data)[i];
}
(aList->data)[position] = item;
(aList->size)++;
}
}
int isEmpty_arrayList(arrayList aList){
if(aList.size == 0)
return 1;
else return 0;
}
void displayList_arrayList(arrayList aList){
printf("Contents of array list: ");
for(int i = 0; i<aList.size; i++){
printf("%d -> ", aList.data[i]);
}
}
void delete_arrayList(arrayList *aList, int pos){
if(pos > aList->size){
//Overflow condition
}else if(pos==aList->size){
(aList->size)--;
}else{
for(int i = pos; i<aList->size; i++){
(aList->data)[i] = (aList->data)[i+1];
}
(aList->size)--;
}
}