Professional Documents
Culture Documents
Stacks&queues
Stacks&queues
bool isFull()
{
return top==SIZE-1;
}
bool isEmpty()
{
return top==-1;
}
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;
}
----------------------------------------------------------------------------------
BALANCED BRACKETS
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#define MAX 100
char stack[MAX];
int top=-1;
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
bool isFull()
{
return rear == SIZE-1;
}
bool isEmpty()
{
return front > rear;
}
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;
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
bool isFull()
{
return ((front==0 && rear==MAX-1) || (front == rear+1));
}
bool isEmpty()
{
return front==-1;
}
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;
char pop()
{
return stack[top--];
}
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>
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)