본문 바로가기

데이터구조/소스코드

OJ 1162

반응형
SMALL
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

// DLL
struct node
{
	char *name;
	int num;
	struct node *next;
	struct node *prev;
};
struct node *head = 0;

void addToDLL(char *_name, int _num)
{
	struct node *cur;
	cur = (struct node *)malloc(sizeof(struct node));
	cur->next = cur->prev = 0;
	cur->num = _num;
	cur->name = (char *)malloc(strlen(_name) + 1);
	strcpy(cur->name, _name);

	if (head == 0)
	{
		head = cur;
		return;
	}
	else
	{
		// add cur to the end of DLL
		struct node *temp = head;
		while (temp->next != 0)
		{
			temp = temp->next;
		}
		temp->next = cur;
		cur->prev = temp;
	}
}

void findAndShow(char *_name)
{
	struct node *temp = head;
	while (temp->next != 0)
	{
		temp = temp->next;
	}

	while (temp != 0)
	{
		if (strcmp(temp->name, _name) == 0)
		{
			printf("%d ", temp->num);
		}
		temp = temp->prev;
	}
}


int main(void)
{
	int n;
	char name[100];
	int num;
	scanf("%d", &n);
	for (int i = 0; i < n; i++)
	{
		scanf("%d %s", &num, name);
		addToDLL(name, num);
	}

	scanf("%s", name);
	findAndShow(name);
	return 0;
}
반응형
LIST

'데이터구조 > 소스코드' 카테고리의 다른 글

OJ 1165  (0) 2016.04.25
OJ 1164  (0) 2016.04.25
Stack handling string  (0) 2016.04.14
SLL with character string member  (0) 2016.04.14
String function review  (0) 2016.04.14