Advertisement
huutho_96

example

Dec 1st, 2015
310
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.89 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. typedef struct tagNode
  6. {
  7.     int value;
  8.     struct tagNode *pNext;
  9. } Node;
  10.  
  11. typedef struct tagQueue
  12. {
  13.     Node *pHead, *pTail;
  14. } Queue;
  15.  
  16. void CreateQueue(Queue *q)
  17. {
  18.     q->pHead = q->pTail = NULL;
  19. }
  20.  
  21. Node* CreateNode(int value)
  22. {
  23.     Node *p = (Node *)malloc(sizeof(Node));
  24.     if (!p) exit(1);
  25.  
  26.     p->pNext = NULL;
  27.     p->value = value;
  28.     return p;
  29. }
  30.  
  31. void Push(Queue *q, Node *pNew)
  32. {
  33.     if (q->pHead == NULL)
  34.     {
  35.         q->pHead = q->pTail = pNew;
  36.     }
  37.     else
  38.     {
  39.         q->pTail->pNext = pNew;
  40.         q->pTail = pNew;
  41.     }
  42. }
  43.  
  44. int Pop(Queue *q)
  45. {
  46.     Node *p = q->pHead;
  47.     if (q->pHead == NULL) return -1;
  48.     if (q->pHead == q->pTail)
  49.     {
  50.         q->pHead = q->pTail = NULL;
  51.         free(p);
  52.         return 0;
  53.     }
  54.  
  55.     q->pHead = p->pNext;
  56.     free(p);
  57.     return 1;
  58. }
  59.  
  60. void FreeQueue(Queue *q)
  61. {
  62.     Node *p;
  63.     while (q->pHead)
  64.     {
  65.         p = q->pHead;
  66.         q->pHead = p->pNext;
  67.         free(p);
  68.     }
  69.     q->pHead = q->pTail = NULL;
  70. }
  71.  
  72. int Front(Queue q)
  73. {
  74.     return q.pHead->value;
  75. }
  76.  
  77. int isEmpty(Queue q)
  78. {
  79.     return ((q.pHead == NULL) ? 1 : 0);
  80. }
  81.  
  82. void CopyQueue(Queue *q1, Queue q2)
  83. {
  84.     FreeQueue(q1);
  85.     Node *p = q2.pHead;
  86.     while (p)
  87.     {
  88.         Push(q1, CreateNode(p->value));
  89.         p = p->pNext;
  90.     }
  91. }
  92.  
  93.  
  94. typedef struct tagDListNode
  95. {
  96.     Queue value;
  97.     struct tagDListNode *pNext, *pPrev;
  98. } DListNode;
  99.  
  100. typedef struct tagDList
  101. {
  102.     DListNode *pHead, *pTail;
  103. } DList;
  104.  
  105.  
  106. void CreateDList(DList *l)
  107. {
  108.     l->pHead = l->pTail = NULL;
  109. }
  110.  
  111.  
  112. DListNode* CreateDListNode(Queue value)
  113. {
  114.     DListNode *p = (DListNode *)malloc(sizeof(DListNode));
  115.     if (!p) exit(1);
  116.  
  117.     p->pNext = p->pPrev = NULL;
  118.     p->value = value;
  119.     return p;
  120. }
  121.  
  122. void AddTail(DList *l, DListNode* pNew)
  123. {
  124.     if (l->pHead == NULL)
  125.     {
  126.         l->pHead = l->pTail = pNew;
  127.     }
  128.     else
  129.     {
  130.         l->pTail->pNext = pNew;
  131.         pNew->pPrev = l->pTail;
  132.         l->pTail = pNew;
  133.     }
  134. }
  135.  
  136. void PrintQueue(Queue q)
  137. {
  138.     Node *p = q.pHead;
  139.     if (!p) printf("0");
  140.     while (p)
  141.     {
  142.         printf("%d", p->value);
  143.         p = p->pNext;
  144.     }
  145. }
  146.  
  147. int Sum(Queue q)
  148. {
  149.     Node *p = q.pHead;
  150.     int sum = 0;
  151.     while (p)
  152.     {
  153.         sum += p->value;
  154.         p = p->pNext;
  155.     }
  156.     return sum;
  157. }
  158.  
  159. void PrintDList(DList l)
  160. {
  161.     DListNode *p = l.pHead;
  162.     while (p)
  163.     {
  164.         PrintQueue(p->value);
  165.         printf("\t\t\t%d\n", Sum(p->value));
  166.         p = p->pNext;
  167.     }
  168. }
  169.  
  170. void Input(DList *l)
  171. {
  172.     char str[1024];
  173.     Queue *q;
  174.     int i = 1, j;
  175.     int size;
  176.  
  177.  
  178.     puts("Viec nhap se ket thuc khi ban nhap xau rong");
  179.     while (1)
  180.     {
  181.         fflush(stdin);
  182.         printf("So thu %d:", i++);
  183.         gets(str);
  184.         size = strlen(str);
  185.         if (size == 0)
  186.             break;
  187.         j = 0;
  188.  
  189.         q = (Queue *)malloc(sizeof(Queue));
  190.         CreateQueue(q);
  191.  
  192.         while (j < size && str[j] == '0') j++;
  193.         while (j < size)
  194.             Push(q, CreateNode(str[j++] - 48));
  195.  
  196.         AddTail(l, CreateDListNode(*q));
  197.         q->pHead = q->pTail = NULL;
  198.     }
  199. }
  200.  
  201. int main()
  202. {
  203.     DList L;
  204.     Queue *q;
  205.     int i, j, m;
  206.  
  207.     CreateDList(&L);
  208.  
  209.     Input(&L);
  210.  
  211.     PrintDList(L);
  212.     return 40;
  213. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement