第03次作业-栈和队列

可爱的小天才,由于放假了,导致作业没做,希望多给点分啊,爱你的小学弟。

2.PTA实验作业

1.1 题目1:7-1 jmu-字符串是否对称

 

 

1.3 代码截图

 

 

 

7.2 代码截图

7.1

 

 

 

 

总分   155    也就是两分

  1.  
  2.  *  list.h 
  3.  *  Created on: 2017年9月5日 
  4.  *      Author: jobschu 
  5.  */  
  6.   
  7. #ifndef __LIST_H__  
  8. #define __LIST_H__  
  9.   
  10. struct list {  
  11.     struct list *next;  
  12.     struct list *prev;  
  13. };  
  14. struct list_x {  
  15.     struct list *next;  
  16.     struct list *prev;  
  17. };  
  18. /*init list*/  
  19. void list_init(struct list *list);  
  20. /*free list*/  
  21. int list_empty(struct list *list);  
  22.   
  23. /*insert before link */  
  24. void list_insert(struct list *link, struct list *new_link);  
  25. /*insert before link */  
  26. void list_append(struct list *list, struct list *new_link);  
  27. /*insert after link*/  
  28. void list_prepend(struct list *list, struct list *new_link);  
  29. /*delete this link*/  
  30. void list_remove(struct list *link);  
  31.   
  32. #define list_entry(link, type, member) \  
  33.     ((type *)((char *)(link)-(unsigned long)(&((type *)0)->member)))  
  34.   
  35. /*get list head*/  
  36. #define list_head(list, type, member)       \  
  37.     list_entry((list)->next, type, member)  
  38.   
  39. #define list_tail(list, type, member)       \  
  40.     list_entry((list)->prev, type, member)  
  41.   
  42. #define list_next(elm, member)                  \  
  43.     list_entry((elm)->member.next, typeof(*elm), member)  
  44.   
  45. #define list_for_each_entry(pos, list, member)          \  
  46.     for (pos = list_head(list, typeof(*pos), member);   \  
  47.          &pos->member != (list);             \  
  48.          pos = list_next(pos, member))  
  49.   
  50. #endif  

 

 

msg.h

 

[cpp] view plain copy
 
  1. /* 
  2.  *  msg.h 
  3.  *  Created on: 2017年9月5日 
  4.  *      Author: jobschu 
  5.  */  
  6. #ifndef __MSG_H__  
  7. #define __MSG_H__  
  8.   
  9. #include "list.h"  
  10.   
  11. #define TID_MSG 0  
  12.   
  13. struct msg {  
  14.     struct list nodeHead;/*消息队列的头*/  
  15.     unsigned int type;  
  16.     unsigned int len;  
  17.     //unsigned int lock;  
  18.     void *pdata;  
  19. };  
  20. typedef struct msg msg_t;  
  21. typedef struct msg * pMsg_t;  
  22.   
  23. /*新建消息资源*/  
  24. pMsg_t msg_new(unsigned int type,unsigned char *data,int len);  
  25. /*消息队列在取得消息数据后,必须释放资源*/  
  26. void msg_free(pMsg_t pMsg);  
  27. #endif  

 

 

msgQueue.h

[cpp] view plain copy
 
  1. /* 
  2.  *  msgQueue.h 
  3.  *  Created on: 2017年9月5日 
  4.  *      Author: jobschu 
  5.  */  
  6.   
  7. #ifndef __MSG_QUEUE_H__  
  8. #define __MSG_QUEUE_H__  
  9.   
  10. struct msgQueue {  
  11.     struct list queueHead;  /*消息队列链表头*/  
  12.     unsigned int length;    /*消息队列长度*/  
  13.     unsigned char lock;     /*消息队列锁,防止在多线程里的互斥操作*/  
  14. };  
  15. typedef struct msgQueue msgQueue_t;  
  16. typedef struct msgQueue * pMsgQueue_t;  
  17.   
  18.   
  19. /*创建消息队列 成功返回消息队列handle 失败返回NULL*/  
  20. pMsgQueue_t msgQueue_create(void);  
  21.   
  22. /*从消息队列中获悉消息*/  
  23. pMsg_t msgQueue_getMsg(pMsgQueue_t q);  
  24.   
  25. /*将消息添加到队列尾部*/  
  26. int msgQueue_putMsg(pMsgQueue_t q,pMsg_t m);  
  27. /*判断消息队列是否为空 为空返回1 否则返回0*/  
  28. int msgQueue_isEmpty(pMsgQueue_t q);  
  29. /*清除消息队列*/  
  30. int msgQueue_delete(pMsgQueue_t q);  
  31.   
  32. #endif  

 

