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 | #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, 0, 10); //a 리스트에 0번째열에 10을 초기화 insertlist(a, 50, &lena, 1, 15); //a 리스트에 1번째열에 15을 초기화 insertlist(b, 30, &lenb, 0, 20); //b 리스트에 0번째열에 20을 초기화 insertlist(a, 50, &lena, 2, 25); //a 리스트에 2번째열에 25을 초기화 insertlist(a, 50, &lena, 3, 30); //a 리스트에 3번째열에 30을 초기화 insertlist(b, 30, &lenb, 1, 35); //b 리스트에 1번째열에 35을 초기화 insertlist(b, 30, &lenb, 2, 40); //b 리스트에 2번째열에 40을 초기화 insertlist(a, 50, &lena, 4, 45); //a 리스트에 4번째열에 45을 초기화 insertlist(b, 30, &lenb, 3, 50); //b 리스트에 3번째열에 50을 초기화 deletelist(b, 30, &lenb, 1); // b리스트의 1번째 배열 삭제 deletelist(b, 30, &lenb, 1); // b리스트의 1번째 배열 삭제 (책이 잘못됐는지 똑같은게 두줄이 있습니다..) insertlist(b, 30, &lenb, 4, 55); //b 리스트에 4번째열에 55을 초기화 deletelist(a, 50, &lena, 3); // a리스트의 3번째 배열 삭제 deletelist(a, 50, &lena, 4); // b리스트의 4번째 배열 삭제 insertlist(a, 50, &lena, 4, 60); //a 리스트에 4번째열에 60을 초기화 insertlist(a, 50, &lena, 5, 70); //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 + 1) return 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; // 추가할 열에 입력한 변수 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]; // 삭제할 열 변수 temp에 저장 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 |
'C++ > 자료구조' 카테고리의 다른 글
c++ 자료구조 버블정렬 (0) | 2016.04.02 |
---|