practical of programming 第二章 链表
 #include <stdio.h>
#include <stdlib.h>
/* emalloc: malloc and report if error */
void *emalloc(size_t n){
void *p;
p = malloc(n);
if (p==NULL)
printf("malloc of %u bytes failed:",n);
return p;
}
typedef struct Nameval Nameval;
struct Nameval{
char *name;
int value;
Nameval *next; /* in list */
};
/* newitem: create new Namevalue item from name and value */
Nameval *newitem(char *name, int value){
Nameval *newp;
newp = (Nameval *)emalloc(sizeof(Nameval));
newp->name = name;
newp->value = value;
newp->next = NULL;
return newp;
}
/* addfront: add newp to front of listp */
Nameval *addfront(Nameval *listp, Nameval *newp){
newp->next = listp;
return newp;
}
/* addend: add newp to end of listp */
Nameval *addend(Nameval *listp, Nameval *newp){
Nameval *p;
if (listp == NULL)
return newp;
for (p = listp; p->next != NULL; p = p->next)
;
p->next = newp;
return listp;
}
/* lookup: sequential search for name in listp */
Nameval *lookup(Nameval *listp, char *name){
for (; listp != NULL; listp = listp->next)
if(strcmp(name, listp->name)==0)
return listp;
return NULL; /* no match */
}
/* 对链表中的所有元素执行相同函数操作,使用函数指针
第三个参数为操作函数的第二个参数 */
/* apply: execute fn for each element of listp */
void apply(Nameval *listp,
void (*fn)(Nameval*, void*), void *arg){
for (; listp!=NULL; listp = listp->next)
(*fn)(listp,arg);
}
/* printnv: 可以由apply使用的操作函数,输出元素项 */
void printnv(Nameval *p, void *arg){
char *fmt;
fmt = (char *)arg;
printf(fmt,p->name,p->value);
}
/* inccounter: increment counter *arg */
void inccounter(Nameval *p, void *arg){
int *ip;
ip = (int *)arg;
(*ip)++;
}
/* freeall: free all elements of listp */
void freeall(Nameval *listp){
Nameval *next; /* 释放前先将的的下一个元素存储到临时里,否则就找不到了 */
for (; listp!=NULL; listp = next){
next = listp->next;
/* assumes name is freed elsewhere */
free(listp);
}
}
/* delitem: delete first "name" from listp ,此处没有处理name域*/
Nameval *delitem(Nameval *listp, char *name){
Nameval *p, *prev;
prev = NULL;
for (p = listp; p != NULL; p = p->next){
if(strcmp(name,p->name)==0){
if(prev == NULL)
listp = p->next;
else
prev->next = p->next;
free(p);
return listp;
}
prev = p;
}
printf("delitem: %s not in listp", name);
return NULL; /* cant get here */
}
int main()
{
Nameval *nvlist=NULL;
Nameval *temp;
nvlist = addfront(nvlist, newitem("steve",0x0001));
nvlist = addfront(nvlist, newitem("bill",0x0003));
nvlist = addfront(nvlist, newitem("edison",0x0002));
nvlist = addfront(nvlist, newitem("sylar",0x0004));
nvlist = addfront(nvlist, newitem("peter",0x0005));
apply(nvlist,printnv,"%s: %x\n");
int n;
n = 0;
apply(nvlist,inccounter,&n);
printf("%d elements in nvlist\n",n);
if((temp=lookup(nvlist,"edison"))!=NULL)
printf("find ok! The id is %d named %s.\n",
temp->value,temp->name);
return 0;
}
    #include <stdlib.h>
/* emalloc: malloc and report if error */
void *emalloc(size_t n){
void *p;
p = malloc(n);
if (p==NULL)
printf("malloc of %u bytes failed:",n);
return p;
}
typedef struct Nameval Nameval;
struct Nameval{
char *name;
int value;
Nameval *next; /* in list */
};
/* newitem: create new Namevalue item from name and value */
Nameval *newitem(char *name, int value){
Nameval *newp;
newp = (Nameval *)emalloc(sizeof(Nameval));
newp->name = name;
newp->value = value;
newp->next = NULL;
return newp;
}
/* addfront: add newp to front of listp */
Nameval *addfront(Nameval *listp, Nameval *newp){
newp->next = listp;
return newp;
}
/* addend: add newp to end of listp */
Nameval *addend(Nameval *listp, Nameval *newp){
Nameval *p;
if (listp == NULL)
return newp;
for (p = listp; p->next != NULL; p = p->next)
;
p->next = newp;
return listp;
}
/* lookup: sequential search for name in listp */
Nameval *lookup(Nameval *listp, char *name){
for (; listp != NULL; listp = listp->next)
if(strcmp(name, listp->name)==0)
return listp;
return NULL; /* no match */
}
/* 对链表中的所有元素执行相同函数操作,使用函数指针
第三个参数为操作函数的第二个参数 */
/* apply: execute fn for each element of listp */
void apply(Nameval *listp,
void (*fn)(Nameval*, void*), void *arg){
for (; listp!=NULL; listp = listp->next)
(*fn)(listp,arg);
}
/* printnv: 可以由apply使用的操作函数,输出元素项 */
void printnv(Nameval *p, void *arg){
char *fmt;
fmt = (char *)arg;
printf(fmt,p->name,p->value);
}
/* inccounter: increment counter *arg */
void inccounter(Nameval *p, void *arg){
int *ip;
ip = (int *)arg;
(*ip)++;
}
/* freeall: free all elements of listp */
void freeall(Nameval *listp){
Nameval *next; /* 释放前先将的的下一个元素存储到临时里,否则就找不到了 */
for (; listp!=NULL; listp = next){
next = listp->next;
/* assumes name is freed elsewhere */
free(listp);
}
}
/* delitem: delete first "name" from listp ,此处没有处理name域*/
Nameval *delitem(Nameval *listp, char *name){
Nameval *p, *prev;
prev = NULL;
for (p = listp; p != NULL; p = p->next){
if(strcmp(name,p->name)==0){
if(prev == NULL)
listp = p->next;
else
prev->next = p->next;
free(p);
return listp;
}
prev = p;
}
printf("delitem: %s not in listp", name);
return NULL; /* cant get here */
}
int main()
{
Nameval *nvlist=NULL;
Nameval *temp;
nvlist = addfront(nvlist, newitem("steve",0x0001));
nvlist = addfront(nvlist, newitem("bill",0x0003));
nvlist = addfront(nvlist, newitem("edison",0x0002));
nvlist = addfront(nvlist, newitem("sylar",0x0004));
nvlist = addfront(nvlist, newitem("peter",0x0005));
apply(nvlist,printnv,"%s: %x\n");
int n;
n = 0;
apply(nvlist,inccounter,&n);
printf("%d elements in nvlist\n",n);
if((temp=lookup(nvlist,"edison"))!=NULL)
printf("find ok! The id is %d named %s.\n",
temp->value,temp->name);
return 0;
}

 
                
            
         浙公网安备 33010602011771号
浙公网安备 33010602011771号