Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ConsoleApplication1.cpp : 定義主控台應用程式的進入點。
- //
- #include "stdafx.h"
- #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->next = NULL;
- 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 << "empty" << endl;
- return;
- }
- 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;
- }
- void main()
- {
- mystack st1;
- st1.push(1);
- st1.push(2);
- st1.push(3);
- st1.push(4);
- st1.pop();
- st1.pop();
- st1.pop();
- st1.pop();
- st1.pop();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement