"linkedlist.h"
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#pragma warning(disable : 4996)
struct linked_list {
char passcode;
struct linked_list* next;
};
typedef struct linked_list node;
typedef node* link;
"listlib.c"
#include "linkedlist.h"
link createnode(char passcode)
{
link cur;
cur = (node*)malloc(sizeof(node));
if (cur == NULL)
return NULL;
cur->passcode = (char *)malloc(sizeof(char));
cur->passcode = passcode;
cur->next = NULL;
return cur;
}
link append(link head, link cur)
{
link nextnode = head;
if (head == NULL)
{
head = cur;
return head;
}
while (nextnode->next != NULL)
nextnode = nextnode->next;
nextnode->next = cur;
return head;
}
int printlist(link head)
{
int cnt = 0;
link nextnode = head;
while (nextnode != NULL)
{
printf("%3d번째 암호는 %c\n", ++cnt, nextnode->passcode);
nextnode = nextnode->next;
}
return cnt;
}
"functionlib.c"
#include "linkedlist.h"
void function(int choice, link head, link cur)
{
int input = 0;
char ch;
link nextnode = head;
link pre = head;
link post = NULL;
switch (choice) {
case 1:
printf("몇 번째 단지에 집어넣으시겠습니까? : ");
scanf("%d", &input);
if (input < 2)
{
printf("정도전의 집 단지는 바꿀 수 없소.\n신성불가침의 영역이오.\n");
break;
}
getchar();
printf("무슨 암호를 넣으시겠습니까? : ");
scanf("%c", &ch);
cur = createnode(ch);
for (int i = 0; i < input - 2; i++)
pre = pre->next;
post = pre->next;
pre->next = cur;
cur->passcode = ch;
cur->next = post;
break;
case 2:
printf("몇 번째 단지를 없애버릴까요? : ");
scanf_s("%d", &input);
if (input < 2)
{
printf("정도전의 집은 없앨 수 없소.\n");
break;
}
for (int i = 0; i < input - 2; i++)
pre = pre->next;
pre->next = pre->next->next;
break;
case 3:
printlist(head);
break;
case 4:
printf("몇 번째 단지의 정보가 궁금합니까? : ");
scanf_s("%d", &input);
for(int i=0; inext;
printf("%d번째 단지의 암호는 %c입니다.\n", input, nextnode->passcode);
break;
default:
printf("입력 오류 또는 종료 커맨드로 인한 종료.\n");
}
}
"main.c"
#include "linkedlist.h"
link createnode(char passcode);
link append(link head, link cur);
int function(int choice, link head, link cur);
int printlist(link head);
int main()
{
char passcode;
link head = NULL;
link cur = NULL;
int choice = 0;
printf("고려의 혁명을 위한 비밀문자를 입력하고 엔터 누르시오. 0을 누르면 끝입니다.\n");
while(1)
{
scanf_s("%c", &passcode, 1);
getchar();
if (passcode == '0')
break;
cur = createnode(passcode);
if (cur == NULL) exit(1);
head = append(head, cur);
}
system("cls");
while (choice != 5)
{
printf("암호가 완성되었습니다. 무얼 하시겠습니까?\n1.단지에 암호 삽입\n2.단지 제거\n3.전체 암호 검색\n4.n번째 단지 암호 검색\n5.종료\n선택하시오 : ");
scanf_s("%d", &choice);
function(choice, head, cur);
printf("\n\n");
}
system("pause");
return 0;
}
실행결과
댓글
댓글 쓰기