0% found this document useful (0 votes)
3 views

Circular Queue

The document contains multiple C programs demonstrating data structures and algorithms, including a circular queue, a double-ended queue (deque), infix to postfix expression conversion using a stack, and a palindrome checker. Each program includes functions for essential operations such as enqueue, dequeue, and display for queues, as well as push and pop for stacks. The code snippets illustrate the implementation of these concepts with user interaction for input and output.

Uploaded by

hackergang8905
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Circular Queue

The document contains multiple C programs demonstrating data structures and algorithms, including a circular queue, a double-ended queue (deque), infix to postfix expression conversion using a stack, and a palindrome checker. Each program includes functions for essential operations such as enqueue, dequeue, and display for queues, as well as push and pop for stacks. The code snippets illustrate the implementation of these concepts with user interaction for input and output.

Uploaded by

hackergang8905
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

AIM: Implement a C Program for Circular Queue.

Source Code:
#include<stdio.h>
#define MAX 10
int q[MAX];
int front=-1,rear=-1;
int val;
int main()
{
int ch;
clrscr();
printf("\n1.enqueue\n2.dequeue\n3.display\n4.exit\n");
while(1)
{
printf("enter ur choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:enqueue();
break;
case 2:dequeue();
break;
case 3:display();
break;
case 4:exit(0);
break;
default:printf("wrong choice");
}
}
}
void enqueue()
{
printf("enter the value:");
scanf("%d",&val);
if(front==-1&&rear==-1)
{
rear=front=0;
q[rear]=val;
}
else if((rear+1)%MAX==front)
printf("queue is overflow");
else
{
rear=(rear+1)%MAX;
q[rear]=val;
}
}
void dequeue()
{
if(front==-1&&rear==-1)
printf("queue is underflow");
else
{
printf("%d\t\n",q[front]);
if(front==rear)
front=rear=-1;
else
front=(front+1)%MAX;
}
}
void display()
{
int i=front;
if(front==-1&& rear==-1)
{
printf("queue is underflow");
}
else
{
while(i!=(rear+1)%MAX)
{
printf("%d",q[i]);
i=(i+1)%MAX;
}
}
}
Aim: Implement a double-ended queue (deque) with essential operations.
Source Code:
#include<stdio.h>
#define MAX 10
int dq[MAX];
int front=-1,rear=-1;
int val;
int main()
{
int ch;
//clrscr();
printf("\n1.enqueueatfront\n2.enqueueatrear\n3.dequeueatfront\n4.dequeueatrear\n5.display\
n6.exit\n");
while(1)
{
printf("enter ur choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:enqueueatfront();
break;
case 2:enqueueatrear();
break;
case 3:dequeueatfront();
break;
case 4: dequeueatfront();
break;
case 5:display();
break;
case 6:exit(0);
break;
default:printf("wrong choice");
}
}
}
void enqueueatfront()
{
printf("enter the value:");
scanf("%d",&val);
if((front==-1&&rear==MAX-1)||(front==rear+1))
{
printf("queue is overflow");
}
else if(front==-1&&rear==-1)
{
front=0;
rear=0;
dq[front]=val;
}
else if(front==0)
{
front=MAX-1;
dq[front]=val;
}
else
{
front--;
dq[front]=val;
}
}
void enqueueatrear()
{
printf("enter the value:");
scanf("%d",&val);
if((front==-1&&rear==MAX-1)||(front==rear+1))
{
printf("queue is overflow");
}
else if(front==-1&&rear==-1)
{
front=0;
rear=0;
dq[rear]=val;
}
else if(rear==MAX-1)
{
rear=0;
dq[rear]=val;
}
else
{
rear++;
dq[rear]=val;
}
}
void dequeueatfront()
{
if(front==-1&&rear==-1)
printf("queue is underflow");
else if(front==rear)
{
printf("%d\t\n",dq[front]);
front=rear=-1;
}
else if(front==MAX-1)
{
printf("%d\t\n",dq[front]);
front=0;
}
else
{
printf("%d\t\n",dq[front]);
front++;
}
}
void dequeueatrear()
{
if(front==-1&&rear==-1)
printf("queue is underflow");
else if(front==rear)
{
printf("%d\t\n",dq[front]);
front=rear=-1;
}
else if(rear==0)
{
printf("%d\t\n",dq[rear]);
rear=MAX-1;
}
else
{
printf("%d\t\n",dq[rear]);
rear--;
}
}

void display()
{
int i=front;
if(front==-1&& rear==-1)
{
printf("queue is underflow");
}
else
{
while(i!=rear)
{
printf("%d",dq[i]);
i=(i+1)%MAX;
}
printf("%d",dq[rear]);
}
}
AIM: Use a stack to evaluate an infix expression and convert it to postfix.
Source Code:
#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 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;
}
AIM: Create a program to determine whether a given string is a palindrome or not.
Source Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 50
int top = -1, front = 0;
int stack[MAX];
void push(char);
void pop();
void main()
{
int i, choice;
char s[MAX], b;
while (1)
{
printf("1-enter string\n2-exit\n");
printf("enter your choice\n");
scanf("%d", &choice);
switch (choice)
{
case 1:
printf("Enter the String\n");
scanf("%s", s);
for (i = 0;s[i] != '\0';i++)
{
b = s[i];
push(b);
}
for (i = 0;i < (strlen(s) / 2);i++)
{
if (stack[top] == stack[front])
{
pop();
front++;
}
else
{
printf("%s is not a palindrome\n", s);
break;
}
}
if ((strlen(s) / 2) ==front)
printf("%s is palindrome\n", s);
front = 0;
top = -1;
break;
case 2:
exit(0);
default:
printf("enter correct choice\n");
}
}
}
void push(char a)
{
top++;
stack[top] = a;
}
void pop()
{
top--;
}

You might also like