본문 바로가기

데이터구조/소스코드

OJ 1132번 해답

반응형
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#include <stdio.h>
#include <stdlib.h>

struct node
{
	int data;
	struct node *prev;
	struct node *next;
};
struct node *head = 0;

struct statNode
{
	int data;
	int freq;
	struct statNode *prev;
	struct statNode *next;
};
struct statNode *sHead = 0;


// returns the pointer to the node which has bigger data than _data
// returns null if _data is the biggest
struct statNode *whoIsBigger(int _data)
{
	struct statNode *temp = sHead;

	while (temp != 0)
	{
		if (temp->data > _data)
		{
			break;
		}
		temp = temp->next;
	}
	return temp;

}

// returns the pointer to the node which has _data
// returns null if no node has _data
struct statNode* whereIsNode(int _data)
{
	struct statNode *temp = sHead;

	while (temp != 0)
	{
		if (temp->data == _data)
		{
			break;
		}
		temp = temp->next;
	}
	return temp;

}

void showStatDLL()
{
	struct statNode *temp = sHead;
	while (temp != 0)
	{
		printf("%d %d ", temp->data, temp->freq);
		temp = temp->next;
	}
}

void addToStatDLL(int _data)
{
	// 1. determine which node has _data
	struct statNode *temp;
	temp = whereIsNode(_data);
	if (temp != 0) // temp is pointing to the node having _data
	{
		temp->freq += 1;
		return;
	}
	else  // no node has _data
	{
		struct statNode *cur;
		cur = (struct statNode *)malloc(sizeof(struct statNode));
		cur->data = _data;
		cur->freq = 1;
		cur->next = cur->prev = 0;

		if (sHead == 0)
		{
			sHead = cur;
			return;
		}
		else
		{
			struct statNode *temp;
			temp = whoIsBigger(_data);
			if (temp == 0)  // _data is the biggest
			{
				// add to the last location
				struct statNode *final = sHead;
				while (final->next != 0)
				{
					final = final->next;
				}
				final->next = cur;
				cur->prev = final;
				return;

			}
			else
			{
				if (temp == sHead)  // _data is the smallest
				{
					// add to the first location
					cur->next = sHead;
					sHead = cur;
					cur->next->prev = cur;
					return;
				}
				else
				{
					// insert _data before temp
					cur->next = temp;
					cur->prev = temp->prev;
					temp->prev = cur;
					cur->prev->next = cur;
					return;
				}
			}
		}
	}
}



void addToDLL(int n)
{
	struct node *cur;
	cur = (struct node *)malloc(sizeof(struct node));
	cur->next = cur->prev = 0;
	cur->data = n;

	// check if there is no node
	if (head == 0)
	{
		head = cur;
		return;
	}

	// find the last node
	{
		struct node *temp = head;
		while (temp->next != 0)
		{
			temp = temp->next;
		}
		temp->next = cur;
		cur->prev = temp;
		return;
	}
}

void showDLL()
{
	struct node *cur = head;
	while (cur != 0)
	{
		printf("%d ", cur->data);
		cur = cur->next;
	}
}

int main(void)
{
	addToDLL(1);
	addToDLL(4);
	addToDLL(3);
	addToDLL(2);
	addToDLL(1);
	addToDLL(3);
	addToDLL(2);
	addToDLL(1);

	struct node *temp = head;
	while (temp != 0)
	{
		addToStatDLL(temp->data);
		temp = temp->next;
	}

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

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

String function review  (0) 2016.04.14
OJ 1131번 해답  (0) 2016.04.11
Online Judge 1117 문제의 해답  (0) 2016.04.04
Queue 구현코드 (큐)  (0) 2016.04.04
Stack 구현 소스  (0) 2016.04.04