Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- struct node
- {
- int data;
- struct node* pNext;
- };
- typedef struct node NODE;
- struct list
- {
- NODE* pHead;
- NODE* pTail;
- };
- typedef struct list LIST;
- void init(list& a)
- {
- a.pHead = NULL;
- a.pTail = NULL;
- }
- NODE* getNode(int x)
- {
- NODE* p = new NODE;
- p->pNext = NULL;
- p->data = x;
- return p;
- }
- void addNotetoHead(list& a, NODE* b)
- {
- if (a.pHead == NULL)
- {
- a.pHead = b = a.pTail;
- }
- else
- {
- b->pNext = a.pHead;
- a.pHead = b;
- }
- }
- void addNotetoTail(list& a, NODE* b)
- {
- if (a.pHead == NULL)
- {
- a.pHead = b = a.pTail;
- }
- else
- {
- a.pTail->pNext = b;
- a.pTail = b;
- }
- }
- void input(list& a)
- {
- int x;
- cout << "nhap so luong phan tu x:";
- cin >> x;
- for (int i = 1; i <= x; i++)
- {
- int n;
- cout << "nhap gia tri can truyen vao:";
- cin >> n;
- NODE* p = getNode(n);
- addNotetoHead(a, p);
- }
- }
- void output(list a)
- {
- for (NODE* p = a.pHead; p != NULL; p = p->pNext)
- {
- cout << p->data << " ";
- }
- }
- int Sum(list a)
- {
- int tong = 0;
- for (NODE* p = a.pHead; p != NULL; p = p->pNext)
- {
- tong = tong + p->data;
- }
- return tong;
- }
- int main()
- {
- LIST l;
- init(l);
- input(l);
- cout << "danh sach LKD: \n";
- output(l);
- int kq = Sum(l);
- cout << "tong cac gia tri :\n" << kq;
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement