这个函数调用没有用到参数,那还传参数干什么?

mcmay 2014-04-16 03:50:50
各位大侠, 下面这个函数中都没有用到参数,那还设置参数干什么?代码分为三个部分,一个.h文件,两个.c文件。

Listing 17.3. The list.h Interface Header File
/* list.h -- header file for a simple list type */
#ifndef LIST_H_
#define LIST_H_
#include <stdbool.h> /* C99 feature */

/* program-specific declarations */
#define TSIZE 45 /* size of array to hold title */

struct film
{
char title[TSIZE];
int rating;
};

/* general type definitions */

typedef struct film Item;
typedef struct node
{
Item item;
struct node * next;
} Node;
typedef Node * List;

/* function prototypes */

/* operation: initialize a list */
/* preconditions: plist points to a list */
/* postconditions: the list is initialized to empty */
void InitializeList(List * plist);

/* operation: determine if list is empty */
/* plist points to an initialized list */
/* postconditions: function returns True if list is empty */
/* and returns False otherwise */
bool ListIsEmpty(const List *plist);

/* operation: determine if list is full */
/* plist points to an initialized list */
/* postconditions: function returns True if list is full */
/* and returns False otherwise */
bool ListIsFull(const List *plist);

/* operation: determine number of items in list */
/* plist points to an initialized list */
/* postconditions: function returns number of items in list */
unsigned int ListItemCount(const List *plist);

/* operation: add item to end of list */
/* preconditions: item is an item to be added to list */
/* plist points to an initialized list */
/* postconditions: if possible, function adds item to end */
/* of list and returns True; otherwise the */
/* function returns False */
bool AddItem(Item item, List * plist);

/* operation: apply a function to each item in list */
/* plist points to an initialized list */
/* pfun points to a function that takes an */
/* Item argument and has no return value */
/* postcondition: the function pointed to by pfun is */
/* executed once for each item in the list */
void Traverse (const List *plist, void (* pfun)(Item item) );

/* operation: free allocated memory, if any */
/* plist points to an initialized list */
/* postconditions: any memory allocated for the list is freed */
/* and the list is set to empty */
void EmptyTheList(List * plist);

#endif


Listing 17.4. The films3.c Program

/* films3.c -- using an ADT-style linked list */

/* compile with list.c */
#include <stdio.h>
#include <stdlib.h> /* prototype for exit() */
#include "list.h" /* defines List, Item */

void showmovies(Item item);

int main(void)
{
List movies;
Item temp;

/* initialize */
InitializeList(&movies);
if (ListIsFull(&movies))
{
fprintf(stderr,"No memory available! Bye!\n");
exit(1);
}

/* gather and store */
puts("Enter first movie title:");
while (gets(temp.title) != NULL && temp.title[0] != '\0')
{
puts("Enter your rating <0-10>:");
scanf("%d", &temp.rating);
while(getchar() != '\n')
continue;
if (AddItem(temp, &movies)==false)
{
fprintf(stderr,"Problem allocating memory\n");
break;
}
if (ListIsFull(&movies))
{
puts("The list is now full.");
break;
}
puts("Enter next movie title (empty line to stop):");
}

/* display */
if (ListIsEmpty(&movies))
printf("No data entered. ");
else
{
printf ("Here is the movie list:\n");
Traverse(&movies, showmovies);
}
printf("You entered %d movies.\n", ListItemCount(&movies));

/* clean up */
EmptyTheList(&movies);
printf("Bye!\n");

return 0;
}

void showmovies(Item item)
{
printf("Movie: %s Rating: %d\n", item.title, item.rating);
}


Listing 17.5. The list.c Implementation File
/* list.c -- functions supporting list operations */

#include <stdio.h>
#include <stdlib.h>
#include "list.h"

/* local function prototype */
static void CopyToNode(Item item, Node * pnode);

/* interface functions */
/* set the list to empty */

void InitializeList(List * plist)
{
* plist = NULL;
}

/* returns true if list is empty */
bool ListIsEmpty(const List * plist)
{
if (*plist == NULL)
return true;
else
return false;
}

/* returns true if list is full */
bool ListIsFull(const List * plist) //这个函数比较奇怪,明明参数,函数定义中却没有使用
{
Node * pt;
bool full;

pt = (Node *) malloc(sizeof(Node));
if (pt == NULL)
full = true;
else
full = false;
free(pt);

return full;
}

/* returns number of nodes */
unsigned int ListItemCount(const List * plist)
{
unsigned int count = 0;
Node * pnode = *plist; /* set to start of list */

while (pnode != NULL)
{
++count;
pnode = pnode->next; /* set to next node */
}

return count;
}

/* creates node to hold item and adds it to the end of */
/* the list pointed to by plist (slow implementation) */
bool AddItem(Item item, List * plist)
{
Node * pnew;
Node * scan = *plist;

pnew = (Node *) malloc(sizeof(Node));
if (pnew == NULL)
return false; /* quit function on failure */

CopyToNode(item, pnew);
pnew->next = NULL;
if(scan == NULL) /* empty list, so place */
*plist = pnew; /* pnew at head of list */
else
{
while (scan->next != NULL)
scan = scan->next; /* find end of list */
scan->next = pnew; /* add pnew to end */
}

return true;
}

