|
소스를 이해해야하는데요
큰 내용은 이해를해서 주석을 달아놓았거덩요...
근데요...세부적인 부분의 의미를 모르겠어요...
이해할 수 있도록..세부적인 주석을...좀 부탁드려도 될까요?^^
행복한 12월 되세요 ^^*
읽어주셔서 감사합니다 (--)(__)
/* 프로그램 설명
< 단순 연결 리스트 구현 >
1.노드 구성 (struct _node)
name: 문자열 → char name[3];
score: 정수형 → int score;
next: 다음 노드 포인터 → struct _node *next;
2.조작 팁(tip)
headp: 하나의 포인터 사용 (head pointer)
3.구현되어야 하는 연산
ordered_insert: 삽입 연산 ? score의 순서에 따라
delete: 삭제 연산 ? name으로 node 선택
traverse: 탐색 연산 (recursive)
exit: 종료 연산 ? memory free (recursive) */
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>
typedef struct link_t {
char ch[3];
int num;
struct link_t *next;
} link;
void in_link(char word[],int i);//입력하는 함수
void de_link(char word[]);//삭제하는 함수
void traverse(link *heap);//보여주는 함수
void exit_link(link *heap);//끝내는 함수
void init_link();
link *topp;
link *curr;
void main ()
{
int menu;
init_link();
do {
printf("select menu(1.insert 2.delete 3.traverse 4.exit) : ");
scanf("%d",&menu);
switch (menu)
{
case 1:
{
char name1[3];
int score;
printf("name? : ");
scanf("%s",name1);
printf("score? : ");
scanf("%d",&score);
in_link(name1,score);
break;
}
case 2:
{
char name2[3];
printf("name? : ");
scanf("%s",name2);
de_link(name2);
break;
}
case 3:
{
printf("이름 점수\n");
curr=topp;
traverse(curr);
break;
}
case 4:
{
printf("이름 점수\n");
curr=topp;
exit_link(curr);
break;
}
default:
{
printf("wrong select.");
}
}
} while (menu!=4);
}
void in_link(char word[],int i)//삽입연산,score 순서에 따라 삽입
{
link *prep;
link *curp;
link *temp;
prep=topp;
curp=prep->next;
temp=(link *)malloc(sizeof(link));
strcpy(temp->ch,word);
temp->num=i;
if(curp!=NULL)
{
while(i>=curp->num)
{
prep=curp;
curp=curp->next;
if (curp==NULL) break;
}
if(curp!=NULL)
{
prep->next=temp;
temp->next=curp;
}
}
if(curp==NULL)
{
prep->next=temp;
temp->next=NULL;
}
}
void de_link(char word[])
/*delete
name에 맞는 노드를 삭제
아이디어
삭제할 노드의 name 입력
headp 기준
마지막 노드인 경우
노드를 free 시킨 후, 노드의 끝을 NULL로 만들어 준다
마지막 노드가 아닌 경우
노드의 포인터를 바꾼 후, 노드를 free 시킨다*/
{
link *prep;
link *delp;
int qqq=0;
prep=topp;
delp=prep->next;
if(delp!=NULL)
{
while(strcmp(word,delp->ch)!=0)
{
prep=delp;
delp=delp->next;
if (delp==NULL)
{
qqq=1;
break;
}
}
}
if(qqq==1) printf("There''s nothing.\n");
else
{
prep->next=delp->next;
free(delp);
}
}
void traverse(link *heap)
/* 탐색연산
리스트의 노드 데이터들을 보여주는 함수
재귀적(recursive)으로 구현
아이디어
1.리스트가 없는 경우
출력 없이 return
2.리스트가 있는 경우
노드 데이터 출력 후, 나머지는 재귀 함수의 매개변수로 넘겨준다
3.알고리즘
초기 호출 시에 traverse의 매개변수는 headp를 기준한다*/
{
link *tmpp;
tmpp=heap->next;
if(heap->next!=NULL)
{
printf("%4s %4d\n",tmpp->ch,tmpp->num);
traverse(heap->next);
}
}
void exit_link(link *heap)
/*exit
메모리의 모든 공간을 free 시킨 후 종료하는 함수
재귀적으로 구현
아이디어
traverse와 유사
리스트가 없는 경우
headp까지 free
리스트가 있는 경우
노드를 free 시킨 후, 나머지 노드를 재귀 함수의 매개변수로 넘긴다*/
{
if(heap->next!=NULL)
{
link *afterp;
afterp=heap->next;
printf("%4s %4d\n",afterp->ch,afterp->num);
free(heap);
exit_link(afterp);
}
}
void init_link()
{
topp=(link *)malloc(sizeof(link));
topp->next=NULL;
curr=(link *)malloc(sizeof(link));
}
|