Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

DS Wala

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 15

DS WALA

CHECK PARANTHESIS”(”<- YE WALA


#include <iostream>
#include <string>
#include <stack>
using namespace std;
int main(){
stack<string> s;
string s1;
cout<<"enter ur characters here:"<<endl;
cin>>s1;
for(int i=0;s1[i]!='\0';i++){
if(s1[i]=='('){
s.push("(");
}
else
if(s1[i]==')'){
s.pop();
}
else{
continue;
}
}
if(s.empty()){
cout<<"shi hai";
}
else{
cout<<"galat hai"<<endl;
}
}
Output:
enter ur characters here:
vicky()()()()()((()
galat hai
Write a C/C++ program to find the minimum
element of the stack in constant time without using
extra space.

Code

// Online C++ compiler to run C++ program online

#include <iostream>

#include<stack>

#include<limits.h>

using namespace std;

int main() {

stack<int> s1;

int mini=INT_MAX;

string s;

while(true){

cout<<"enter ur function to be done"<<endl;

cin>>s;

if(s=="push"){

int n;

cout<<"enter no to be pushed"<<endl;

cin>>n;

if(s1.empty()){

s1.push(n);

mini=n;

else

if(n<mini){
int t=s1.top();

s1.push(2*n-t);

mini=n;

else{

s1.push(n);

else

if(s=="pop"){

if(s1.empty()){

cout<<"khali hai"<<endl;

else

if(s1.top()>mini){

s1.pop();

else

if(s1.top()<mini){

int t=s1.top();

s1.pop();

int m=mini;

mini=2*mini-t;

return m;

else if(s=="top"){

if(s1.empty()){

cout<<"empty stack"<<endl;
}

else

if(s1.top()>mini){

cout<<s1.top()<<endl;

else

if(s1.top()<mini){

cout<<mini;

else if(s=="min"){

if(s1.top()>=mini){

cout<<"minimum is "<<mini<<endl;

else

if(s1.top()<mini){

cout<<"minimum is "<<mini<<endl;

else

break;

Output:

enter ur function to be done

pop
khali hai

enter ur function to be done

top

empty stack

enter ur function to be done

push

enter no to be pushed

enter ur function to be done

push

enter no to be pushed

enter ur function to be done

min

minimum is 3

enter ur function to be done

top

3enter ur function to be done

Q Write a C/C++ program to find the minimum element of the stack in constant time with using extra space.
Code:

// Online C++ compiler to run C++ program online

#include <iostream>

#include<stack>

#include<limits.h>

using namespace std;

int main()

int mini=INT_MAX;
stack<int> p,q;

string s;

while(true){

cout<<"enter string"<<endl;

cin>>s;

if(s=="push"){

int n;

cout<<"enter no."<<endl;

cin>>n;

if(p.empty()){

p.push(n);

q.push(n);

else if(n<=q.top()){

p.push(n);

q.push(n);

else

p.push(n);

else if(s=="pop"){

if(p.empty()){

cout<<"empty stack"<<endl;

else if(p.top()==q.top()){
p.pop();

q.pop();

else

p.pop();

else

if(s=="top"){

if(p.empty()){

cout<<"empty stack"<<endl;

else

cout<<p.top()<<endl;

else

if(s=="min"){

if(q.empty()){

cout<<"empty"<<endl;

else

cout<<q.top()<<endl;

else{

break;
}

Output:

enter string

push

enter no.

enter string

pop

enter string

top

empty stack

enter string

pop

empty stack

enter string

push

enter no.

enter string

push

enter no.

enter string

top

enter string
min

1enter string

#QUEUE CREATION USING C(STRUCT)

#include <stdio.h>
#include <stdlib.h>
struct Queue{
int front;
int rear;
int capacity;
int *array;
};
struct Queue* createqueue(int capacity){
struct Queue* queue=(struct Queue*)malloc(sizeof(struct Queue));
queue->capacity=capacity;
queue->rear=queue->front=-1;
queue->array=(int*)malloc(sizeof(int)*queue->capacity);
return queue;
}
int isFull(struct Queue* queue){
if(queue->rear==queue->capacity-1){
return 1;
}
else{
return 0;
}
int isEmpty(struct Queue* queue){
if(queue->rear==-1){
return 1;
}
else{
return -1;
}
}
void enqueue(struct Queue*queue,int x){
if(isFull)
return;
queue->array[++queue->rear]=x;
if(queue->front==-1){
queue->front++;
}
}
int dequeue(struct Queue* queue){
if(isEmpty){
return -1;
}
int x=queue->array[queue->front];
queue->front++;
if(queue->front>queue->rear){
queue->front=queue->rear=-1;
}
return x;
}
int front(struct Queue* queue){
if(isEmpty){
printf("empty queue");
return -1;
}
return queue->array[queue->front];
}
int rear(struct Queue* queue){
if(isEmpty){
printf("empty queue");
return -1;
}
return queue->array[queue->rear];
}
}
int main()
{
struct Queue*queue=createqueue(100);
enqueue(queue,10);
enqueue(queue,20);
enqueue(queue,30);
enqueue(queue,40);
enqueue(queue,50);

return 0;
}
#STACK IN C
#include <stdio.h>
#include <stdlib.h>
struct Stack{
int capacity;
int top;
int *array;
};
struct Stack* createstack(int capacity){
struct Stack* stack=(struct Stack*)malloc(sizeof(struct Stack*));
stack->capacity=capacity;
stack->top=-1;
stack->array=(int*)malloc(sizeof(int)*stack->capacity);
return stack;
}
int full(struct Stack* stack){
if(stack->top==stack->capacity-1){
return 1;
}
return 0;
}
int empty(struct Stack* stack){
if(stack->top==-1){
return 1;
}
return 0;
}
void push(struct Stack* stack,int x){
if(full(stack)){
printf("stack full");

}
stack->array[++stack->top]=x;
}
int pop(struct Stack* stack){
if (empty(stack)){
printf("empty stack");
return -1;
}
int x=stack->array[stack->top--];
return x;
}
int top(struct Stack*stack){
if (empty(stack)){
printf("empty stack");
return -1;
}
int x=stack->array[stack->top];
return x;
}
int main(){
struct Stack* stack=create(10);
push(stack,100);
push(stack,200);
push(stack,300);

You might also like