抽象数据结构(ADT)中的初始化以及在调用函数中改变值的方法

来源:互联网 发布:c语言不等号怎么打出来 编辑:程序博客网 时间:2024/04/27 18:55

呵呵我弄了4个多小时才弄好,之前还看到清华大学的一本数据结构的书上(李春葆的数据结构教程上机实验指导)出现了这种结构,真的是无语,就是Linklist *&list,我在我的系统里面编译过了,通不过,当然了我用的是linux可能跟vc的实现还是有差别的。
希望有人可以告诉我Linklist *&list这个是什么东西。

现在看一个单链表的结构:

#define TSIZE      45    /* size of array to hold title  */
struct film
{
    char title[TSIZE];
    int rating;
};
typedef struct film Item;
typedef struct node
{
    Item item;
    struct node * next;
} Node;
typedef Node * List;

则以后定义结构题可以有两种方式一种是:Node 结构体名/Node *结构体名,另外一个就是List  结构体名/List  *结构体名
比如:Node *plist 或者List  *plist

可以这么理解,Node ==* List;那么Node定义的*plist在List定义为**plist。那么要在一个调用函数中改变plist的值只要使用指针即可,似乎是这样的,但是并不是这个样子的,使用Node *plist是无法实现正常的传值,而使用List  *plist就可以,同样把List  *plist转换成同样意义的Node **plist,也可以实现正常的传值。

初始化调用函数调用如下:(具体的四个c程序我在最后给出)
一://c1.c里面实现
void InitializeList(List  *plist)
{
         //plist=malloc(sizeof (List));
    * plist = NULL;
}
二://c2.c里面实现
void InitializeList(Node  **plist)
{
        // plist=(Node *)malloc(sizeof (List));
    * plist = NULL;
}
三://c3.c里面实现
Node *InitializeList(Node  *plist)
{
       // plist=(Node *)malloc(sizeof (List));
     plist = NULL;
    return plist;
}
四://c4.c里面实现
void InitializeList(Node  *plist)
{
       // plist=(Node *)malloc(sizeof (List));
     plist = NULL;
//    return plist;
}
一二调用函数都可以正常的传值而且是使用地址,他们没有问题。第四个调用的初始化函数明显就不可以实现正常的传值,要实现就要用第三个,他不是使用的地址而是直接的赋值调用。

为什么Node  *plist而Node  **plist就可以实现地址到值的变换?我认为主要是因为Node结构里面还有一个node结构,要实现对里面的全部地址的选取就要把两层都要起地址,故要用**两个指针才可以,但是有一个问题就出现了,对整个结构体的首地址是plist还是*plist?看c2.c程序就晓得了,是后者!!!!

在上面所说道的4个程序里面还有一个传值的函数,就是AddItem函数,给出c1和c4的实现;
c1:
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  */
   pnew= 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;
}
所使用的List *plist传值没有问题,但是要注意的一点就是List *plist本身定义的就是一个地址,那么在使用plist的时候要用*plist,而不是plist,跟前面Node  **plist的结构体的首地址的引用是一个意思!

c4.c:
Node *AddItem(Item item, Node  *plist)
{
    Node * pnew;
    Node * scan = plist;
    printf("scan=%d",scan);
    printf("plist=%d",plist);
    puts("");
    pnew = (Node *) malloc(sizeof(Node));
    if (pnew == NULL)
        return false;     /* quit function on failure  */
   pnew= CopyToNode(item, pnew);
    pnew->next = NULL;
    if (scan == NULL)          /* empty list, so place */
     { plist = pnew;         /* pnew at head of list */
      printf("plist=%d",plist);
      return plist;   
     }

    else
    {
        while (scan->next != NULL)
            scan = scan->next;  /* find end of list    */
        scan->next = pnew;      /* add pnew to end     */
    plist=scan;
    return plist;
    }
这里Node  *plist是没有办法实现地址到值的传递的,所以要用到值返回这个步骤,因此,我们在想改变具体的值的时候记得用指针,若是结构体里还有就要尝试变通之法了。

下面给出我的4个c程序,里面一些地方有点点变动,我想研究数据结构的同志们看看是应该有好处的哦。我在AddItem函数里面用了几个printf函数就是要跟踪初始化以及复制值以后plist的地址变化与否。其中c4是一个标准的可以编译但是不能正常运行的程序,用作“反面教材”吧。

c1.c

#include <stdbool.h> 
#include <stdio.h>
#include <stdlib.h>  

#define TSIZE      45    /* size of array to hold title  */
struct film
{
    char title[TSIZE];
    int rating;
};

typedef struct film Item;

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

typedef Node * List;

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);
static Node * CopyToNode(Item item, Node * pnode);
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);
}


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

/* interface functions   */
/* set the list to empty */
void InitializeList(List  *plist)
{
         //plist=malloc(sizeof (List));
    * 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  */
   pnew= 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 Node * CopyToNode(Item item, Node * pnode)
{
    pnode->item = item;  /* structure copy */
    return pnode;
}

c2.c
#include <stdbool.h> 
#include <stdio.h>
#include <stdlib.h>  

#define TSIZE      45    /* size of array to hold title  */
struct film
{
    char title[TSIZE];
    int rating;
};

typedef struct film Item;

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

typedef Node * List;

void InitializeList(Node **plist);
bool ListIsEmpty(const Node *plist);
bool ListIsFull(const Node *plist);
unsigned int ListItemCount(Node **plist);
bool AddItem(Item item, Node  ** plist);
void Traverse (Node *plist, void (* pfun)(Item item) );
void EmptyTheList(Node ** plist);
static Node * CopyToNode(Item item, Node * pnode);
void showmovies(Item item);

int main(void)
{
    Node *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);
}





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

