April 11, 2009

Bir is mulakatinda basiniza gelebilecekler 2

Filed under: General — Tags: , , — admin @ 2:01 pm

Mulakata girdigim Meshur firmada sorulan sorulardan biri de, herbir nodu

1
2
3
4
5
6
struct list_s {
    char *name;
    char *surname;
    unsigned index;
    struct list_s *next;
};

seklinde olan bir linked list in icerisindeki butun elemanlari ile nasil kopyalanacagiydi.
(bu islemei yapan bir C fonksiyonu yazilacak)

Asgidaki kodda uygulamasini gorebilirsiniz.
Uygulamanin kaynak kodunu buradan indirebilirsiniz.

hede@hodo ~ $ gcc -o llist llist.c

seklinde derleyeyin

hede@hodo ~ $ ./llist

ile calistirin

Yazilim firmalarinda mulakada gireceklere basarilar.

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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
/*
Bolat Dinc Linked List example.
31.03.2009
bolatdinc-{at}-gmail.com
for more information look at http://en.wikipedia.org/wiki/Linked_List
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
 
struct list_s {
	char 	*name;
	char 	*surname;
	unsigned index;
	struct list_s *next;
};
 
/*
*
-------------------------------Prothotype Declerations--------------------------
*
*/
 
struct list_s*
list_s_add_front(struct list_s **p, char *name, char *surname);
 
struct list_s*
list_s_copy(struct list_s **new_list, struct list_s **source_list);
 
struct list_s*
list_copy_node(struct list_s **new_node, struct list_s **source_node);
 
void 
list_print(struct list_s *n);
 
void
list_remove(struct list_s **p);
 
int
main(int argc, char **argv){
	struct list_s *list_deneme = NULL;
	struct list_s *list_copy   = NULL;
        int i;
	for (i = 0; i<1000; i++) { /*iterative test to detect memory leak! */
		list_s_add_front(&list_deneme, "Fonksiyon", "Pointeri");
		list_s_add_front(&list_deneme, "Implicit", "Cast");
		list_s_add_front(&list_deneme, "Posix", "4.3");
		list_s_add_front(&list_deneme, "Select", "Poll");
		list_s_add_front(&list_deneme, "Volatile", "Constant");	
		list_s_add_front(&list_deneme, "Bitwise", "AndOr");
 
		list_s_copy(&list_copy ,&list_deneme);
 
		printf("\n----list_deneme-----\n");
		list_print(list_deneme);
		printf("\n----list_copy-----\n");
		list_print(list_copy);
 
		list_remove(&list_deneme);
		list_remove(&list_deneme);
		list_remove(&list_deneme);
		list_remove(&list_deneme);
		list_remove(&list_deneme);
		list_remove(&list_deneme);
		list_remove(&list_copy);
		list_remove(&list_copy);
		list_remove(&list_copy);
		list_remove(&list_copy);
		list_remove(&list_copy);
		list_remove(&list_copy);
	}
 
	printf("\n----list_deneme-----\n");
	list_print(list_deneme);
	printf("\n----list_copy-----\n");
	list_print(list_copy);
 
	return 0;
}
 
/*
Function name	: struct list_s* 
		  list_s_add_front(struct list_s **p, char *name, char *surname);
Parameters	: struct list_s **p 	-> A pointer of the linked list;
		  char *name 		-> A pointer of the name field of the node
		  char *surnaname 	-> A pointer of the surname field of the node		  
Return Value	: A pointer of the linked list that heve been created.
Description	: This function creates a linked list by adding nodes at the front
		  of the linked list.
*/
struct list_s*
list_s_add_front(struct list_s **p, char *name, char *surname){
	char *fname = "list_s_add_front()";
	/*Create new node at the front of the linked list*/
	struct list_s * n = (struct list_s*) malloc(sizeof(struct list_s));
	if(n == NULL){
		printf("Memory allocation error in %s line: %d\n", fname , __LINE__-2);
		exit(-1);
	}
 
	n->next = *p;
	*p = n;
 
	/*Rezerve memory location for 'name' field*/
	n->name = (char*) malloc(strlen(name)+1);
	if(n->name == NULL){
		printf("Memory allocation error in %s line: %d\n", fname , __LINE__-2);
		exit(-1);
	}
 
	/*Rezerve memory location for 'surname' field*/
	n->surname = (char*) malloc(strlen(surname)+1);
	if(n->surname == NULL){
		printf("Memory allocation error in %s line: %d\n", fname , __LINE__-2);
		exit(-1);		
	}
 
	strcpy(n->name, name);
	strcpy(n->surname ,surname);
	return *p;
}
 
