Q1. 원형리스트를 이용해 리스트에 10,20,30,40를 순서대로 삽입하고 리스트를 출력하는 프로그램 작성
#include <stdio.h>
#include <stdlib.h>
// 원형 리스트 노드 구조체 정의
typedef struct Node {
int data;
struct Node* next;
} Node;
// 원형 리스트에 데이터 삽입
void insertCircularList(Node** head, int value) {
Node* newNode = (Node*)malloc(sizeof(Node));
newNode->data = value;
if (*head == NULL) {
newNode->next = newNode;
*head = newNode;
}
else {
Node* current = *head;
while (current->next != *head) {
current = current->next;
}
newNode->next = *head;
current->next = newNode;
}
}
// 원형 리스트 출력
void printCircularList(Node* head) {
if (head == NULL) {
printf("리스트가 비어 있습니다.\n");
return;
}
Node* current = head;
do {
printf("%d -> ", current->data);
current = current->next;
} while (current != head);
printf("\n");
}
int main() {
Node* myList = NULL;
insertCircularList(&myList, 10);
insertCircularList(&myList, 20);
insertCircularList(&myList, 30);
insertCircularList(&myList, 40);
printCircularList(myList);
return 0;
}
출력결과

Q2. 5명의 플레이어가 보드게임을 한다. 게임에 빠지면 누구 순서인지 잊어버릴 수 있다. 원형리스트를 이용해서 프로그램을 작성해 현재 누구 순서인지 출력
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#pragma warning (disable : 4996)
// 플레이어 구조체 정의
typedef struct Player {
char name[20];
struct Player* next;
} Player;
// 원형 리스트에 플레이어 추가
void addPlayer(Player** head, char playerName[]) {
Player* newPlayer = (Player*)malloc(sizeof(Player));
strcpy(newPlayer->name, playerName);
if (*head == NULL) {
newPlayer->next = newPlayer;
*head = newPlayer;
}
else {
Player* current = *head;
while (current->next != *head) {
current = current->next;
}
newPlayer->next = *head;
current->next = newPlayer;
}
}
// 현재 순서 출력
void printCurrentTurn(Player* head) {
printf("현재 차례는=%s\n", head->name);
}
int main() {
Player* playerList = NULL;
char players[5][20] = { "Kim", "Park", "Lee", "Choi", "Jung" };
int currentPlayer = 0;
// 5명의 플레이어를 원형 리스트에 추가
for (int i = 0; i < 5; i++) {
addPlayer(&playerList, players[i]);
}
// 현재 순서 출력
for (int i = 0; i < 5; i++) {
printCurrentTurn(playerList);
playerList = playerList->next;
}
return 0;
}
실행결과

Q3. 이중 연결리스트를 이용해 0,1,2,3,4를 입력 후 4,3,2,1,0을 삭제하고 단계별로 리스트를 출력하는 프로그램
#include <stdio.h>
#include <stdlib.h>
#pragma warning (disable : 4996)
// 노드 구조체 정의
typedef struct Node {
int data;
struct Node* prev;
struct Node* next;
} Node;
// 이중 연결 리스트에 노드 추가
void addNode(Node** head, int data) {
Node* newNode = (Node*)malloc(sizeof(Node));
newNode->data = data;
newNode->prev = NULL;
newNode->next = NULL;
if (*head == NULL) {
*head = newNode;
}
else {
Node* current = *head;
while (current->next != NULL) {
current = current->next;
}
current->next = newNode;
newNode->prev = current;
}
}
// 이중 연결 리스트에서 노드 삭제
void deleteNode(Node** head, int data) {
if (*head == NULL) {
printf("리스트가 비어 있습니다.\n");
return;
}
Node* current = *head;
while (current != NULL) {
if (current->data == data) {
if (current->prev != NULL) {
current->prev->next = current->next;
}
else {
*head = current->next;
}
if (current->next != NULL) {
current->next->prev = current->prev;
}
free(current);
printf("노드 %d 삭제 완료\n", data);
return;
}
current = current->next;
}
printf("노드 %d를 찾을 수 없습니다.\n", data);
}
// 이중 연결 리스트 출력
void printList(Node* head) {
if (head == NULL) {
printf("리스트가 비어 있습니다.\n");
return;
}
Node* current = head;
while (current != NULL) {
printf("%d ", current->data);
current = current->next;
}
printf("\n");
}
int main() {
Node* myList = NULL;
// 노드 추가
addNode(&myList, 0);
addNode(&myList, 1);
addNode(&myList, 2);
addNode(&myList, 3);
addNode(&myList, 4);
// 초기 리스트 출력
printf("초기 리스트: ");
printList(myList);
// 노드 삭제
deleteNode(&myList, 4);
printf("4 삭제 후 리스트: ");
printList(myList);
deleteNode(&myList, 3);
printf("3 삭제 후 리스트: ");
printList(myList);
deleteNode(&myList, 2);
printf("2 삭제 후 리스트: ");
printList(myList);
deleteNode(&myList, 1);
printf("1 삭제 후 리스트: ");
printList(myList);
deleteNode(&myList, 0);
printf("0 삭제 후 리스트: ");
printList(myList);
return 0;
}
실행결과