文件main.c

 

[cpp] view plain copy
 
  1. /* 
  2.  *  main.c 
  3.  *  Created on: 2017年9月5日 
  4.  *      Author: jobschu 
  5.  */  
  6. #include <stdio.h>  
  7. #include <stdlib.h>  
  8. #include <sys/ioctl.h>  
  9.   
  10. #include <sys/types.h>  
  11. #include <sys/stat.h>  
  12.   
  13. #include <errno.h>  
  14.   
  15. #include <fcntl.h>  
  16. #include <string.h>  
  17. #include <pthread.h>  
  18. #include <unistd.h>  
  19. #include <signal.h>  
  20.   
  21. #include "msg.h"  
  22. #include "msgQueue.h"  
  23.   
  24. static pMsgQueue_t youQueue;  
  25. static pMsgQueue_t myQueue;  
  26.   
  27. static pMsg_t myMsg;  
  28. static unsigned char buff[][10]={  
  29.         "1111111111","2222222222","3333333333","4444444444",  
  30.         "5555555555","6666666666","7777777777","8888888888"  
  31. };  
  32. /*测试一实现单个消息队列发送 单个消息队列接收测试*/  
  33. int test_0(void){  
  34.     int num = 0,i=0;  
  35.     printf("only for test!\n");  
  36.     /*创建消息队列*/  
  37.     myQueue = msgQueue_create();  
  38.     if(myQueue == NULL){  
  39.         printf("new msg queue err!\n");  
  40.         return -1;  
  41.     }  
  42.     printf("add msg to queue...\n");  
  43.     /*获取要模拟发送的消息长度*/  
  44.     num = sizeof(buff)/sizeof(buff[0]);  
  45.   
  46.     for(i=0;i<num;i++){  
  47.         /*新建消息*/  
  48.         myMsg = msg_new(0,buff[i],sizeof(buff[i]));  
  49.         /*将新建的消息推送到消息队列*/  
  50.         if(msgQueue_putMsg(myQueue,myMsg)){  
  51.             printf("put Msg err:[%d]\n",i);  
  52.         }  
  53.         //printf("add msg:%s to queue\n",buff[i]);  
  54.     }  
  55.     printf("get msg from queue...\n");  
  56.       
  57.       
  58.     while(!msgQueue_isEmpty(myQueue)){  
  59.         myMsg = msgQueue_getMsg(myQueue);  
  60.         if(myMsg == NULL||myMsg->len <=0 ||myMsg->pdata == NULL){  
  61.             continue;  
  62.         }  
  63.         printf("get msg:len:[%d],data:%s\n",myMsg->len,myMsg->pdata);  
  64.         /*将获取到的消息使用后释放资源*/  
  65.         msg_free(myMsg);  
  66.     }  
  67.     printf("main end!\n");  
  68.     /*删除消息队列*/  
  69.     msgQueue_delete(myQueue);  
  70.     return 0;  
  71. }  
  72.   
  73. /*测试方法二 通过主线程发送消息到消息队列,线程用来异步接收消息处理消息*/  
  74. static void *thread(void *str)  
  75. {  
  76.     int i = 0;  
  77.     unsigned char RecvBuff[256];  
  78.     unsigned int RecvLen = 0;  
  79.     while(1)  
  80.     {  
  81.         /*获取消息队列中的消息*/  
  82.         if(!msgQueue_isEmpty(myQueue)){  
  83.             myMsg = msgQueue_getMsg(myQueue);  
  84.   
  85.             if((myMsg != NULL) && (myMsg->len > 0) && (myMsg->pdata != NULL)){  
  86.                 printf("get msg:len:[%d],data:%s\n",myMsg->len,myMsg->pdata);  
  87.                 /*释放掉消息占有的空间*/  
  88.                 msg_free(myMsg);  
  89.             }  
  90.         }  
  91.         usleep(100000);//100ms  
  92.     }  
  93. }  
  94.   
  95. int test_1(void){  
  96.   
  97.     int num = 0,i=0;  
  98.     pthread_t pth;  
  99.   
  100.     printf("start the man process!\n");  
  101.     /*创建消息队列*/  
  102.     myQueue = msgQueue_create();  
  103.     if(myQueue == NULL){  
  104.         printf("new msg queue err!\n");  
  105.         return -1;  
  106.     }  
  107.   
  108.     printf("create new pthread!\n");  
  109.     /*创建接收消息队列的线程*/  
  110.     pthread_create(&pth,NULL,thread,NULL);  
  111.   
  112.     printf("add msg to queue...\n");  
  113.     /*获取要模拟发送的消息长度*/  
  114.     num = sizeof(buff)/sizeof(buff[0]);  
  115.   
  116.     for(i=0;i<num;i++){  
  117.         /*新建消息*/  
  118.         myMsg = msg_new(0,buff[i],sizeof(buff[i]));  
  119.         /*将新建的消息推送到消息队列*/  
  120.         if(msgQueue_putMsg(myQueue,myMsg)){  
  121.             printf("put Msg err:[%d]\n",i);  
  122.         }  
  123.         usleep(3000000);//1000ms=1s  
  124.     }  
  125. }  
  126.   
  127. /*创建两个并行的消息队列,交叉利用测试功能*/  
  128. int test_2(void){  
  129.     int num = 0,i=0;  
  130.     printf("only for test!\n");  
  131.     /*创建消息队列*/  
  132.     myQueue = msgQueue_create();  
  133.     if(myQueue == NULL){  
  134.         printf("new msg queue err!\n");  
  135.         return -1;  
  136.     }  
  137.     /*创建消息队列*/  
  138.     youQueue = msgQueue_create();  
  139.     if(youQueue == NULL){  
  140.         printf("new msg queue err!\n");  
  141.         return -1;  
  142.     }  
  143.   
  144.     printf("add msg to myQueue...\n");  
  145.     /*获取要模拟发送的消息长度*/  
  146.     num = sizeof(buff)/sizeof(buff[0]);  
  147.   
  148.     for(i=0;i<num/2;i++){  
  149.         /*新建消息*/  
  150.         myMsg = msg_new(0,buff[i],sizeof(buff[i]));  
  151.         /*将新建的消息推送到消息队列*/  
  152.         if(msgQueue_putMsg(myQueue,myMsg)){  
  153.             printf("put Msg err:[%d]\n",i);  
  154.         }  
  155.     }  
  156.     printf("add msg to youQueue...\n");  
  157.     for(i=num/2;i<num;i++){  
  158.         /*新建消息*/  
  159.         myMsg = msg_new(0,buff[i],sizeof(buff[i]));  
  160.         /*将新建的消息推送到消息队列*/  
  161.         if(msgQueue_putMsg(youQueue,myMsg)){  
  162.             printf("put Msg err:[%d]\n",i);  
  163.         }  
  164.     }  
  165.   
  166.     printf("get youQueue from queue...\n");  
  167.     /*将消息从消息队列中读取出来*/  
  168.     while(!msgQueue_isEmpty(youQueue)){  
  169.         myMsg = msgQueue_getMsg(youQueue);  
  170.         if(myMsg == NULL||myMsg->len <=0 ||myMsg->pdata == NULL){  
  171.             continue;  
  172.         }  
  173.         printf("get youQueue:len:[%d],data:%s\n",myMsg->len,myMsg->pdata);  
  174.         msg_free(myMsg);  
  175.     }  
  176.     printf("get myQueue from queue...\n");  
  177.     /*将消息从消息队列中读取出来*/  
  178.     while(!msgQueue_isEmpty(myQueue)){  
  179.         myMsg = msgQueue_getMsg(myQueue);  
  180.         if(myMsg == NULL||myMsg->len <=0 ||myMsg->pdata == NULL){  
  181.             continue;  
  182.         }  
  183.         printf("get myQueue:len:[%d],data:%s\n",myMsg->len,myMsg->pdata);  
  184.         msg_free(myMsg);  
  185.     }  
  186.   
  187.   
  188.     printf("main end!\n");  
  189.     /*删除消息队列*/  
  190.     msgQueue_delete(myQueue);  
  191.     msgQueue_delete(youQueue);  
  192.     return 0;  
  193. }  
  194.   
  195. int main(int argc,char **argv)  
  196. {  
  197.     //return test_0();  
  198.     //return test_1();  
  199.     return test_2();  
  200. }  

 

posted on 2018-04-07 20:31  .sugar  阅读(207)  评论(1编辑  收藏  举报

导航