Ads

Download as pdf or txt
Download as pdf or txt
You are on page 1of 13

7

#include <stdio.h>

#include <stdlib.h>

#define MAX 5 // Define the maximum size of the Circular Queue

typedef struct {

char data[MAX];

int front, rear, size;

} CircularQueue;

void initializeQueue(CircularQueue *q) {

q->front=0;

q->rear=-1;

q->size=0;

int isFull(CircularQueue *q) {

return (q->size == MAX);

int isEmpty(CircularQueue *q) {

return (q->size == 0);

void insertElement(CircularQueue *q, char element) {

if (isFull(q)){

printf("Queue Overflow\n");

}else{

q->rear=(q->rear+1)%MAX;

q->data[q->rear]=element;

q->size++;

printf("Inserted element '%c'\n",element);

}}

void deleteElement(CircularQueue *q) {

if (isEmpty(q)){

printf("Queue Underflow\n");
}else{

char element=q->data[q->front];

q->front=(q->front+1)%MAX;

q->size--;

printf("Deleted element '%c'\n",element);

}}

void displayStatus(CircularQueue *q) {

if(isEmpty(q)){

printf("Queue is empty\n");

}else{

int i=q->front;

for(int count=0;count<q->size;count++){

printf("%c ",q->data[i]);

i=(i+1) % MAX;

printf("\n");

int main() {

CircularQueue q;

initializeQueue(&q);

int choice;

char element;

do {

printf("Circular Queue Menu:\n");

printf("1. Insert Element\n");

printf("2. Delete Element\n");

printf("3. Display Queue Status\n");

printf("4. Exit\n");

printf("Enter your choice: ");

scanf("%d", &choice);

switch (choice) {

case 1:
printf("Enter the character to insert: ");

scanf(" %c", &element);

insertElement(&q, element);

break;

case 2:

deleteElement(&q);

break;

case 3:

displayStatus(&q);

break;

case 4:

printf("Exit\n");

break;

default:

printf("Invalid choice\n");

break;

} while (choice != 4);

return 0; }
8
#include <stdio.h>

#include<string.h>

#include<stdlib.h>

struct node{

int usn;

char name[10];

struct node *next;

};

struct node *head=NULL;

void insertionatfront(){

struct node *newnode;

newnode=(struct node*)malloc(sizeof(struct node));

printf("\nEnter name and USN of student with space:");

scanf("%s %d",&newnode->name,&newnode->usn);

if(head==NULL){

head=newnode;

newnode->next=NULL;

else{

newnode->next=head;

head=newnode;

void insertionatend(){

struct node *newnode,*temp=head;

newnode=(struct node*)malloc(sizeof(struct node));

printf("\nEnter name and USN of student with space:");

scanf("%s %d",&newnode->name,&newnode->usn);
if(head==NULL){

head=newnode;

newnode->next=NULL;

else{

while(temp->next!=NULL){

temp=temp->next;

temp->next=newnode;

newnode->next=NULL;

void deleteatfront(){

struct node *temp=head;

if(head==NULL){

printf("\nlinked list is empty!!");

else{

head=head->next;

free(temp);

void deleteatend(){

struct node *prev,*temp=head;

if(head==NULL){

printf("\nlinked list is empty!!");

else{
while(temp->next!=NULL){

prev=temp;

temp=temp->next;

prev->next=NULL;

free(temp);

void display(){

struct node *temp=head;

if(head==NULL){

printf("\nlinked list is empty!!");

else{

while(temp!=NULL){

printf("\nName of the student and usn are:%s %d",temp->name,temp->usn);

temp=temp->next;

int main() {

int choice;

do {

printf("\nMenu:\n");

printf("1. Insert at Front\n");

printf("2. Display List\n");

printf("3. Insert at End\n");

printf("4. Delete from Front\n");


printf("5. Delete from End\n");

printf("6. Exit\n");

printf("Enter your choice: ");

scanf("%d", &choice);

switch (choice) {

case 1:

insertionatfront();

break;

case 2:

display();

break;

case 3:

insertionatend();

break;

case 4:

deleteatfront();

break;

case 5:

deleteatend();

break;

case 6:

printf("Exiting program.\n");

break;

default:

printf("Invalid choice! Try again.\n");

} while (choice != 6);

return 0;

}
9
#include <stdio.h>

#include<string.h>

#include<stdlib.h>

struct node{

int usn;

char name[10];

struct node *next;

struct node *prev;

};

struct node *head=NULL;

void insertionatfront(){

struct node *newnode;

newnode=(struct node*)malloc(sizeof(struct node));

printf("\nEnter name and USN of student with space:");

scanf("%s %d",&newnode->name,&newnode->usn);

if(head==NULL){

head=newnode;

newnode->next=NULL;

newnode->prev=NULL;

else{

newnode->next=head;

newnode->prev=NULL;

head=newnode;

void insertionatend(){

struct node *newnode,*temp=head;


newnode=(struct node*)malloc(sizeof(struct node));

printf("\nEnter name and USN of student with space:");

scanf("%s %d",&newnode->name,&newnode->usn);

if(head==NULL){

head=newnode;

newnode->next=NULL;

newnode->prev=NULL;

else{

while(temp->next!=NULL){

temp=temp->next;

newnode->prev=temp;

temp->next=newnode;

newnode->next=NULL;

void deleteatfront(){

struct node *temp=head;

if(head==NULL){

printf("\nlinked list is empty!!");

else{

head=head->next;

head->prev=NULL;

free(temp);

void deleteatend(){
struct node *p,*temp=head;

if(head==NULL){

printf("\nlinked list is empty!!");

else{

while(temp->next!=NULL){

p=temp;

temp=temp->next;

p->next=NULL;

free(temp);

void display(){

struct node *temp=head;

if(head==NULL){

printf("\nlinked list is empty!!");

else{

while(temp!=NULL){

printf("\nName of the student and usn are:%s %d",temp->name,temp->usn);

temp=temp->next;

int main() {

int choice;
do {

printf("\nMenu:\n");

printf("1. Insert at Front\n");

printf("2. Display List\n");

printf("3. Insert at End\n");

printf("4. Delete from Front\n");

printf("5. Delete from End\n");

printf("6. Exit\n");

printf("Enter your choice: ");

scanf("%d",&choice);

switch (choice) {

case 1:

insertionatfront();

break;

case 2:

display();

break;

case 3:

insertionatend();

break;

case 4:

deleteatfront();

break;

case 5:

deleteatend();

break;

case 6:

printf("Exiting program.\n");

break;

default:

printf("Invalid choice! Try again.\n");

}
} while (choice != 6);

return 0;

}
12
#include<stdio.h>

int binary_search_middle_element(int arr[], int size){

int left=0;

int right=size-1;

int middle=(left+right)/2;

return arr[middle];

int main(){

int arr[]={1,3,5,7,9,11,13};

int size=sizeof(arr)/sizeof(arr[0]);

int middle_element=binary_search_middle_element(arr,size);

printf("the middle element is :%d",middle_element);

return 0;

You might also like