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

#define SIZE 5

int stack[SIZE],top = -1;

bool isFull()
{
return top==SIZE-1;
}

bool isEmpty()
{
return top==-1;
}

void push(int element)


{
if(isFull())
printf("\nStack Overflow");
else
stack[++top] = element;
}

void pop()
{
if(isEmpty())
printf("\nStack underflow");
else
printf("\nElement to be deleted is: %d",stack[top--]);
}

void peek()
{
if(isEmpty())
printf("\nThere is no top most element due to empty stack");
else
printf("\nTop of stack is: %d",stack[top]);
}

void display()
{
if(isEmpty())
printf("\nNo elements to print due to empty stack");
else
{
for(int i=top;i>=0;i--)
printf("\n%d",stack[i]);
printf("\n");
}
}

-------------------------------------------------------------------------
Implementation of array using LL

struct stackNode
{
int data;
struct stackNode *next;
}*top = NULL;

bool isEmpty()
{
return top == NULL;
}

struct stackNode* push(struct stackNode *top,int value)


{
struct stackNode *newNode = (struct stackNode*) malloc(sizeof(struct
stackNode));
newNode->data = value;
newNode->next = top;
return newNode;
}

struct stackNode* pop(struct stackNode *top)


{
if(isEmpty())
{
printf("\nDue to empty stack we cannot perform pop operation\n");
return NULL;
}
return top->next;
//return top==NULL || top->next==NULL ? NULL : top->next;
}

void peek(struct stackNode *top)


{
if(isEmpty())
printf("\nNo top element due to empty stack\n");
else
printf("\nTop of stack is: %d\n",top->data);
}

void display(struct stackNode *top)


{
if(top)
{
printf("%d\n",top->data);
display(top->next);
}
}

----------------------------------------------------------------------------------
BALANCED BRACKETS

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#define MAX 100

char stack[MAX];
int top=-1;

void push(char ch)


{
stack[++top] = ch;
}

int main()
{
char str[100];
scanf("%s",str);

int i = -1;
while(str[++i])
{
char ch = str[i];
if(ch=='(' || ch=='{' || ch=='[') push(ch);
else if((ch==')' && stack[top]=='(') || (ch==']' && stack[top]=='[') ||
(ch=='}' && stack[top]=='{'))
top--;
else
{
printf("NO\n");
return 0;
}
}
str[i]=='\0' && top==-1 ? printf("YES\n") : printf("NO\n");

return 0;
}

-------------------------------------------------------------------------------
ARRAY Implemenatation using QUEUE

#define SIZE 5

int queue[SIZE], front = 0, rear = -1;

bool isFull()
{
return rear == SIZE-1;
}

bool isEmpty()
{
return front > rear;
}

void enQueue(int element)


{
if(isFull())
printf("\nQueue is overflow\n");
else
queue[++rear] = element;
}

void deQueue()
{
if(isEmpty())
printf("\nQueue is underflow\n");
else
{
printf("\nElement to be deleted is: %d\n",queue[front++]);
if(isEmpty())
{
front = 0;
rear = -1;
}
}
}

void display()
{
if(isEmpty())
printf("\nDue to empty Queue... Printing is not possible\n");
else
{
for(int i=front;i<=rear;i++)
printf("%d ",queue[i]);
printf("\n");
}
}
__________________________________________________________
LINKED LIST IMPLEMENTATION USING QUEUE:

struct QueueNode
{
int data;
struct QueueNode *next;
}*front = NULL, *rear = NULL;

void enQueue(int value)


{
struct QueueNode *newNode = (struct QueueNode*) malloc(sizeof(struct
QueueNode));
newNode->data = value;
newNode->next = NULL;

if(front==NULL)
front = rear = newNode;
else
{
rear->next = newNode;
rear = newNode;
}
}

