# Maximum Element

### Problem Statement :

```You have an empty sequence, and you will be given N queries. Each query is one of these three types:

1 x  -Push the element x into the stack.
2    -Delete the element present at the top of the stack.
3    -Print the maximum element in the stack.
Input Format

The first line of input contains an integer, N . The next N lines each contain an above mentioned query. (It is guaranteed that each query is valid.)

Constraints

1 < = N < = 10 ^5
1 < = x < =  10^9
1 <=  type <= 3

Output Format

For each type 3 query, print the maximum element in the stack on a new line.```

### Solution :

```                            ```Solution in C :

In C ++ :

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#include <stack>
using namespace std;

int main() {

stack<int> max;
stack<int> s;
max.push(0);
int n;
cin>>n;
while (n--){
int a;
cin>>a;
if(a==1){
cin>>a;
if(a>=max.top()) max.push(a);
s.push(a);
}
else if(a==2){
if(s.top()==max.top()) max.pop();
s.pop();
}
else if(a==3) cout<<max.top()<<endl;
}
return 0;
}

In Java :

import java.io.*;
import java.util.*;

public class Solution {
private static void getMaxElementFromStack()
{
Stack<Integer> stack = new Stack<Integer>();
Stack<Integer> onlyMaxs = new Stack<Integer>();

Scanner sc = new Scanner(System.in);

int N = Integer.parseInt(sc.nextLine());
int temp = 0;

while(sc.hasNext())
{
String type[] = sc.nextLine().split(" ");
switch(type[0])
{
case "1":
temp = Integer.parseInt(type[1]);
stack.push(temp);
if(onlyMaxs.isEmpty() || onlyMaxs.peek() <= temp)
onlyMaxs.push(temp);
break;
case "2":
temp = stack.pop();
if(temp == onlyMaxs.peek())
onlyMaxs.pop();
break;
case "3":
System.out.println(onlyMaxs.peek());
}
N--;
}

while(N-- > 0)
System.out.println(onlyMaxs.peek());

}
public static void main(String[] args) {
getMaxElementFromStack();
}
}

In C :

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

typedef struct _node node;
struct _node{
long long int data;
node *next;
};
node* getnode(long long int data){
node *newnode=(node*)malloc(sizeof(node));
newnode->data=data;
newnode->next=NULL;
return newnode;
}
node * push(node *top,int long long  data){
node *newnode=getnode(data);
if(top == NULL)
return newnode;
newnode->next=top;
return newnode;
}

node * delete(node *top){
node  *temp=top;
top=top->next;
free(temp);
}
void move(node *fromTop,node *toTop) {
int data=fromTop->data;
delete(fromTop);
push(toTop,data);

}

void printMax(node *maxtop){
printf("%lld\n",maxtop->data);
}
int main() {

int choice;
long long int N;
long long int data;
node *top=NULL,*maxtop=NULL;
scanf("%lld",&N);
while(N) {
scanf("%d",&choice);
if(choice == 1) {
scanf("%lld",&data);
if(top == NULL)
maxtop=push(top,data);
else if(data >= maxtop->data)
maxtop=push(maxtop,data);
top=push(top,data);

}
else if(choice == 2) {
if(top->data == maxtop->data)
maxtop=delete(maxtop);
top=delete(top);
}
else if(choice == 3) {
printMax(maxtop);
}
N--;
}
return 0;
}

In Python3 :

stack = []
max_stack = []

for _ in range(int(input())):
try:
cmd = input()
except:
cmd = '3'
if cmd[0] == '1':
n = int(cmd.split()[1])
stack.append(n)
if len(max_stack) == 0 or max_stack[-1] < n:
max_stack.append(n)
else:
max_stack.append(max_stack[-1])
elif cmd == '2':
stack.pop()
max_stack.pop()
else:
print(max_stack[-1])```
```

## Insert a node at a specific position in a linked list

Given the pointer to the head node of a linked list and an integer to insert at a certain position, create a new node with the given integer as its data attribute, insert this node at the desired position and return the head node. A position of 0 indicates head, a position of 1 indicates one node away from the head and so on. The head pointer given may be null meaning that the initial list is e

## Delete a Node

Delete the node at a given position in a linked list and return a reference to the head node. The head is at position 0. The list may be empty after you delete the node. In that case, return a null value. Example: list=0->1->2->3 position=2 After removing the node at position 2, list'= 0->1->-3. Function Description: Complete the deleteNode function in the editor below. deleteNo

## Print in Reverse

Given a pointer to the head of a singly-linked list, print each data value from the reversed list. If the given list is empty, do not print anything. Example head* refers to the linked list with data values 1->2->3->Null Print the following: 3 2 1 Function Description: Complete the reversePrint function in the editor below. reversePrint has the following parameters: Sing