双向链表 PushBack/PopFront

 

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. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值