Advertisement
Guest User

Untitled

a guest
May 20th, 2018
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.50 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3.  
  4. #include <sys/types.h>
  5. #include <sys/ipc.h>
  6. #include <sys/msg.h>
  7.  
  8. #define MSGSIZE 100
  9. #define MSGTYPE1 1
  10. #define MSG_REGQ 2
  11. #define MSGTYPE3 3
  12. #define MSG_LEAVEQ 4
  13. #define ERRCODE -1
  14.  
  15. #define ALLTYPES 0
  16.  
  17.  
  18. struct Node {
  19.   pid_t member;
  20.   struct Node* next;
  21. };
  22.  
  23. typedef struct Node Node;
  24.  
  25. typedef struct mmbrlist{
  26.   Node* head;
  27.   Node* tail;
  28. } mmbrlist;
  29.  
  30. struct message {
  31.   long mtype;
  32.   pid_t pid;
  33.   char mtext[MSGSIZE];
  34. };
  35. typedef struct message message;
  36.  
  37. mmbrlist initmmbrList() {
  38.   Node* head = (Node*)malloc(sizeof(Node));
  39.   head->member = 0;
  40.   head->next = NULL;
  41.   mmbrlist list = {head, head};
  42.   return list;
  43. }
  44.  
  45. int ml_pushback(mmbrlist* list, pid_t item){
  46.   Node* toadd = (Node*)malloc(sizeof(Node));
  47.  
  48.   if (NULL == toadd) {
  49.     return EXIT_FAILURE;
  50.   }
  51.  
  52.   toadd->next = NULL;
  53.   toadd->member = item;
  54.   list->tail->next = toadd;
  55.   list->tail = toadd;
  56.   return EXIT_SUCCESS;
  57. }
  58.  
  59. void ml_removenode(Node* prev){
  60.   Node* todel = prev->next;
  61.   prev->next = todel->next;
  62.   free(todel);
  63. }
  64. void ml_remove(mmbrlist* list, pid_t item){
  65.   for (Node* curNode = list->head;
  66.     curNode != NULL;
  67.     curNode = curNode->next)
  68.   {
  69.     if (curNode->next == NULL)
  70.       break;
  71.     if (curNode->next->member == item)
  72.       ml_removenode(curNode);
  73.   }
  74. }
  75.  
  76. void ml_clear(Node* head){
  77.   if (NULL == head)
  78.     return;
  79.  
  80.   ml_clear(head->next);
  81.   free(head);
  82. }
  83.  
  84. int ml_isempty(mmbrlist list) {
  85.   return list.head->next == NULL;
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement