C++语言题目3

IT小小龙 posted @ 2014年4月06日 16:26 in C++ with tags 链栈 , 633 阅读
//采用面向对象的方法实现一个stack类,操作包括进栈,出栈,取栈顶元素,显示整个栈;
//(只要内存够用,可以无限次进栈)
#include"iostream"
using namespace std;

class Node{
private:
  int x;
  Node* next;
public:
  Node(int data)
  {
    x = data;
  }
  void setNext(Node* p)
  {
    next = p;
  }
  Node* getNext()
  {
    return next;
  }
  int setData(int number)
  {
    x = number;
  }
  int getData()
  {
    return x;
  }
  ~Node(){}
};

class Stack
{
private:
  Node *nodelast;
public:
  Stack()
  {
    nodelast = new Node(0);
    nodelast->setNext(NULL);
  }
  void pushData(int x)
  {
    Node *p = new Node(x);
    p->setNext(nodelast->getNext());
    nodelast->setNext(p);
    nodelast->setData(nodelast->getData()+1);
  }
  int popData()
  {
    int value;
    int error = 0;
    Node *p = nodelast->getNext();
    Node *q = p;
    if((p != NULL))
      {
	value = q->getData();
	p = p->getNext();
	nodelast->setNext(p);
	nodelast->setData(nodelast->getData()-1);
	delete q;
	return value;
      }
    else
      {
	throw error;
      }
  }
  int getTopData()
  {
    int value;
    int error = 0;
    Node *p = nodelast->getNext();
    if((p != NULL))
      {
	value = p->getData();
	return value;
      }
    else
      {
	throw error;
      }
  }
  void showStack()
  {
    int i;
    Node *p = nodelast;
    cout<<"Top ";
    for(i=0; i< nodelast->getData(); i++)
      {
	p = p->getNext();
	cout<< p->getData()<<" ";
      }
    cout<<"End"<<endl;
  }
};
int main()
{
  int value;
  Stack s;
  try
    {
      cout<<"出栈:"<<endl;
      s.popData();
    }
  catch(int)
    {
      cout<<"栈为空!"<<endl;
    }
  cout<<"入栈1,3,2:"<<endl;
  s.pushData(1);
  s.pushData(3);
  s.pushData(2);
  s.showStack();
  try
    {
      value = s.popData();
      cout<<"出栈:"<<value<<endl;
      s.showStack();
      value = s.getTopData();
      cout<<"当前栈顶元素:"<<value<<endl;
      s.showStack();
    }
  catch(int)
    {
      cout<<"栈为空!"<<endl;
    }
  cout<<"入栈3,4,5:"<<endl;
  s.pushData(3);
  s.pushData(4);
  s.pushData(5);
  s.showStack();
  return 1;
}
Avatar_small
Saksham Portal Login 说:
2022年8月07日 18:45

Haryana state government has brought the Management Information system into their education system to help the higher education of the state to get services through the MIS system. The DSC MIS Haryana Education portal has been launched with the Management Information system and has been provided access to the students, teachers, officers, and other senior education admins. Saksham Portal Login This is a one stop for every detail of the education such that the students date along with their education curriculum will be available. As well the teacher’s information with their education type will be available by providing enough information for officers to reach out to everyone based on their requirements.

Avatar_small
UBSE Model Paper Cla 说:
2022年8月16日 19:38

Uttarakhand Board Model Paper 2023 Class 7 Pdf Download with Answers for English Medium, Hindi Medium, Urdu Medium & Students for Small Answers, Long Answer, Very Long Answer Questions, and Essay Type Questions to Term1 & Term2 Exams


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter