본문 바로가기

Programming/C&C++&C#

[C언어] 간단한 주소록 만들기를 링크드리스트로 구현

728x90
반응형

#include <stdio.h>

#include <stdlib.h>

#include <string.h>


#define MAX_NAME 50

#define TRUE 1


typedef struct node *NodePointer;

typedef struct node {

int age;

char name[MAX_NAME];

NodePointer link;

} Node;


void Make_Node(NodePointer );

void Print_Node(NodePointer );

void Fine_Node(NodePointer );

void delete_Node(NodePointer);


int main( void )

{

int i,j;

int nember;


NodePointer head = NULL;

NodePointer temp = head;


while(TRUE)

{

printf("1) 입력 \n 2) 출력  \n3) 검색   \n 4) 삭제 \n 5) 종료\n");

scanf("%d",&nember);

switch(nember)

{

case 1:

Make_Node(&head);

break;


case 2:

Print_Node(head);

break;


case 3:

Fine_Node(head);

break;


case 4:

delete_Node(&head);

break;


case 5:

return 0;

break;

}

}

}


void Make_Node(NodePointer *temp)

{

int i;

NodePointer item;

NodePointer cur  = *temp;


item = (NodePointer)malloc(sizeof(Node));

printf("이름 입력 : ");

scanf("%s", item->name);

printf("나이 입력 : ");

scanf("%d", &item->age);


item->link = NULL;


if(*temp == NULL)

{

*temp = item;

return ;

}


else

{

for( ; ;  )

{

if((cur)->link == NULL)

{

(cur)->link  = item;

break;

}

cur = cur->link;


}


}

}


void Print_Node(NodePointer temp)

{

for( ; temp != NULL ;temp = temp->link)

{

printf("이름 : %s  ", temp->name);

printf("나이 : %d  \n",temp->age);

}

}


void Fine_Node(NodePointer temp)

{

char findname[30];

printf("찾을 사람 이름 입력 :");

scanf("%s",findname);


for( ; temp != NULL ;temp = temp->link)

{

if(strcmp(temp->name,findname)== 0)

{

printf("이름 : %s  ", temp->name);

printf("나이 : %d  \n",temp->age);

break;

}

}

if(temp == NULL)

{

printf("찾는사람 없습니다.\n");

}

}


void delete_Node(NodePointer *node)

{

char findname[30];

NodePointer temp = *node;

NodePointer item;

printf("지울 사람 이름 입력 :");

scanf("%s",findname);


for( ; temp != NULL ;temp = temp->link)

{

if(strcmp(temp->name,findname)== 0)

{

printf("이름 : %s  ", temp->name);

printf("나이 : %d  \n",temp->age);

item->link = temp->link;


free(temp);

break ;

}


item = temp;

}

if(temp == NULL)

{

printf("찾는사람 없습니다.\n");

}

}



반응형