/*
Function name	: struct list_s* 
		  list_s_copy(struct list_s **s1, struct list_s **s2);
Parameters	: struct list_s **s1 -> A pointer of the linked list which points 
		  the copy of the linked list.
		  struct list_s **s2 -> A pointer of the linked list which will be copied.
Return Value	: A pointer of the linked list which points the copy of the linked list.
Description	: This function creates a linked list that entirely copy of 'source_list'.
*/
struct list_s*
list_s_copy(struct list_s **new_list, struct list_s **source_list){
	/*
	char *fname  = "list_s_copy()";
	*/
	struct list_s *tmp_new_list 	= NULL;
	struct list_s *tmp_source_list	= *source_list;
 
	if (tmp_source_list == NULL){
		*new_list = NULL;
		return NULL;
	}
 
	for(;tmp_source_list != NULL;){
		/*define head pointer of the copied linked list*/
		if (*new_list == NULL){
			list_copy_node(new_list, source_list);
		/*reserve head pointer of the copied linked list*/
			tmp_new_list = *new_list;
		}else{
			list_copy_node(&(tmp_new_list->next), &(tmp_source_list->next));
			tmp_new_list = tmp_new_list->next;
			tmp_source_list = tmp_source_list->next;
		}
	}
 
	return *new_list;
}
 
/*
Function name	: struct list_s* 
		  list_copy_node(struct list_s **s1, struct list_s **s2);
Parameters	: struct list_s **s1 -> A pointer of copied node.
		  struct list_s **s2 -> A pointer of node which will be copied.
Return Value	: A pointer of copied node.
Description	: This function creates a node then copies the source node into new node.
*/
struct list_s*
list_copy_node(struct list_s **new_node, struct list_s **source_node){
	char *fname = "list_copy_node()";
 
	if (*source_node == NULL){
		*new_node = NULL;
		return NULL;
	}else{
		*new_node = (struct list_s*) malloc(sizeof(struct list_s));
		if(*new_node == NULL){
			printf("Memory allocation error in %s line: %d\n", fname, __LINE__-2);
			exit(-1);
		}
 
		(*new_node)->name = (char*) malloc(strlen((*source_node)->name)+1);
		if((*new_node)->name == NULL){
			printf("Memory allocation error in %s line: %d\n", fname , __LINE__-2);
			exit(-1);
		}
 
		(*new_node)->surname = (char*) malloc(strlen((*source_node)->surname)+1);
		if((*new_node)->surname == NULL){
			printf("Memory allocation error in %s line: %d\n", fname , __LINE__-2);
			exit(-1);
		}
 
		strcpy((*new_node)->name, (*source_node)->name);
		strcpy((*new_node)->surname, (*source_node)->surname);
		(*new_node)->next = NULL;
 
		return *new_node;
	}
}
 
/*
Function name	: void list_print(struct list_s *n);
Parameters	: struct list_s *n -> A pointer of the linked list 
		  to be printed from head to taill.
Return Value	: void
Description	: Prints the linked list which passed from input parameter.
*/
void 
list_print(struct list_s *llist){
	if (llist == NULL)	{
		printf("list is empty\n");
	}
 
	while (llist != NULL){
		printf("print %p %p %s %s\n", llist, llist->next, llist->name, llist->surname);;
		llist = llist->next;
	}
}
 
/*
Function name	: void list_remove(struct list_s **p);
Parameters	: struct list_s **p -> A pointer of the linked list 
		  to be removed its head node.
Return Value	: void
Description	: Removes head node of the linked list which passed from parameter.
*/
void 
list_remove(struct list_s **llist_head){
	if (*llist_head != NULL)	{
		struct list_s *n = *llist_head;
		*llist_head = (*llist_head)->next;
 
		free(n->name); /*delete dynamic node fields*/
		free(n->surname);
		free(n); /*delete node*/
	}
}
Older Posts »

Powered by WordPress