본문 바로가기

데이터구조/소스코드

SLL with character string member

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

struct node
{
	char *data;
	int sweetness;
	struct node *next;
};
struct node *head;

void showSLL()
{
	struct node *temp = head;
	while (temp != 0)
	{
		printf("%s\n", temp->data);
		temp = temp->next;
	}
}

void addToSLL(char *str, int _sweetness)
{
	struct node *cur;
	cur = (struct node *)malloc(sizeof(struct node));
	cur->next = 0;
	cur->sweetness = _sweetness;
	cur->data = (char *)malloc(strlen(str) + 1);
	strcpy(cur->data, str);

	if (head == 0)
	{
		head = cur;
		return;
	}
	else
	{
		struct node *temp = head;
		while (temp->next != 0)
		{
			temp = temp->next;
		}
		temp->next = cur;
		return;
	}
}
void destroySLL()
{
	struct node *temp = head;
	while (temp != 0)
	{
		head = head->next;
		free(temp->data);
		free(temp);
	}
}

void findInfo(char *_fruit)
{
	struct node *cur = head;
	while (cur != 0)
	{
		if (strcmp(cur->data, _fruit) == 0)
		{
			printf("%s : %d\n",
				cur->data, cur->sweetness);
			return;
		}
		cur = cur->next;
	}
	printf("There is no such %s\n", _fruit);

}

int main(void)
{
	int n;
	char str[100];
	scanf("%d", &n); // 4

	for (int i = 0; i < n; i++)
	{
		scanf("%s", str);
		addToSLL(str, i+10);
	}

	//showSLL();
	printf("Enter name of fruit");
	scanf("%s", str);
	findInfo(str);

	return 0;
}
반응형
LIST

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

OJ 1162  (0) 2016.04.25
Stack handling string  (0) 2016.04.14
String function review  (0) 2016.04.14
OJ 1131번 해답  (0) 2016.04.11
OJ 1132번 해답  (0) 2016.04.11