Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- class node {
- public:
- int data;
- node* next;
- };
- class mystack {
- public:
- node* head;
- mystack() { head = new node(); head->next = NULL; }
- void push(int i);
- void list();
- void pop();
- };
- void mystack::push(int i)
- {
- node* x = new node();
- x->data = i;
- if (head->next == NULL)
- {
- head->next = x;
- return;
- }
- node* p = head;
- while (p->next != NULL)
- p = p->next;
- x->next = p->next;
- p->next = x;
- }
- void mystack::list()
- {
- if (head->next == NULL)
- cout << "無資料" << endl;
- node* p = head;
- while (p->next != NULL)
- {
- p = p->next;
- cout << p->data << endl;
- }
- }
- void mystack::pop() {
- if (head->next == NULL) {
- cout << "無資料" << endl;
- return;
- }
- node* current = head;
- node* pre = NULL;
- while (current->next != NULL)
- {
- pre = current;
- current = current->next;
- }
- cout << current->data << endl;
- delete current;
- pre->next = NULL;
- }
- class myqueue {
- public:
- node* head;
- myqueue() { head = new node; head->next = NULL; }
- void enqueue(int i);
- void dequeue();
- };
- void myqueue::enqueue(int i) {
- node* x = new node();
- x->data = i;
- if (head->next == NULL)
- {
- head->next = x;
- return;
- }
- node* p = head;
- while (p->next != NULL)
- p = p->next;
- x->next = p->next;
- p->next = x;
- }
- void myqueue::dequeue()
- {
- if (head->next == NULL)
- {
- cout << "無資料" << endl;
- return;
- }
- node* current = head;
- node* pre = NULL;
- if (current->next != NULL)
- {
- current = current->next;
- cout << current->next << endl;
- pre = current->next;
- delete current;
- head->next = pre;
- }
- }
- void main()
- {
- mystack st1;
- st1.push(1);
- st1.push(2);
- st1.push(3);
- st1.list();
- st1.pop();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement