#include <stdio.h>

#include <string.h>

#include <stdlib.h>


typedef struct type_node *type_ptr;

typedef struct type_node {

char name[20];

int num;

type_ptr link;

}our_node;

type_ptr search_ins_position(type_ptr head, char *str);


void main() {

int i = 0;

type_ptr head, np, prev;

char str[20];

int numl, res;

FILE *fp;

fp = fopen("123.txt", "r");


head = NULL;

while (1) {

res = fscanf(fp, "%s %d", str, &numl);

if (res < 2)

break;

np = (type_ptr)malloc(sizeof(our_node));

strcpy(np->name, str);

np->num = numl;

if (!head) {

head = np;

np->link = NULL;

}

else {

prev = search_ins_position(head, str);

if (!prev) {

np->link = head;

head = np;

}

else {

np->link = prev->link;

prev->link = np;


}

}

}

while (1)

{

printf("%s %d\n", head->name, head->num);

if (head->link == NULL)

break;

head = head->link;

}

}

type_ptr search_ins_position(type_ptr head, char *str)

{

type_ptr temp = NULL;

if (strcmp(str, head->name) < 0)

return 0;

else

{

while (1)

{

if (strcmp(str, head->name) >= 0)

temp = head;

if (head->link == NULL)

break;

head = head->link;

}

return temp;

}

}


search_ins_position 함수를 만드는 건데


한번 만들어 봤는데 어떤가요..?