Ds Circular Queue
Ds Circular Queue
Ds Circular Queue
#include<stdio.h>
# define max 5
int queue[max],front=-1,rear=-1;
void enqueue(int element){
int i=front;
if(front==-1&&rear==-1)
{
front=0;
rear=0;
queue[rear]=element;
printf("Inserted->%d",element);
}
else if((rear+1)%max==front){
printf("Queue is overflow");
}
else{
rear=(rear+1);
queue[rear]=element;
printf("Inserted->%d",element);
}
}
void dequeue(){
if((front==-1)&&(rear==-1)){
printf("\n Queue is underflow");
}
else if(front==rear){
printf("\n The Dequeue element -> %d",queue[front]);
front=-1;
rear=-1;
}
else{
printf("\n The dequeue element is %d",queue[front]);
Data Structure PROGRAM
front=(front+1)%max;
}
}
void display(){
int i=front;
if(front==-1&&rear==-1){
printf("\n Queue is empty");
}
else{
printf("\n Elements in a queue are:");
while(i<=rear)
{
printf("%d \t",queue[i]);
i++;
}
}
printf("\n");
}
int main(){
//int front=-1,rear=-1;
int ch,x,element;
do{
printf(" \n Circular Queue");
printf("\n 1. Enqueue\n2. Dequeue\n 3.Diplay \n 4.Exit");
printf("\n enter your choice:");
scanf("%d",&ch);
switch(ch){
case 1:
printf("Enter the element:");
scanf("%d",&element);
enqueue(element);
Data Structure PROGRAM
break;
case 2:
dequeue();
break;
case 3:
display();
break;
case 4:
printf("\n Exit the program");
break;
default:
printf("\n Invalid choice");
}while(ch!=4);
}
Data Structure PROGRAM
EXPECTED INPUT/OUTPUT
Data Structure PROGRAM
Data Structure PROGRAM
Data Structure PROGRAM
#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");
Data Structure PROGRAM
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;
}
…………………………………………………………………………………………………………………………………………………………….
Input/Output
Data Structure PROGRAM
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node *next;
};
printf("\n[head] =>");
//start from the beginning
while(ptr != NULL) {
printf(" %d =>",ptr->data);
ptr = ptr->next;
}
printf(" [null]\n");
}
//link->key = key;
link->data = data;
int main() {
insert(10);
insert(20);
insert(30);
insert(1);
insert(40);
insert(56);
printList();
return 0;
}
#include <stdio.h>
#include <stdlib.h>
// Creating a node
struct node {
int value;
struct node *next;//self referential structure
};
int main() {
// Initialize nodes
struct node *head;
struct node *one = NULL;
struct node *two = NULL;
struct node *three = NULL;
// Allocate memory
one = malloc(sizeof(struct node));
Data Structure PROGRAM
// Connect nodes
one->next = two;
two->next = three;
three->next = NULL;
// printing node-value
head = one;
printLinkedlist(head);
}
Data Structure PROGRAM
#include <stdio.h>
#include <stdlib.h>
inorder(root->left);
inorder(root->right);
printf("%d ", root->data);
}
}
// Function to print the binary search tree in a graphical format
void printTree(struct Node* root, int space) {
if (root == NULL)
return;
// Increase distance between levels
space += 3;
// Process right child first
printTree(root->right, space);
// Print current node after space
printf("\n");
for (int i = 5; i < space; i++)
printf(" ");
printf("%d\n", root->data);
// Process left child
printTree(root->left, space);
}
int main() {
struct Node* root = NULL;
root = insert(root, 50);
insert(root, 30);
insert(root, 20);
insert(root, 40);
insert(root, 70);
insert(root, 60);
insert(root, 80);
return 0;
}