본문 바로가기

데이터구조/소스코드

OJ 1164

반응형
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
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct node
{
	char *name;
	struct node *next;
};
struct node *head = 0;

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

	if (head == 0)
	{
		head = cur;
		return;
	}
	else
	{
		struct node *temp = head;
		while (temp->name[0] != cur->name[0])
		{
			temp = temp->next;
			if (temp == 0)
			{
				break;
			}
		}

		// there is no one with the same alphabet
		if (temp == 0)
		{
			temp = head;
			while (temp->next != 0)
			{
				temp = temp->next;
			}
			temp->next = cur;
			return;
		}
		else // temp is the one with the same alpha.
		{
			cur->next = temp->next;
			temp->next = cur;
			return;
		}
	}
}

void showALL()
{
	struct node *temp = head;
	while (temp != 0)
	{
		printf("%s ", temp->name);
		temp = temp->next;
	}
}

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

	showALL();
	return 0;
}


#if 0
// 1162
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;
}

#endif


반응형
LIST

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

OJ 1165  (0) 2016.04.25
OJ 1165  (0) 2016.04.25
OJ 1162  (0) 2016.04.25
Stack handling string  (0) 2016.04.14
SLL with character string member  (0) 2016.04.14