1 solutions
-
0
C :
#include<stdio.h> #include<stdlib.h> #include<malloc.h> #define ERROR 0 #define OK 1 typedef int Status; typedef int ElemType; typedef struct Node { ElemType data; struct Node *next; }Node; typedef struct Stack { Node *top; }Stack; Status Init(Stack *S) { if(!S) return ERROR; S->top=NULL; return OK; } Status Empty(Stack S) { return !S.top; } Status Push(Stack *S,ElemType e) { Node *newnode=(Node *)malloc(sizeof(Node)); if(!newnode) return ERROR; newnode->data=e; newnode->next=S->top; S->top=newnode; return OK; } Status Pop(Stack *S,ElemType *e) { Node *p=S->top; *e=p->data; S->top=p->next; free(p); return OK; } Status Destroy(Stack S,ElemType e) { if(!Empty(S)) Pop(&S,&e); return OK; } Status GetTop(Stack S,ElemType *e) { if(Empty(S)) return ERROR; *e=S.top->data; return OK; } int main() { int n,i,e; char a; while((scanf("%d",&n)!=EOF)) { if(n==0) return 0; Stack S; Init(&S); for(i=0;i<n;i++) { scanf("\n%c",&a); switch(a) { case'P': { scanf("%d",&e); Push(&S,e); if(i==n-1) printf("\n"); continue; } case'O': { if(!Empty(S)) Pop(&S,&e); if(i==n-1) printf("\n"); continue; } case'A': { if(GetTop(S,&e)) printf("%d\n",e); else printf("E\n"); if(i==n-1) printf("\n"); continue; } } } Destroy(S,e); } return 0; }C++ :
#include <stdio.h> int n; int run() { int i,a[12222],l=0; char c; while(n>0) { n--; c=' '; while((c!='P')&&(c!='O')&&(c!='A')) scanf("%c",&c); if(c=='P') { scanf("%d",&i); l++; a[l]=i; } if(c=='O') if(l>0) l--; if(c=='A') { if(l>0) printf("%d\n",a[l]); else printf("E\n"); } } printf("\n"); } int main() { scanf("%d",&n); while(n!=0) { run(); n=0; scanf("%d",&n); } return 0; }Pascal :
program acm1909; var n,i,t,top:integer; ch:string; a:array[1..10000]of longint; begin readln(n); while n<>0 do begin top:=0; for i:=1 to n do begin readln(ch); if ch[1]='P' then begin ch:=copy(ch,2,length(ch)); val(ch,t); inc(top); a[top]:=t; end; if (ch[1]='O')and(top>0) then dec(top); if ch[1]='A' then begin if top=0 then writeln('E') else writeln(a[top]); end; end; writeln; readln(n); end; end.Java :
import java.util.*; public class Main { public static void main(String[] args) { int a,b,n; String k; Stack<Integer> s = new Stack<Integer>(); Scanner cin=new Scanner(System.in); while(cin.hasNext()) { n=cin.nextInt(); if(n==0) break; for(int i=0;i<n;i++) { k=cin.next(); if(k.charAt(0)=='P') { s.push(cin.nextInt()); } else if(k.charAt(0)=='O') { if(s.empty()!=true) s.pop(); } else if(k.charAt(0)=='A') { if(s.empty()!=true) System.out.println(s.peek()); else System.out.println("E"); } } while(s.empty()!=true) s.pop(); System.out.print("\n"); } cin.close(); } }
- 1
Information
- ID
- 93
- Time
- 1000ms
- Memory
- 32MiB
- Difficulty
- (None)
- Tags
- # Submissions
- 0
- Accepted
- 0
- Uploaded By