#include
#include
typedef struct node{
double distance;
char station[20];
struct node *next;
}node_t;
node_t *start;
node_t *NodeAllocate(void)
{
node_t *p = (node_t *)malloc(sizeof(node_t));
if(p == NULL){
fprintf(stderr,"\nERROR: Out of memory\n");
exit(1);
}
return p;
}
void NodeInsert(int ins, node_t *start, node_t *newp)
{
int i;
node_t *p;
for(p = start, i = 1;p != NULL;p = p->next, i++){
if(i == ins){
newp->next = p->next;
p->next = newp;
break;
}
}
printf("%d", i);
}
int main(void)
{
int count = 0, ins;
double d;
node_t *start, *p, *pre_p;
start = p = NodeAllocate();
while(1){
printf("\nPlease type DISTANCEdistance = d;
printf("\nPlease type STATION_NAME>");
scanf("%s", p->station);
p->next = NodeAllocate();
pre_p = p;
p = p->next;
}
}
pre_p->next = NULL;
printf("\n***** Normal display *****\n");
printf("distance station_name\n");
for(p = start;p != NULL;p = p->next){
printf("%8.1f %s\n", p->distance, p->station);
}
printf("\n");
printf("\nPlease type INSERT POSITIONcount){
printf("Ooops! Can't Insert(xox)/");
}else{
printf("\nPlease type DISTANCE distance = d;
printf("\nPlease type STATION_NAME station);
NodeInsert(ins, start, p);
}
}
printf("\n***** Normal display *****\n");
printf("distance station_name\n");
for(p = start;p != NULL;p = p->next)
printf("%8.1f %s\n", p->distance, p->station);
printf("\n");
return 0;
}
|