bool isEmpty()
{
return front == NULL;
}
void deQueue()
{
if(isEmpty())
printf("\nNo elements to display\n");
else
{
printf("\nElement to be deleted is: %d\n",front->data);
front = front->next;
}
}
void display()
{
struct QueueNode *ptr = front;
while(ptr != rear)
{
printf("%d --> ",ptr->data);
ptr = ptr->next;
}
printf("%d --> NULL",ptr->data);
}

int count()
{
if(front==NULL && rear==NULL)
return 0;

int c = 0;
struct QueueNode *ptr = front;
while(ptr)
{
c++;
ptr = ptr->next;
}
return c;
}

---------------------------------------------------------------------------------
CIRCULAR QUEUE

#define MAX 5

int cQueue[MAX], front = -1, rear = -1;

bool isFull()
{
return ((front==0 && rear==MAX-1) || (front == rear+1));
}

bool isEmpty()
{
return front==-1;
}

void insert(int ele)


{
if(isFull())
printf("\nCirculer Queue is Overflow. Not possible to insert value: %d\
n",ele);
else
{
if(front == -1)
front = 0;
rear = (rear+1)%MAX;
cQueue[rear] = ele;
printf("\nElement %d inserted successfully\n",ele);
}
}
void delete()
{
if(isEmpty())
printf("\nCirculer Queue is empty\n");
else
{
printf("\nElement to be deleted is: %d\n",cQueue[front]);
if(front == rear)
front = rear = -1;
else
front = (front+1)%MAX;
}
}

void display()
{
if(isEmpty())
printf("\nDue to empty list not possible to display\n");
else
{
for(int i=front;i!=rear;i=(i+1)%MAX)
printf("%d ",cQueue[i]);
printf("%d\n",cQueue[rear]);
}
}

-----------------------------------------------------------------------------
INFIX TO POSTFIX

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

char stack[100000];
int top = -1;

void push(char ch)


{
stack[++top] = ch;
}

char pop()
{
return stack[top--];
}

int priority(char ch)


{
return (ch=='+'||ch=='-') ? 1 : (ch=='*'||ch=='/'||ch=='%') ? 2 : 0;
}

int main()
{
char str[100];
scanf("%s",str);

int i = -1;
while(str[++i])
{
if(isupper(str[i]))
printf("%c",str[i]);
else if(str[i]=='(')
push(str[i]);
else if(str[i]==')')
{
char ch;
while((ch=pop())!='(')
printf("%c",ch);
}
else
{
while(priority(stack[top]) >= priority(str[i]))
printf("%c",pop());
push(str[i]);
}
}
while(top!=-1)
printf("%c",pop());
return 0;
}
-----------------------------------------------------------------------------------
------------
PREFIX OPERATION

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

int main()
{
char str[100];
int stack[100], top = -1;

scanf("%s",str);

int i = -1;

while(str[++i])
{
if(isupper(str[i]))
{
int n;
scanf("%d",&n);
stack[++top] = n;
}
else
{
int var2 = stack[top--];
int var1 = stack[top--];
int result;
switch(str[i])
{
case '+': result = var1 + var2; break;
case '-': result = var1 - var2; break;
case '*': result = var1 * var2; break;
case '/': result = var1 / var2; break;
}
stack[++top] = result;
}
}
printf("%d",stack[top--]);
return 0;
}
-----------------------------------------------------------------------------------
--

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

long stack[10000000], top = -1;

int main()
{
int n;
scanf("%d",&n);

while(n--)
{
int choice;
scanf("%d",&choice);
if(choice == 1)
{
long value;
scanf("%ld",&value);
stack[++top] = value;
}
if(choice == 2) top--;
if(choice == 3)
{
long max = stack[top];
for(long i=top;i>=0;i--)
if(max < stack[i])
max = stack[i];
printf("%ld\n",max);
}
}
return 0;
}
----------------------------------------------------------
def Josh(person, k, index):

if len(person) == 1:

print(person[0])
return
index = ((index+k)%len(person))
person.pop(index)
Josh(person,k,index)
n = int(input())
k = int(input())
k-=1
index = 0
person=[]
for i in range(1,n+1):
person.append(i)
Josh(person,k,index)

You might also like