Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <conio.h>
- using namespace std;
- struct Node
- {
- int data;
- Node *next;
- };
- struct list
- {
- Node *head;
- Node *tail;
- };
- Node *createNode(int x)
- {
- Node *p = new Node;
- if (p == NULL)
- {
- return NULL;
- }
- else
- {
- p->data = x;
- p->next = NULL;
- }
- return p;
- }
- void createlist(list &l)
- {
- l.head = l.tail = NULL;
- }
- void andtail(list &l, int x)
- {
- Node *p = createNode(x);
- if (l.head == NULL)
- {
- l.head = l.tail = p;
- }
- else
- {
- l.tail->next = p;
- l.tail = p;
- }
- }
- void input(list &l, int &n)
- {
- cout << "nhap so node can tao: ";
- cin >> n;
- createlist(l);
- for (int i = 1; i <= n; i++)
- {
- int x;
- cout << "nhap vao data: ";
- cin >> x;
- andtail(l, x);
- }
- }
- void output(list l)
- {
- for (Node *p = l.head; p != NULL; p = p->next)
- {
- cout << " " << p->data;
- }
- }
- void main()
- {
- int n;
- int x;
- list(l);
- input(l, n);
- output(l);
- _getch();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement