aboutsummaryrefslogtreecommitdiff
path: root/main.c
diff options
context:
space:
mode:
authorleiyu3 <s444814187@gmail.com>2022-09-20 16:17:17 -0400
committerleiyu3 <s444814187@gmail.com>2022-09-20 16:17:17 -0400
commit8614d6b1f445b1725eb23eeb3e6ae2c62fcc63fb (patch)
tree2c66ca51a93f8ac151d2dc16bd283eb4d27b4f92 /main.c
parent990a9b141a2388531aab958d4d0852638c766869 (diff)
downloadvector_c-8614d6b1f445b1725eb23eeb3e6ae2c62fcc63fb.tar.gz
vector_c-8614d6b1f445b1725eb23eeb3e6ae2c62fcc63fb.zip
implement pop
Diffstat (limited to 'main.c')
-rw-r--r--main.c30
1 files changed, 14 insertions, 16 deletions
diff --git a/main.c b/main.c
index ab3fbb3..63fe862 100644
--- a/main.c
+++ b/main.c
@@ -78,7 +78,13 @@ void insert(vector_t *vec, int index, int val){
78 78
79void prepend(vector_t *vec, int value){ 79void prepend(vector_t *vec, int value){
80 insert(vec, 0, value); 80 insert(vec, 0, value);
81}
81 82
83int pop(vector_t *vec){
84 // remove item at the end, return value
85 int ret = vec->arr[vec->cur_size-1];
86 vec->cur_size--;
87 return ret;
82} 88}
83 89
84void print_vec(vector_t vec){ 90void print_vec(vector_t vec){
@@ -101,25 +107,17 @@ int main(void){
101 printf("The capacity of the vector is %d.\n", capacity(vec)); 107 printf("The capacity of the vector is %d.\n", capacity(vec));
102 printf("is_empty returns: %d.\n", is_empty(vec)); 108 printf("is_empty returns: %d.\n", is_empty(vec));
103 109
104 push(&vec, 1);
105 push(&vec, 2);
106 push(&vec, 3);
107 push(&vec, 4);
108 push(&vec, 5);
109 push(&vec, 6);
110 push(&vec, 7);
111 push(&vec, 8);
112 push(&vec, 9);
113 push(&vec, 10);
114 push(&vec, 11);
115 push(&vec, 12);
116 push(&vec, 13);
117 push(&vec, 14);
118 push(&vec, 15);
119 prepend(&vec, 0);
120 110
121 printf("-----\n"); 111 printf("-----\n");
122 112
113 for (int i = 0; i < vec.max_size; i++){
114 push(&vec, i);
115 }
116
117 for (int i = 0; i < vec.max_size; i++){
118 printf("pop: %d\n", pop(&vec));
119 }
120
123 printf("The size of the vector is %d.\n", size(vec)); 121 printf("The size of the vector is %d.\n", size(vec));
124 printf("The first element of the vector is %d.\n", at(vec, 0)); 122 printf("The first element of the vector is %d.\n", at(vec, 0));
125 printf("is_empty returns: %d.\n", is_empty(vec)); 123 printf("is_empty returns: %d.\n", is_empty(vec));