קוד:
#include<stdio.h>
#include<string.h>
typedef struct phonenum{
char first_name[7],last_name[12],address[22],tel[11];
struct phonenum *nxt_phone;
};
struct phonenum *find_place(char *new_name,struct phonenum *rash){
struct phonenum *p,*save;
char found = 0;
for(save = p,p = rash; p && (!found) ;p = p->nxt_phone){
if(strcmp(new_name,p->last_name) < 0)
found = 1;
else
found = 0;
}
return(save);
}
struct phonenum *insert(struct phonenum **after){
struct phonenum *save;
save = *after;
*after = malloc(sizeof(struct phonenum));
if(after != NULL)
(*after)->nxt_phone = save;
return (*after);
}
void add_phone(char *f_nam,char *l_nam,char *address,char *tel,struct phonenum *rash){
struct phonenum *p;
p = find_place(l_nam,rash);
if(p == rash)
p = insert(&rash);
else
p = insert(&(p->nxt_phone));
strcpy(p->first_name,f_nam);
strcpy(p->last_name,l_nam);
strcpy(p->address,address);
strcpy(p->tel,tel);
}
void main(){
struct phonenum *first_phone;
first_phone = malloc(sizeof(struct phonenum));
first_phone->nxt_phone = NULL;
add_phone("shalom","hlkjv","Alon-5643","04-546584",first_phone);
printf("%s",first_phone->first_name);
getch();
}
קוד:
--------------------Configuration: connect_list - Win32 Debug--------------------
Compiling...
connect_list.c
G:\my program\connect_list.c(23) : warning C4013: 'malloc' undefined; assuming extern returning int
G:\my program\connect_list.c(23) : warning C4047: '=' : 'struct phonenum *' differs in levels of indirection from 'int '
G:\my program\connect_list.c(43) : warning C4047: '=' : 'struct phonenum *' differs in levels of indirection from 'int '
G:\my program\connect_list.c(47) : warning C4013: 'getch' undefined; assuming extern returning int
G:\my program\connect_list.c(11) : warning C4700: local variable 'p' used without having been initialized
Linking...
connect_list.exe - 0 error(s), 5 warning(s)