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
#include <iostream>
 
using namespace std;
 
int insertlist(int list[], int n, int *length, int i, int x); // i번째 위치에 원소 x를 추가함
int deletelist(int list[], int n, int *length, int i); // i번째 원소 삭제
void printflist(int* list, int* length); // 출력 함수
 
int main() {
    int a[50], lena = 0;
    int b[30], lenb = 0, temp;
 
    insertlist(a, 50, &lena, 010); //a 리스트에 0번째열에 10을 초기화
    insertlist(a, 50, &lena, 115); //a 리스트에 1번째열에 15을 초기화
    insertlist(b, 30, &lenb, 020); //b 리스트에 0번째열에 20을 초기화
    insertlist(a, 50, &lena, 225); //a 리스트에 2번째열에 25을 초기화
    insertlist(a, 50, &lena, 330); //a 리스트에 3번째열에 30을 초기화
    insertlist(b, 30, &lenb, 135); //b 리스트에 1번째열에 35을 초기화
    insertlist(b, 30, &lenb, 240); //b 리스트에 2번째열에 40을 초기화
    insertlist(a, 50, &lena, 445); //a 리스트에 4번째열에 45을 초기화
    insertlist(b, 30, &lenb, 350); //b 리스트에 3번째열에 50을 초기화
    deletelist(b, 30, &lenb, 1); // b리스트의 1번째 배열 삭제
    deletelist(b, 30, &lenb, 1); // b리스트의 1번째 배열 삭제 (책이 잘못됐는지 똑같은게 두줄이 있습니다..)
    insertlist(b, 30, &lenb, 455); //b 리스트에 4번째열에 55을 초기화
    deletelist(a, 50, &lena, 3); // a리스트의 3번째 배열 삭제 
    deletelist(a, 50, &lena, 4); // b리스트의 4번째 배열 삭제
    insertlist(a, 50, &lena, 460); //a 리스트에 4번째열에 60을 초기화
    insertlist(a, 50, &lena, 570); //a 리스트에 5번째열에 70을 초기화
 
    printflist(a, &lena); // a리스트 출력
    printflist(b, &lenb); // b리스트 출력
}
 
int insertlist(int list[], int n, int * length, int i, int x)
{
    int j;
    if (i<0 || i>*length + 1return 0// 저장할 배열번호가 0보다 작거나 남은 길이와 맞지 않다면 함수 종료
    if (*length + 1 == n) return 0// 늘어나는길이 length가 n(50) 이라면 함수 종료 (저장공간 x)
    for (j = *length; j >= i; j--) list[j + 1= list[j]; // 다음 번호로 밀어내어 저장
    list[i] = x; // i번열에 x저장
    *length = *length + 1// 남은 길이 계산
    return 1;
}
 
int deletelist(int list[], int n, int *length, int i)
{
    int temp, j;
    if (i<0 || (i>*length)) return -1;
    temp = list[i];
    for (j = i + 1; j <= *length; j++) list[j - 1= list[j];
    *length = *length - 1;
    return (temp);
}
 
void printflist(int * list, int * length)
{
    for (int i = 0; i < *length; i++// 길이에 맞춰 출력
        cout << *(list + i) << " "; // 포인터 list로 배열 참조하여 출력
    cout << "\n";
}
 
 
cs


+ Recent posts