#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct Node
{
int num;
char data[80];
struct Node *next;
} Node;
// 전역변수
Node head;
void add_node (int num, char *data)
{
Node *here = &head;
Node *temp;
temp = malloc(sizeof (Node));
temp->num = num; strcpy(temp->data, data); temp->next = NULL;
while(here->next)
{
if (here->num <= temp->num)
{
if (!here->next)
{
here->next = temp;
return;
}
else if (here->next->num > temp->num)
{
temp->next = here->next;
here->next = temp;
return;
}
}
here = here->next;
}
here->next = temp;
return;
};
void delete_node (int index)
{
Node *here = &head;
Node *temp;
while(here)
{
if (here->next->num == index)
{
temp = here->next;
here->next = here->next->next;
free(temp);
return;
}
here = here->next;
}
return;
};
int display(void)
{
int count = 1;
Node *here = head.next;
while(here)
{
printf ("%d : %d \t%s\n", count, here->num, here->data);
here = here->next;
count++;
}
return count;
}
int main()
{
int loop;
int t_n;
char t_d[80];
head.next = NULL;
head.num = -1;
strcpy(head.data, "");
for (loop = 0; loop < 5; loop++)
{
scanf("%d\n", &t_n);
gets(t_d);
add_node(t_n, t_d);
}
display();
delete_node(3);
delete_node(1);
printf ("\n");
display();
return 0;
}
#include <stdlib.h>
#include <string.h>
typedef struct Node
{
int num;
char data[80];
struct Node *next;
} Node;
// 전역변수
Node head;
void add_node (int num, char *data)
{
Node *here = &head;
Node *temp;
temp = malloc(sizeof (Node));
temp->num = num; strcpy(temp->data, data); temp->next = NULL;
while(here->next)
{
if (here->num <= temp->num)
{
if (!here->next)
{
here->next = temp;
return;
}
else if (here->next->num > temp->num)
{
temp->next = here->next;
here->next = temp;
return;
}
}
here = here->next;
}
here->next = temp;
return;
};
void delete_node (int index)
{
Node *here = &head;
Node *temp;
while(here)
{
if (here->next->num == index)
{
temp = here->next;
here->next = here->next->next;
free(temp);
return;
}
here = here->next;
}
return;
};
int display(void)
{
int count = 1;
Node *here = head.next;
while(here)
{
printf ("%d : %d \t%s\n", count, here->num, here->data);
here = here->next;
count++;
}
return count;
}
int main()
{
int loop;
int t_n;
char t_d[80];
head.next = NULL;
head.num = -1;
strcpy(head.data, "");
for (loop = 0; loop < 5; loop++)
{
scanf("%d\n", &t_n);
gets(t_d);
add_node(t_n, t_d);
}
display();
delete_node(3);
delete_node(1);
printf ("\n");
display();
return 0;
}
'Exercise & Quizz > C++' 카테고리의 다른 글
Stack calculator #4 - Infix - Postfix translation using Stask structure (0) | 2008.08.21 |
---|---|
Stack calculator #3 - Infix - Postfix translation using Stask structure (0) | 2008.08.21 |
Stack calculator #2 - Infix - Postfix translation using Stask structure (0) | 2008.08.21 |
Stack calculator #1 - Infix - Postfix translation using Stask structure (0) | 2008.08.21 |
A mouse in the Maze : 미로찾기 (0) | 2008.08.21 |