新手求助:C Primer Plus书中高级数据一章中抽象数据类型(ADT)的问题

qq_30355845 2015-08-04 04:04:37
/* 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; //这里把一个结构体 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


根据 typedef Node * List 这个定义, List实际上就是个 Node结构体的指针了是吗?但是我在后面的代码中又看到了这样的变量声明和函数参数传递。 List movies; InitializeList(&movies); 既然根据前面的typedef定义List本来就是一个指针了,也就是说变量movies本身就是个Node结构体指针。那么,为何把它当做函数参数传递时还要用&movies的形式呢?谢谢!
...全文
205 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
ForestDB 2015-08-07
  • 打赏
  • 举报
回复
void foo(int * a) { *a = 1; } int a = 0; foo(&a); LZ还能解释这里的代码?
温_2013 2015-08-06
  • 打赏
  • 举报
回复
InitializeList这个函数传递的本身就是二级指针,因此 对于List movies, movies是结构指针; InitializeList(&movies)才是正确的
fly_dragon_fly 2015-08-04
  • 打赏
  • 举报
回复
跟定义匹配 void InitializeList(List * plist); plist就是List *不是List
jiqiang01234 2015-08-04
  • 打赏
  • 举报
回复
先看二级指针的章节,再回过头来看

33,311

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 新手乐园
社区管理员
  • 新手乐园社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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