Q4. 이중 연결리스트를 이용해 음악을 저장하고 사용자의 명령어에 따라 곡을 선택하는 프로그램
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#pragma warning (disable : 4996)
// 음악 구조체 정의
typedef struct Music {
char title[100];
char artist[100];
struct Music* prev;
struct Music* next;
} Music;
// 이중 연결 리스트에 음악 추가
void addMusic(Music** head, char title[], char artist[]) {
Music* newMusic = (Music*)malloc(sizeof(Music));
strcpy(newMusic->title, title);
strcpy(newMusic->artist, artist);
newMusic->prev = NULL;
newMusic->next = NULL;
if (*head == NULL) {
*head = newMusic;
}
else {
Music* current = *head;
while (current->next != NULL) {
current = current->next;
}
current->next = newMusic;
newMusic->prev = current;
}
}
// 음악 리스트 출력
void printMusicList(Music* head) {
if (head == NULL) {
printf("음악 리스트가 비어 있습니다.\n");
return;
}
Music* current = head;
while (current != NULL) {
printf("곡: %s\n", current->title);
printf("아티스트: %s\n", current->artist);
printf("------------------------\n");
current = current->next;
}
}
// 음악 선택 및 재생
void playMusic(Music* head) {
if (head == NULL) {
printf("음악 리스트가 비어 있습니다.\n");
return;
}
char command;
Music* current = head;
while (1) {
printf("명령어를 입력하세요 (>, <, q): ");
scanf(" %c", &command);
if (command == '>') {
if (current->next != NULL) {
current = current->next;
printf("현재 재생 중인 곡: %s - %s\n", current->title, current->artist);
}
else {
printf("마지막 곡입니다. 다음 곡이 없습니다.\n");
}
}
else if (command == '<') {
if (current->prev != NULL) {
current = current->prev;
printf("현재 재생 중인 곡: %s - %s\n", current->title, current->artist);
}
else {
printf("첫 번째 곡입니다. 이전 곡이 없습니다.\n");
}
}
else if (command == 'q') {
printf("프로그램을 종료합니다.\n");
break;
}
else {
printf("잘못된 명령어 입력하셨습니다. 올바른 명령어를 입력해주세요.\n");
}
}
}
int main() {
Music* musicList = NULL;
// 음악 추가
addMusic(&musicList, "Fernando", "ABBA");
addMusic(&musicList, "Dancing queen", "ABBA");
addMusic(&musicList, "Mamma Mia", "ABBA");
// 음악 리스트 출력
printf("음악 리스트:\n");
printMusicList(musicList);
// 음악 선택 및 재생
printf("음악을 선택하여 재생합니다.\n");
playMusic(musicList);
return 0;
}
실행결과
