首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

c primer上的链表事例

2013-06-25 
c primer上的链表例子数据类型定义struct film{char title[TSIZE] int rating } typedef struct film I

c primer上的链表例子
数据类型定义

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

typedef struct film Item ;

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

typedef Node * List ;


初始化

List movies ;

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

初始化的时候,为什么不要初始化Node里成员?

而队列的例子却初始化了里面的成员

typedef struct node
{
    int item ;
    struct node *next ;
} Node ;

typedef struct queue
{
    Node * front ;
    Node * rear ;
    int items ;
} Queue ;

    Queue line ;

void InitializeQueue(Queue *pq)
{
    pq->front = pq->rear = NULL ;
    pq->items = 0 ;
}


还发现链表的声明其实是,Node *move
而队列的声明是,Queue line
这是为什么呢?不都是要指向下一个成员的吗

另外问个其他的
eclipse调试的时候为什么不能实时显示pringf的信息,那里可以设置吗?

回复的就有分,最好详细点
[解决办法]
1、(初始化的时候,为什么不要初始化Node里成员?)
初始化的时候是空的,没有成员,就不需要初始化了。只需要把指针置空。
队列至少还有个头节点成员。所以要初始化。
2、
(还发现链表的声明其实是,Node *move
 而队列的声明是,Queue line
 这是为什么呢?不都是要指向下一个成员的吗)
queue是个头,头里面指向一个链表,但那个链表是对使用者透明的。使用的人只有关系头那个节点就可以了。
即使为空,也需要维护,所以需要定义头,分配内存。而链表空的时候可以什么都没有,就个指针。
[解决办法]
因为链表为空的话,其实你也读不了那个数据啊,初始化就是画蛇添足了。
那个queue只是一个头结点,还没有做啥。
[解决办法]
队列有各种各样的初始化。就看有没理解本质了,是否需要

typedef int datatype;

typedef struct node
{
    datatype data;
    struct node *next;
}node,*link;

typedef struct queue
{
    link front;
    link rear;
}queue,*queue_link;

void queue_init(queue_link *Q)
{
    link p;
    *Q = (queue_link)malloc(sizeof(queue));
    if(NULL == *Q)
    {
        perror("malloc");
        exit(-1);
    }

    p = (link)malloc(sizeof(node));
    if(NULL == p)
    {
        perror("malloc");
        exit(-1);
    }
    p->next = NULL;



    (*Q)->front = (*Q)->rear = p;
}

热点排行