#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct _node {
int sNumber;
char sName[20];
char sCity[20];
struct _node* prev;
struct _node* next;
}typedef NODE;
struct _list {
int size;
NODE* head;
NODE* end;
}typedef DLL;
typedef NODE* pNode;
pNode makeNode ( int number, char * name, char * city );
void printList ( pNode start );
void initialize ( DLL *dl );
void append ( DLL *l, int a, char *b, char *c );
int search ( pNode t, int number );
pNode deleteNode( pNode list, int n );
int main (){
int num;
DLL list;
int index;
initialize ( &list );
int searchNum;
int inputNum;
char inputName[20];
char inputReg[20];
append ( &list, 201201496, "Kwon tae won", "Kimpo" );
append ( &list, 201201234, "Kim han dong", "Seoul" );
append ( &list, 201204567, "Kim min kyo", "Busan" );
append ( &list, 201201496, "Park ji sung", "Daejeon" );
while ( 1 ){
printf ( "1.search, 2.append, 3.delete, 4.output, 5.exit\n" );
printf ( "하실 명령을 입력해주세요:" );
fflush ( stdin );
scanf ( "%d", &num );
switch ( num )
{
case 1:
printf ( "검색항 학번을 입력해주세요:" );
scanf ( "%d", &searchNum );
index = search ( list.head, searchNum );
if ( index != -1 )
printf ( "index of %d:%d\n", searchNum, index );
else
printf ( "Not found!\n" );
continue;
case 2:
printf ( "추가할 학생의 학번을 입력해주세요:" );
scanf ( "%d", &inputNum );
printf ( "추가할 학생의 이름을 입력해주세요:" );
scanf("%s", &inputName);
//scanf로 입력을 받을시, 공백을 처리하지 못하기에, gets을 이용해서 줄 입력을 받는다.
//이전 버퍼에 있던 값을 지우기 위해 stdin을 flush해준다.
printf ( "추가할 학생의 주소를 입력해주세요:" );
scanf ( "%s", &inputReg );
append ( &list, inputNum, inputName, inputReg );
continue;
case 3:
printf ( "삭제할 학생의 학번을 입력해주세요:" );
scanf ( "%d", &searchNum );
list.head = deleteNode ( list.head, searchNum );
continue;
case 4:
printList ( list.head );
continue;
case 5:
printf ( "종료합니다" );
return 0;
}
}
}
void printList ( pNode start )
{
while ( start != NULL )
{
printf ( " ss\n", start->sNumber, start->sName, start->sCity );
start = start->next;
}
printf ( "\n" );
}
void initialize ( DLL *dl ){
dl->size = 0;
dl->head = NULL;
dl->end = NULL;
}
pNode makeNode ( int number, char * name, char * city ){
pNode temp;
temp = ( pNode ) malloc ( sizeof( NODE ) );
temp->sNumber = number;
strcpy ( temp->sName, name );
strcpy ( temp->sCity, city );
temp->prev = NULL;
temp->next = NULL;
return temp;
}
void append ( DLL *l, int a, char *b, char *c ){
pNode temp;
temp = makeNode ( a, b, c );
if ( l->size == 0 || l->head == NULL ){
l->head = temp;
l->end = temp;
}
else
l->end->next = temp;
temp->prev = l->end;
l->end = temp;
l->size++;
}
int search ( pNode t, int number ){
int index = 0;
while ( t != NULL ){
if ( t->sNumber == number )
return index;
t = t->next;
index++;
}
return -1;
}
pNode deleteNode( pNode t, int number )
{
pNode originalHead = t;
int index = 0;
pNode target = NULL;
while ( t != NULL ){
if ( t->sNumber == number )
{
target = t;
break;
}
t = t->next;
index++;
}
if ( target == NULL ){
printf ( "Not found!\n" );
return NULL;
}
if ( index == 0 )
{
pNode returnVal = target->next;
free ( target);
return returnVal;
}
else
{
if ( target->next == NULL ){
target->prev->next = NULL;
free ( target );
return originalHead;
}
else
{
pNode temp = target->prev;
target->prev->next = target->next;
target->next->prev = temp;
free ( target );
return originalHead;
}
}
}
댓글 0