/* visit each node and execute function pointed to by pfun */
void Traverse (const List * plist, void (* pfun)(Item item) )
{
Node * pnode = *plist; /* set to start of list */

while (pnode != NULL)
{
(*pfun)(pnode->item); /* apply function to item */
pnode = pnode->next; /* advance to next item */
}
}

/* free memory allocated by malloc() */
/* set list pointer to NULL */
void EmptyTheList(List * plist)
{
Node * psave;

while (*plist != NULL)
{
psave = (*plist)->next; /* save address of next node */
free(*plist); /* free current node */
*plist = psave; /* advance to next node */
}
}

/* local function definition */
/* copies an item into a node */
static void CopyToNode(Item item, Node * pnode)
{
pnode->item = item; /* structure copy */
}

谢谢指教!
...全文
249 7 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
一根烂笔头 2014-04-18
  • 打赏
  • 举报
回复
写代码的人初衷和结果不一致的表现!或者写成带参数接口,在不使用全局变量的情况下也能用一用
mcmay 2014-04-16
  • 打赏
  • 举报
回复
谢谢楼上各位大侠的提点,看来学一门语言还会牵涉到语言之外的很多其他知识。学习的过程也就是个不断扩展的过程。
lm_whales 2014-04-16
  • 打赏
  • 举报
回复
引用 4 楼 supermegaboy 的回复:
[quote=引用 3 楼 lm_whales 的回复:] [quote=引用 2 楼 supermegaboy 的回复:] bool ListIsFull(const List * plist) 应该是设计之初的想法,后来实现的时候没有使用,却忘了删掉。但这段代码废话很多,不够简洁,例如这个LitisFull,这样写就行了:

bool ListIsFull(const List * plist) 
{
    Node * pt = ( Node * )malloc( sizeof( Node ) );
    free( pt );
    return !pt;
}
++ LitisFull就是检查一下,还有没有内存可以分配,有的话就永远不会满,内存用完了就满了。 这个情况,几乎不会遇到满的情况,满了以后,估计程序啥也干不了了。 满了以后,任何其他分配内存的操作,都几乎不能执行,这样这个程序基本上也就跑不下去了。 对于链表,一般不用判满,除非限制节点数量。 [/quote] 这个ListisFull提不提供均可以,提供的话,遇到无内存可分配时,可通过自定义资源管理模块(C)或new_handler(C++)释放内存。[/quote] 哦,原来还可以这样处理,谢谢
飞天御剑流 2014-04-16
  • 打赏
  • 举报
回复
引用 3 楼 lm_whales 的回复:
[quote=引用 2 楼 supermegaboy 的回复:] bool ListIsFull(const List * plist) 应该是设计之初的想法,后来实现的时候没有使用,却忘了删掉。但这段代码废话很多,不够简洁,例如这个LitisFull,这样写就行了:

bool ListIsFull(const List * plist) 
{
    Node * pt = ( Node * )malloc( sizeof( Node ) );
    free( pt );
    return !pt;
}
++ LitisFull就是检查一下,还有没有内存可以分配,有的话就永远不会满,内存用完了就满了。 这个情况,几乎不会遇到满的情况,满了以后,估计程序啥也干不了了。 满了以后,任何其他分配内存的操作,都几乎不能执行,这样这个程序基本上也就跑不下去了。 对于链表,一般不用判满,除非限制节点数量。 [/quote] 这个ListisFull提不提供均可以,提供的话,遇到无内存可分配时,可通过自定义资源管理模块(C)或new_handler(C++)释放内存。
lm_whales 2014-04-16
  • 打赏
  • 举报
回复
引用 2 楼 supermegaboy 的回复:
bool ListIsFull(const List * plist) 应该是设计之初的想法,后来实现的时候没有使用,却忘了删掉。但这段代码废话很多,不够简洁,例如这个LitisFull,这样写就行了:

bool ListIsFull(const List * plist) 
{
    Node * pt = ( Node * )malloc( sizeof( Node ) );
    free( pt );
    return !pt;
}
++ LitisFull就是检查一下,还有没有内存可以分配,有的话就永远不会满,内存用完了就满了。 这个情况,几乎不会遇到满的情况,满了以后,估计程序啥也干不了了。 满了以后,任何其他分配内存的操作,都几乎不能执行,这样这个程序基本上也就跑不下去了。 对于链表,一般不用判满,除非限制节点数量。
飞天御剑流 2014-04-16
  • 打赏
  • 举报
回复
bool ListIsFull(const List * plist) 应该是设计之初的想法,后来实现的时候没有使用,却忘了删掉。但这段代码废话很多,不够简洁,例如这个LitisFull,这样写就行了:

bool ListIsFull(const List * plist) 
{
    Node * pt = ( Node * )malloc( sizeof( Node ) );
    free( pt );
    return !pt;
}
赵4老师 2014-04-16
  • 打赏
  • 举报
回复
写代码的人不知道怎么传参,我猜。

70,022

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