queues

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

WEEK 6:

1.Implement a queue using arrays and linked lists


#include<stdio.h>
#include<stdlib.h>
#define max 5
int rear=-1,front=-1;
int queue[max];
void insert();
int delete();
void display();
int main()
{
int w,num;
while(1)
{
printf("\n1. Insert");
printf("\n2. Delete");
printf("\n3. Display");
printf("\n4. EXIT");
printf("\nEnter What you want :");
scanf("%d",&w);
if(w==1)
insert();
else if(w==2)
num=delete();
else if(w==3)
display();
else if(w==4)
exit(1);
else printf("\nInvalid Choice!!");
}
}

void insert()
{
int num;
if(rear==max-1){
printf("\nQueue is Full !\n");
return;
}
printf("\nEnter a number for insert :");
scanf("%d",&num);
if(front==-1)
front=front+1;

rear=rear+1;
queue[rear]=num;
}
int delete()
{
int num;
if(front==-1 || front==rear+1)
{
printf("\nQueue is Empty !\n");
return 0;
}
num=queue[front];
printf("\n%d was deleted !\n",num);
front=front+1;
}
void display()
{
int i;
if(front==-1 || front==rear+1)
{
printf("\nQueue Is Empty ! Nothing To Display !!");
return;
}
printf("\n\n");
for(i=front;i<=rear;i++)
printf("%d\t",queue[i]);
printf("\n");
}
Out put:::
1. Insert
2. Delete
3. Display
4. EXIT
Enter What you want :1

Enter a number for insert :2


1. Insert
2. Delete
3. Display
4. EXIT
Enter What you want :1
Enter a number for insert :4
1. Insert
2. Delete
3. Display
4. EXIT
Enter What you want :1
Enter a number for insert :8
1. Insert
2. Delete
3. Display
4. EXIT
Enter What you want :1
Enter a number for insert :9
1. Insert
2. Delete
3. Display
4. EXIT
Enter What you want :3
2 4 8 9
1. Insert
2. Delete
3. Display
4. EXIT
Enter What you want :2
2 was deleted !

1. Insert
2. Delete
3. Display
4. EXIT
Enter What you want :3
4 8 9
1. Insert
2. Delete
3. Display
4. EXIT
Enter What you want :

Stack and Queue Applications


i) Use a stack to evaluate an infix expression and convert it to postfix.

#include<stdio.h>
#include<ctype.h>

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

void push(char x)
{
stack[++top] = x;
}

char pop()
{
if(top == -1)
return -1;
else
return stack[top--];
}

int priority(char x)
{
if(x == '(')
return 0;
if(x == '+' || x == '-')
return 1;
if(x == '*' || x == '/')
return 2;
return 0;
}

int main()
{
char exp[100];
char *e, x;
printf("Enter the INFIX expression : ");
scanf("%s",exp);
printf("\n");
e = exp;
while(*e != '\0')
{
if(isalnum(*e))
printf("%c ",*e);
else if(*e == '(')
push(*e);
else if(*e == ')')
{
while((x = pop()) != '(')
printf("%c ", x);
}
else
{
while(priority(stack[top]) >= priority(*e))
printf("%c ",pop());
push(*e);
}
e++;
}

while(top != -1)
{
printf("%c ",pop());
}return 0;
}

OUT PUT:

Enter the INFIX expression : a+b*c

abc*+

=== Code Execution Successful ===

ii) Create a program to determine whether a given string is a palindrome or not.


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

int main() {
char string1[20];
int i, length;
int flag = 0;
printf("Enter a string: ");
scanf("%s", string1);
length = strlen(string1);
for (i = 0; i < length / 2; i++) {
if (string1[i] != string1[length - i - 1]) {
flag = 1;
break;
}
}
if (flag) {
printf("%s is not a palindrome\n", string1);
} else {
printf("%s is a palindrome\n", string1);
}
return 0;
}
OUT PUT:
Enter a string: AROME
AROME is not a palindrome

iii) Implement a stack or queue to perform comparison and check for symmetry

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy