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 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 | #include <stdio.h> #define MAX_QUEUE_SIZE 100 #define EXIT_FAILURE -1 typedef struct { int key; }element; //구조체 선언 element queue[MAX_QUEUE_SIZE]; // 크기가 MAX_QUEUE_SIZE인 구조체 배열 'queue' 선언 int rear = MAX_QUEUE_SIZE - 10; // rear값 설정 int front = 0; // front값 설정 void addq(int front, int *prear, element item, int k) { int i = 0; *prear = (*prear + 1) % MAX_QUEUE_SIZE; // prear 전진 if (front == *prear) { //전진한 prear가 front와 같을경우 printf("queue is full"); // queue = full reutrn; } else if (k < a && k> front) { // 그게아닐경우 k 가 front와 rear 사이의 수 라면 for (i = *prear ; i > k - 1; i--) // k+1번째부터 rear까지 element들을 전부 한칸씩 이동 queue[i] = queue[i - 1]; queue[k - 1] = item; // k번째 배열에 item 삽입 } } element deleteq(int *pfront, int rear, int k) { int i = 0; element item; element temp; if (*pfront == rear) // front == rear 이면 empty return; else if (k < rear && k> *pfront) { temp = queue[k - 1]; // temp에 k번째 원소값 저장 for (i = k - 1; i > *pfront; i--) // front부터 k번째까지 데이터값들을 전진 queue[i] = queue[i - 1]; *pfront = (*pfront + 1) % MAX_QUEUE_SIZE; // *pfront를 한칸 전진 } return temp; // 원래 k번째 요소를 리턴 } void main() { int j = 0; int k = 0; element item; for (j = front; j < rear; j++) queue[j].key = j; // queue배열에 값 저장. for (j = front; j < rear; j++) printf("queue[%d] = %d \n", j, queue[j]); // 배열에 저장된 값들을 표시 printf("삽입할 위치인 k를 입력하세요.\n"); scanf("%d", &k); //삽입할 위치 입력 printf("k에 입력할 값을 입력하세요\n"); scanf("%d", &item); //삽입할 위치에 삽입할 수 입력 addq(front, rear, item, k); printf("삽인된 요소는 %th element = item 입니다.", &k, &item); for (j = front; j < rear; j++) printf("queue[%d] = %d \n", j, queue[j]); //삽입이 된 후 배열 출력. printf("삭제할 위치인 k를 입력하세요.\n"); scanf("%d", &k); //삭제할 위치 k 입력. item = deleteq(front, rear, k); printf("삭제된 요소는 %dth element = item 입니다.", &k, &item); for (j = front; j < rear; j++) printf("queue[%d] = %d \n", j, queue[j]); //삭제한 후 배열 출력 } | cs |
제대로됐나여, 민소정님 참조가잘못됐다는건 알겠는데 그 댓글 답변 달아주신대로 잘 안됩니다...ㅠㅠ이해가안가욧
일단 님 코드는 잘 안보여서 - 민소하게! 더 민소민소하게!!
1234567890 10개 큐가 있어요 - 민소하게! 더 민소민소하게!!
근데 님이 !를 7번위치에 넣고 싶잖아요? - 민소하게! 더 민소민소하게!!
그러면 큐를 새로 만들고 원래 큐에서 dequeue를 6번해서 그값을 새로운 큐에 넣어요 - 민소하게! 더 민소민소하게!!
그리고 새로운 큐에 !를 enqueue하세요 - 민소하게! 더 민소민소하게!!
마지막으로 원래큐에서 dequeue 를 empty될때까지 하면서 나온 값을 새로운 queue에 enqueue하시면 되요 - 민소하게! 더 민소민소하게!!
그림 그리면서 한번 이해할려고 노력해보세요 - 민소하게! 더 민소민소하게!!
게슈탈트붕괴오네-_-;
하 씨발 ㅠㅠ
prear이(가) 0x5A였습니다. 이거 도대체뭐지 ㅠ