-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0319ArraySelf.cpp
More file actions
85 lines (85 loc) · 2.07 KB
/
0319ArraySelf.cpp
File metadata and controls
85 lines (85 loc) · 2.07 KB
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/* deleteItem() , peek() 구현 */
#include <iostream>
using namespace std;
class ArrayList {
int capacity;
int size;
int *arrayLink;
public:
ArrayList() {
this->size=0;
this->capacity=4;
arrayLink = new int[capacity];
}
int getSize() { return size; }
int isEmpty() { return size == 0; }
int getCapacity() { return capacity; }
void insertItem(int newItem, int k);
int deleteItem(int k); //인덱스 k인 곳 삭제
void insertLast(int newItem);
int peek(int k);//인덱스 k인 배열 읽기
void reSize(int newSize);
void print();
};
int ArrayList::peek(int k) {
int temp;
temp = arrayLink[k];
return temp;
}
void ArrayList::reSize(int newSize) {
int *temp = new int[newSize];
for(int i=0; i<size; i++) temp[i]=arrayLink[i];
arrayLink = temp;
}
void ArrayList::insertLast(int newItem) {
if(size==capacity) {
capacity *= 2;
reSize(capacity);
}
arrayLink[size++] = newItem;
}
void ArrayList::print() {
if(isEmpty())
cout<<"배열이 비어있음";
else
for(int i=0; i<size; i++) cout<<arrayLink[i]<<"\t";
cout<<endl;
}
void ArrayList::insertItem(int newItem, int k) {
if(k>size) {
cout<<"배열의 크기에 벗어난 인덱스 입니다."<<endl;
return;
}
if(size==capacity) {
capacity *= 2;
reSize(capacity);
}
for(int i=size-1; i>=k; i--)
arrayLink[i+1] = arrayLink[i];
arrayLink[k] = newItem;
size++;
}
int ArrayList::deleteItem(int k) {
for(int i=k; i<size-1; i++)
arrayLink[i] = arrayLink[i+1];
size--;
if(size<capacity*0.5) {
capacity -=4;
reSize(capacity);
}
}
int main() {
ArrayList arr;
arr.insertLast(10); arr.insertItem(20, 1); arr.insertItem(30,2);
arr.insertLast(40); arr.insertLast(50); arr.insertLast(60);
arr.print();
arr.insertItem(100, 4); arr.print();
cout<<"크기: "<<arr.getSize()<<"용량: "<<arr.getCapacity()<<endl;
cout<<"선택한 값: "<<arr.peek(5)<<endl;
cout<<endl;
arr.deleteItem(5); arr.print();
arr.deleteItem(2); arr.print();
arr.deleteItem(1); arr.print(); arr.deleteItem(0); arr.print();
arr.deleteItem(0); arr.print();
cout<<"크기: "<<arr.getSize()<<"용량: "<<arr.getCapacity()<<endl;
}