Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- typedef struct tagNode
- {
- int value;
- struct tagNode *pNext;
- } Node;
- typedef struct tagQueue
- {
- Node *pHead, *pTail;
- } Queue;
- void CreateQueue(Queue *q)
- {
- q->pHead = q->pTail = NULL;
- }
- Node* CreateNode(int value)
- {
- Node *p = (Node *)malloc(sizeof(Node));
- if (!p) exit(1);
- p->pNext = NULL;
- p->value = value;
- return p;
- }
- void Push(Queue *q, Node *pNew)
- {
- if (q->pHead == NULL)
- {
- q->pHead = q->pTail = pNew;
- }
- else
- {
- q->pTail->pNext = pNew;
- q->pTail = pNew;
- }
- }
- int Pop(Queue *q)
- {
- Node *p = q->pHead;
- if (q->pHead == NULL) return -1;
- if (q->pHead == q->pTail)
- {
- q->pHead = q->pTail = NULL;
- free(p);
- return 0;
- }
- q->pHead = p->pNext;
- free(p);
- return 1;
- }
- void FreeQueue(Queue *q)
- {
- Node *p;
- while (q->pHead)
- {
- p = q->pHead;
- q->pHead = p->pNext;
- free(p);
- }
- q->pHead = q->pTail = NULL;
- }
- int Front(Queue q)
- {
- return q.pHead->value;
- }
- int isEmpty(Queue q)
- {
- return ((q.pHead == NULL) ? 1 : 0);
- }
- void CopyQueue(Queue *q1, Queue q2)
- {
- FreeQueue(q1);
- Node *p = q2.pHead;
- while (p)
- {
- Push(q1, CreateNode(p->value));
- p = p->pNext;
- }
- }
- typedef struct tagDListNode
- {
- Queue value;
- struct tagDListNode *pNext, *pPrev;
- } DListNode;
- typedef struct tagDList
- {
- DListNode *pHead, *pTail;
- } DList;
- void CreateDList(DList *l)
- {
- l->pHead = l->pTail = NULL;
- }
- DListNode* CreateDListNode(Queue value)
- {
- DListNode *p = (DListNode *)malloc(sizeof(DListNode));
- if (!p) exit(1);
- p->pNext = p->pPrev = NULL;
- p->value = value;
- return p;
- }
- void AddTail(DList *l, DListNode* pNew)
- {
- if (l->pHead == NULL)
- {
- l->pHead = l->pTail = pNew;
- }
- else
- {
- l->pTail->pNext = pNew;
- pNew->pPrev = l->pTail;
- l->pTail = pNew;
- }
- }
- void PrintQueue(Queue q)
- {
- Node *p = q.pHead;
- if (!p) printf("0");
- while (p)
- {
- printf("%d", p->value);
- p = p->pNext;
- }
- }
- int Sum(Queue q)
- {
- Node *p = q.pHead;
- int sum = 0;
- while (p)
- {
- sum += p->value;
- p = p->pNext;
- }
- return sum;
- }
- void PrintDList(DList l)
- {
- DListNode *p = l.pHead;
- while (p)
- {
- PrintQueue(p->value);
- printf("\t\t\t%d\n", Sum(p->value));
- p = p->pNext;
- }
- }
- void Input(DList *l)
- {
- char str[1024];
- Queue *q;
- int i = 1, j;
- int size;
- puts("Viec nhap se ket thuc khi ban nhap xau rong");
- while (1)
- {
- fflush(stdin);
- printf("So thu %d:", i++);
- gets(str);
- size = strlen(str);
- if (size == 0)
- break;
- j = 0;
- q = (Queue *)malloc(sizeof(Queue));
- CreateQueue(q);
- while (j < size && str[j] == '0') j++;
- while (j < size)
- Push(q, CreateNode(str[j++] - 48));
- AddTail(l, CreateDListNode(*q));
- q->pHead = q->pTail = NULL;
- }
- }
- int main()
- {
- DList L;
- Queue *q;
- int i, j, m;
- CreateDList(&L);
- Input(&L);
- PrintDList(L);
- return 40;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement