呵呵,说更新就肯定会写的拉~~~
- /*******************************************
- DS_LinkStack
- By_h4breeez
- Date:April.24th,2012
- *******************************************/
- #include<iostream>
- using namespace std;
- typedef struct StackNode{ //行了,是链栈。链栈每个节点的数据结构:元素+指向下一个元素的指针
- int elem;
- struct StackNode *next;
- }Node;
- typedef struct{ //链栈的数据结构
- Node *Top;
- }LinkStack;
- void InitStack(LinkStack *s) //初始化一个链栈,把头指针清零。但这里如果debug可以发现可以查看
- { //top指向的elem和next,显示为能被引用。(因为top是NULL啊,违法访问了)
- s->Top = NULL;
- }
- int StackEmpty(LinkStack *s) //判栈空
- {
- return NULL == s->Top;
- }
- void Push(LinkStack *s,int elem) //压入一个元素到栈中,进栈
- {
- Node *p = (Node *)malloc(sizeof(Node));
- p->elem = elem;
- p->next = s->Top; //遇到指针转换就安心画图吧,刚开始时这样最能帮你理解了
- s->Top = p;
- }
- int Pop(LinkStack *s)
- {
- int elem;
- Node *p = (Node *)malloc(sizeof(Node));
- p->elem = 0;
- p->next = NULL; //最好对刚分配的内存初始化,方便
- if( StackEmpty(s) )
- {
- cout << "Empty Stack!" << endl;
- return -1;
- }
- else
- {
- elem = s->Top->elem;
- p = s->Top;
- s->Top = p->next;
- free(p);
- return elem;
- }
- }
- int StackTop(LinkStack *s) //取得栈顶元素
- {
- if( StackEmpty(s) )
- {
- cout << "Empty Stack!" << endl;
- return -1;
- }
- else
- return s->Top->elem;
- }
- void PrintStack(LinkStack *s) //打印栈内元素,和pop不一样哦。这里只是查看栈内的元素
- {
- LinkStack *p1 = (LinkStack *)malloc(sizeof(LinkStack)); ///////////////////////////////////////////////////////////
- LinkStack *p2 = (LinkStack *)malloc(sizeof(LinkStack));
- p1->Top = s->Top;
- p2->Top = s->Top; //重新用两个指针来操作,不想动s,要不后续容易出错,
- while(!StackEmpty(p1)) //特别是先进栈,然后打印,然后进栈,然后再打印,指针变换不好掌握,所以这里我没有动s
- {
- cout << p1->Top->elem << endl;
- p2->Top = p1->Top->next;
- p1->Top = p2->Top;
- }
- free(p1);
- free(p2);
- }
- int main()
- {
- LinkStack *stack = (LinkStack *)malloc(sizeof(LinkStack));
- int choice = 0,elem = 0;
- InitStack(stack);
- cout << "\t0 to push elements---1 to pop elements\n\t2 to get top element---3 to print all elements---other to exit!" << endl;
- cin >> choice; //如果输入字母会出错
- if(cin.fail()) //所以要在这里对输入流做检测,可以放到while里,但不够美观,还有另外方法处理,见末尾
- {
- cout << "input error!" << endl;
- }
- else
- {
- while( choice == 0||choice == 1||choice == 2||choice == 3 )
- {
- switch(choice)
- {
- case 0:
- cout << "Input element to push in the stack!" << endl;
- cin >> elem;
- Push(stack,elem);
- cout << "done" << endl;
- break;
- case 1:
- cout << "No pop a element!" << endl;
- if(!StackEmpty(stack))
- {
- elem = Pop(stack);
- cout << elem << endl;
- }
- else
- cout << "Empty stack!" << endl;
- break;
- case 2:
- cout << "No pop the top element!" << endl;
- if(!StackEmpty(stack))
- {
- elem = StackTop(stack);
- cout << elem << endl;
- }
- else
- cout << "Empty stack!" << endl;
- break;
- case 3:
- if(!StackEmpty(stack))
- { cout << "No print all elements!" << endl;
- PrintStack(stack);
- }
- else
- {
- cout << "Empty stack!" << endl;
- }
- break;
- default:
- exit(0);
- }
- cout << "New choice:";
- cin >> choice;
- }
- }
- return 0;
- }
- /********************************************************************************************************
- 1.关于输入流的检测 cin.fail(),cin.clear(),cin.ingore();
- cin.fail(),实例如上;
- 方法核心:
- cin.clear(); //清除cin流
- cin.ignore(1024,'\n'); //cin.ignore(1024, '\n')的含义是把缓冲区内从当前字符开始直到
- '\n'之前字符(如果有1024个的话)忽略掉,实际上这里假设一行
- 不会超过1024个字符,所以含义是忽略一行
- 2.栈的应用
- 表达式求值
- 子程序嵌套
- 递归调用
- **********************************************************************************************************/