当前位置:网站首页>One way linked list implementation -- counting

One way linked list implementation -- counting

2022-06-23 13:32:00 Jinxuehou column

  Realization of one-way linked list and metrological statistical function :

int16_t SingLinkList_Traverse(BOOKLT *bookHeadNode)
{
    int16_t  num = 0;// Count the number of books 
    BOOKLT * p = NULL; // Create a BOOKLT Pointer to structure type 
    p = bookHeadNode->next; //p Pointing to the head node next
    if(NULL == p->next)// Only the head node 
    {
       return -1;
    }
    while(p)//p->next Not empty , Continue traversing 
    {
       printf("%s,%s,%d\r\n",p->bookInfo.ISBN,p->bookInfo.bookName,p->bookInfo.price);// Output the data field contents of each node 
       p = p->next;// Point to the next node 
       num++; // Traverse a node , Quantity plus 1
    }
    return num;
}

 main.c The function is implemented as follows :

int main()
{
     int16_t num = 0;

     BOOKLT *myBookHeadNode;// Define a BOOKLT A pointer to a header node of type 

     myBookHeadNode  = SingLinkList_Create();// The above linked list memory has been released , Re create a header node 
     SingLinkList_TailInsert_method2(myBookHeadNode,sizeof(bookStock)/sizeof(BOOKINFO),bookStock);// Post insert method to insert nodes 

     SingLinkList_SelectSort(myBookHeadNode);// Select sort call 
     num = SingLinkList_Traverse(myBookHeadNode);// Traverse the one-way linked list and count the number of books 
     printf("\r\n The number of books is :%d\r\n",num);

     SingLinkList_Destory(myBookHeadNode);// Release the memory of each node and the memory of the header node 
     
     return 0;
}

Book information and Book measurement are as follows : 

  thus , The function of one-way linked list to count the number of books has been realized .

2022.06.19 junction .

原网站

版权声明
本文为[Jinxuehou column]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/174/202206231102035001.html

随机推荐