双向链表 PushBack/PopFront

来源:互联网 发布:nginx laravel 编辑:程序博客网 时间:2024/05/21 19:36
 

2 双向链表 PushBack/PopFront

 287人阅读 评论(0) 收藏 举报
 分类:
 
[cpp] view plain copy
  1. //实现调用和功能实现分开 双向链表  
  2. #include "stdafx.h"  
  3.   
  4. void BuildData()  
  5. {  
  6.     Person data;  
  7.     while(1){  
  8.         scanf("%d %s %s",&data.iId,  
  9.                 data.szName,data.szMajor);  
  10.         if(data.iId<1)  
  11.             break;  
  12.         PushBack(&data);  
  13.     }  
  14. }  
  15. void PrintData()  
  16. {  
  17.     Person data;  
  18.     while(PopFront(&data)){  
  19.         printf("id:%d,name:%s,major:%s\n",  
  20.             data.iId,data.szName,data.szMajor);  
  21.     }  
  22. }  
  23.   
  24. int main()  
  25. {  
  26.     BuildData();  
  27.     PrintData();  
  28.     return 0;  
  29. }  

 

[cpp] view plain copy
  1. //stack.h  
  2. #if !defined  __STACK_H__  
  3. #define __STACK_H__  
  4.   
  5. #include "stdafx.h"  
  6.   
  7. struct Person  
  8. {  
  9.     int iId;  
  10.     char szName[16];  
  11.     char szMajor[16];  
  12. };  
  13.   
  14. struct Node  
  15. {  
  16.     Person date;  
  17.     Node *pBack;  
  18.     Node *pNext;  
  19. };  
  20.   
  21. void PushBack(const Person *pDate);  
  22. bool PopFront(Person *pDate);  
  23.   
  24. #endif  

 

[cpp] view plain copy
  1. //stack.cpp  
  2. #include "stdafx.h"  
  3.   
  4. static  Node *g_pHead=NULL;  
  5. static  Node *g_taill=NULL;  
  6.   
  7. void PushBack(const Person *pDate)  
  8. {  
  9.     Node *pNode=(Node*)malloc(sizeof(Node));  
  10.     pNode->date=*pDate;  
  11.     pNode->pNext=g_pHead;  
  12.     g_pHead=pNode;    
  13. }  
  14.   
  15. bool PopFront(Person *pDate)  
  16. {  
  17.     Node *pDelete=g_pHead;  
  18.     if(g_pHead==NULL)  
  19.         return false;  
  20.     *pDate=g_pHead->date;  
  21.     g_pHead=g_pHead->pNext;  
  22.     free(pDelete);  
  23.     return true;  
  24. }  
0 0