/* interface functions   */
/* set the list to empty */
void InitializeList(Node  **plist)
{
        // plist=(Node *)malloc(sizeof (List));
    * plist = NULL;
}

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

/* returns true if list is full */
bool ListIsFull(const Node * 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(Node **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, Node  **plist)
{
    Node * pnew;
    Node * scan =* plist;
    printf("scan=%d",scan);
    printf("*plist=%d",*plist);
    puts("");
    pnew = (Node *) malloc(sizeof(Node));
    if (pnew == NULL)
        return false;     /* quit function on failure  */
   pnew= CopyToNode(item, pnew);
    pnew->next = NULL;
    if (scan == NULL)          /* empty list, so place */
     { *plist = pnew;         /* pnew at head of list */
      printf("*plist=%d",*plist);}

    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  (Node * 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(Node **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 Node * CopyToNode(Item item, Node * pnode)
{
    pnode->item = item;  /* structure copy */
    return pnode;
}

c3.c

#include <stdbool.h> 
#include <stdio.h>
#include <stdlib.h>  

#define TSIZE      45    /* size of array to hold title  */
struct film
{
    char title[TSIZE];
    int rating;
};

typedef struct film Item;

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

typedef Node * List;

Node *InitializeList(Node *plist);
bool ListIsEmpty(const Node *plist);
bool ListIsFull(const Node *plist);
unsigned int ListItemCount(Node **plist);
Node *AddItem(Item item, Node  * plist);
void Traverse (Node *plist, void (* pfun)(Item item) );
void EmptyTheList(Node ** plist);
static Node * CopyToNode(Item item, Node * pnode);
void showmovies(Item item);

int main(void)
{
    Node *movies;
    Item temp;


/* initialize       */
    movies=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 ((movies=AddItem(temp, movies))==NULL)
        {
            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);
}





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

/* interface functions   */
/* set the list to empty */
Node *InitializeList(Node  *plist)
{
       // plist=(Node *)malloc(sizeof (List));
     plist = NULL;
    return plist;
}

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

/* returns true if list is full */
bool ListIsFull(const Node * 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(Node **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)  */
Node *AddItem(Item item, Node  *plist)
{
    Node * pnew;
    Node * scan = plist;
    printf("scan=%d",scan);
    printf("plist=%d",plist);
    puts("");
    pnew = (Node *) malloc(sizeof(Node));
    if (pnew == NULL)
        return false;     /* quit function on failure  */
   pnew= CopyToNode(item, pnew);
    pnew->next = NULL;
    if (scan == NULL)          /* empty list, so place */
     { plist = pnew;         /* pnew at head of list */
      printf("plist=%d",plist);
      return plist;   
     }

    else
    {
        while (scan->next != NULL)
            scan = scan->next;  /* find end of list    */
        scan->next = pnew;      /* add pnew to end     */
    plist=scan;
    return plist;
    }
  
   // return true;
}

/* visit each node and execute function pointed to by pfun */
void Traverse  (Node * 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(Node **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 Node * CopyToNode(Item item, Node * pnode)
{
    pnode->item = item;  /* structure copy */
    return pnode;
}

c4.c

#include <stdbool.h> 
#include <stdio.h>
#include <stdlib.h>  

#define TSIZE      45    /* size of array to hold title  */
struct film
{
    char title[TSIZE];
    int rating;
};

typedef struct film Item;

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

typedef Node * List;

void InitializeList(Node *plist);
bool ListIsEmpty(const Node *plist);
bool ListIsFull(const Node *plist);
unsigned int ListItemCount(Node **plist);
Node *AddItem(Item item, Node  * plist);
void Traverse (Node *plist, void (* pfun)(Item item) );
void EmptyTheList(Node ** plist);
static Node * CopyToNode(Item item, Node * pnode);
void showmovies(Item item);

int main(void)
{
    Node *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 ((movies=AddItem(temp, movies))==NULL)
        {
            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);
}





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

/* interface functions   */
/* set the list to empty */
void InitializeList(Node  *plist)
{
       // plist=(Node *)malloc(sizeof (List));
     plist = NULL;
//    return plist;
}

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

/* returns true if list is full */
bool ListIsFull(const Node * 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(Node **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)  */
Node *AddItem(Item item, Node  *plist)
{
    Node * pnew;
    Node * scan = plist;
    printf("scan=%d",scan);
    printf("plist=%d",plist);
    puts("");
    pnew = (Node *) malloc(sizeof(Node));
    if (pnew == NULL)
        return false;     /* quit function on failure  */
   pnew= CopyToNode(item, pnew);
    pnew->next = NULL;
    if (scan == NULL)          /* empty list, so place */
     { plist = pnew;         /* pnew at head of list */
      printf("plist=%d",plist);
      return plist;   
     }

    else
    {
        while (scan->next != NULL)
            scan = scan->next;  /* find end of list    */
        scan->next = pnew;      /* add pnew to end     */
    plist=scan;
    return plist;
    }
  
   // return true;
}

/* visit each node and execute function pointed to by pfun */
void Traverse  (Node * 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(Node **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 Node * CopyToNode(Item item, Node * pnode)
{
    pnode->item = item;  /* structure copy */
    return pnode;
}
原创粉丝点击