Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- struct Node
- {
- int heso, somu;
- Node *pNext;
- };
- struct List
- {
- Node *pHead, *pTail;
- };
- void AddHead(List &L, Node *p)
- {
- if (L.pHead == NULL)
- {
- L.pHead = L.pTail = p;
- }
- else
- {
- p->pNext = L.pHead;
- L.pHead = p;
- }
- }
- void CreateList(List &L)
- {
- L.pHead = L.pTail = NULL;
- }
- Node * CreateNode(int heso, int somu)
- {
- Node *p = new Node;
- if (!p) exit(1);
- p->heso = heso;
- p->somu = somu;
- p->pNext = NULL;
- return p;
- }
- void NhapDaThuc(List &L)
- {
- int x, y;
- int n;
- cout << "bac cua da thuc: ";
- cin >> n;
- do
- {
- cout << "he so: ";
- cin >> x;
- cout << "so mu: ";
- cin >> y;
- AddHead(L, CreateNode(x, y));
- n--;
- } while (n >= 0);
- }
- void AddDaThuc(List &L, Node *pAdd)
- {
- Node *p = L.pHead;
- while (p != NULL && p->somu != pAdd->somu)
- p = p->pNext;
- if (p == NULL) AddHead(L, pAdd);
- else
- {
- p->heso += pAdd->heso;
- delete pAdd;
- }
- }
- void CongDaThuc(List &L, List &L1)
- {
- Node *p = NULL;
- while (L1.pHead != NULL)
- {
- p = L1.pHead;
- L1.pHead = p->pNext;
- p->pNext = NULL;
- AddDaThuc(L, p);
- }
- }
- void Output(List L)
- {
- Node *p = L.pHead;
- while (p)
- {
- cout << p->heso << " * x ^ " << p->somu << " + ";
- p = p->pNext;
- }
- }
- void main()
- {
- List L1, L2;
- CreateList(L1);
- CreateList(L2);
- NhapDaThuc(L1);
- NhapDaThuc(L2);
- CongDaThuc(L1, L2);
- Output(L1);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement