본문 바로가기

데이터구조/소스코드

OJ 1165

반응형
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

// 1165
struct node
{
	char *city;
	int num;
	struct node *prev;
	struct node *next;
};
struct node *head = 0;

void addToDLL(char *_city)
{
	struct node *cur;
	cur = (struct node *)malloc(sizeof(struct node));
	cur->next = cur->prev = 0;
	cur->city = (char *)malloc(strlen(_city) + 1);
	strcpy(cur->city, _city);

	int cnt = 0;
	for (int i = 0; i < strlen(cur->city); i++)
	{
		switch (cur->city[i])
		{
		case 'a':
		case 'e':
		case 'i':
		case 'o':
		case 'u':
			cnt++;
			break;
		}
	}
	cur->num = cnt;

	if (head == 0)
	{
		head = cur;
		return;
	}
	else
	{
		struct node *temp = head;

		while (temp->num > cur->num)
		{
			if (temp->next == 0)
			{
				cur->prev = temp;
				temp->next = cur;
				return;
			}
			else
			{
				temp = temp->next;
			}
		}

		// temp is the one 
		// having the same number of vowels.
		while (cur->num == temp->num &&
			(cur->city, temp->city) > 0)
		{
			if (temp->next == 0)
			{
				cur->prev = temp;
				temp->next = cur;
				return;
			}
			else
			{
				temp = temp->next;
			}
		}

		// must insert cur before temp
		if (head == temp)
		{
			cur->next = temp;
			temp->prev = cur;
			head = cur;
			return;
		}
		else
		{
			cur->next = temp;
			cur->prev = temp->prev;
			temp->prev->next = cur;
			temp->prev = cur;
			return;
		}
	}
}

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

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

	showALL();
	return 0;
}
반응형
LIST

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

OJ 1160  (0) 2016.04.25
OJ 1165  (0) 2016.04.25
OJ 1164  (0) 2016.04.25
OJ 1162  (0) 2016.04.25
Stack handling string  (0) 2016.04.14