Sudo布局[1.3]|堆栈设计

给定q个查询,您需要在堆栈上执行操作。查询有三种类型:1、2和3。如果操作是推动(1),则推动元素;如果操作是弹出(2),则弹出元素;如果是顶部(3),则在堆栈顶部打印元素(如果堆栈为空,则打印“-1”,不带引号)。 例如:

null
Input: Queries =  6                  3                  1 5                  1 6                  1 7                  2                  3Output: -1         6 The first query is to print top, but since the stack is empty, so we print -1.Next three queries are to push 5, 6, and 7, so we pushed them on a stack. Next query is pop, so we popped 7 from a stack. Final query is to print thetop, so 6 is there at the top and thus printed.

方法: 堆栈 可用于执行给定的操作。如果查询的输入为1,则获取另一个输入,并使用 作用如果查询的输入为2,则使用 这个 pop() 在堆栈中运行。如果查询的输入为3,则使用 这个 top() 作用 以下是上述方法的实施情况:

C++

// C++ program for
// Sudo-Placement | Stack Design
#include <bits/stdc++.h>
using namespace std;
stack< int > s;
// function to perform type-1 operation
void _push( int n)
{
s.push(n);
}
// function to perform type-2 operation
void _pop()
{
s.pop();
}
// function to perform type-3 operation
void print()
{
// if the stack is not empty
if (!s.empty())
cout << s.top() << endl;
else
cout << -1 << endl;
}
// Driver Code
int main()
{
// 1st query
print();
// 2nd query
_push(5);
// 3rd query
_push(6);
// 4th query
_push(7);
// 5th query
_pop();
// 6th query
print();
return 0;
}


JAVA

import java.util.*;
// Java program for
// Sudo-Placement | Stack Design
class GFG
{
static Stack<Integer> s = new Stack<>();
// function to perform type-1 operation
static void _push( int n)
{
s.push(n);
}
// function to perform type-2 operation
static void _pop()
{
s.pop();
}
// function to perform type-3 operation
static void print()
{
// if the stack is not empty
if (!s.empty())
{
System.out.println(s.peek());
}
else
{
System.out.println(- 1 );
}
}
// Driver Code
public static void main(String[] args)
{
// 1st query
print();
// 2nd query
_push( 5 );
// 3rd query
_push( 6 );
// 4th query
_push( 7 );
// 5th query
_pop();
// 6th query
print();
}
}
// This code contributed by Rajput-Ji


Python3

# Python3 program for
# Sudo-Placement | Stack Design
s = [];
# function to perform
# type-1 operation
def _push(n):
s.append(n);
# function to perform
# type-2 operation
def _pop():
s.pop();
# function to perform
# type-3 operation
def _print():
# if the stack is not
# empty
if ( len (s) > 0 ):
print (s[ len (s) - 1 ]);
else :
print ( "-1" );
# Driver Code
if __name__ = = '__main__' :
# 1st query
_print();
# 2nd query
_push( 5 );
# 3rd query
_push( 6 );
# 4th query
_push( 7 );
# 5th query
_pop();
# 6th query
_print();
# This code is contributed by 29AjayKumar


C#

// C# program for
// Sudo-Placement | Stack Design
using System;
using System.Collections.Generic;
class GFG
{
static Stack< int > s = new Stack< int >();
// function to perform type-1 operation
static void _push( int n)
{
s.Push(n);
}
// function to perform type-2 operation
static void _pop()
{
s.Pop();
}
// function to perform type-3 operation
static void print()
{
// if the stack is not empty
if (s.Count != 0)
{
Console.WriteLine(s.Peek());
}
else
{
Console.WriteLine(-1);
}
}
// Driver Code
public static void Main()
{
// 1st query
print();
// 2nd query
_push(5);
// 3rd query
_push(6);
// 4th query
_push(7);
// 5th query
_pop();
// 6th query
print();
}
}
/* This code contributed by PrinciRaj1992 */


Javascript

<script>
// Javascript program for
// Sudo-Placement | Stack Design
let s=[];
// function to perform type-1 operation
function _push(n)
{
s.push(n);
}
// function to perform type-2 operation
function _pop()
{
s.pop();
}
// function to perform type-3 operation
function print()
{
// if the stack is not empty
if (s.length!=0)
{
document.write(s[s.length-1]+ "<br>" );
}
else
{
document.write(-1+ "<br>" );
}
}
// Driver Code
// 1st query
print();
// 2nd query
_push(5);
// 3rd query
_push(6);
// 4th query
_push(7);
// 5th query
_pop();
// 6th query
print();
// This code is contributed by rag2127
</script>


输出:

-16

© 版权声明
THE END
喜欢就支持一下吧
点赞5 分享