[go: up one dir, main page]

0% found this document useful (0 votes)
7 views5 pages

Week-2 Dsa Programs

The document provides implementations for a circular queue and basic array operations in C. It includes functions for inserting, deleting, displaying, searching, and updating elements in both data structures. The main programs present a menu-driven interface for user interaction with these operations.

Uploaded by

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

Week-2 Dsa Programs

The document provides implementations for a circular queue and basic array operations in C. It includes functions for inserting, deleting, displaying, searching, and updating elements in both data structures. The main programs present a menu-driven interface for user interaction with these operations.

Uploaded by

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

3.

Implementation of Circular Queue


#include<stdio.h>
#include<stdlib.h>
#define SIZE 5
void enQueue(int);
void deQueue();
void display();
int cQueue[SIZE], front = -1, rear = -1;
//main program
void main()
{
int choice, value;
while(1){
printf("\n****** MENU ******\n");
printf("\n****** CIRCULAR QUEUE ******\n");
printf("1. Insert an element\n2. Delete an element\n3. Display the
elements \n4. Exit\n");
printf("Enter your choice: ");
scanf("%d",&choice);
switch(choice){
case 1: printf("\nEnter the value to be insert: ");
scanf("%d",&value);
enQueue(value);
break;
case 2: deQueue();
break;
case 3: display();
break;
case 4: exit(0);
break;
default: printf("\nPlease select the correct choice!!!\n");
}
}
}
// enQueue operation
void enQueue(int value)
{
if((front == 0 && rear == SIZE - 1) || (front == rear+1))
printf("\nCircular Queue is Full! Insertion not possible!!!\n");
else{
if(rear == SIZE-1 && front != 0)
rear = -1;
cQueue[++rear] = value;
printf("\nInsertion Success!!!\n");
if(front == -1)
front = 0;
}
}
// deQueue operation

void deQueue()
{
if(front == -1 && rear == -1)
printf("\nCircular Queue is Empty! Deletion is not possible!!!\n");
else{
printf("\nDeleted element : %d\n",cQueue[front++]);
if(front == SIZE)
front = 0;
if(front-1 == rear)
front = rear = -1;
}
}
// Display operation
void display()
{
if(front == -1)
printf("\nCircular Queue is Empty!!!\n");
else{
int i = front;
printf("\nCircular Queue Elements are : \n");
if(front <= rear){
while(i <= rear)
printf("%d\t",cQueue[i++]);
}
else{
while(i <= SIZE - 1)
printf("%d\t", cQueue[i++]);
i = 0;
while(i <= rear)
printf("%d\t",cQueue[i++]);
}
}}
Output:
//Implementation of Array ADT.
#include<stdio.h>
int a[5],i,n,j,value;
//Insert operation
void insert()
{
printf("Enter 5 integers: ");
for(int i = 0; i < 5; ++i)
{
scanf("%d", &a[i]);
}
}
//Delete operation
void delete()
{
printf("\nEnter an element to delete\n");
scanf("%d",&n);
for(i=0;i<5;i++)
{
if(a[i]==n)
{
for(j=i;j<=4;j++)
{
if(j==4)
{
a[j]=0;
}
else
{
a[j]=a[j+1];
}
}
break;
}
}
}
//Display operation
void display()
{
for(i=0;i<5;i++)
{
printf("%d ",a[i]);
}
}
//Search operation
void search()
{
printf("\nEnter an element to search\n");
scanf("%d",&n);
for(i=0;i<5;i++)
{
if(a[i]==n)
{
printf("\nthe searching element %d found at %d index",n,i);
}
}
}
//update operation
void update()
{
int index;
printf("enter the index number and value which u want to update:");
scanf("%d%d",&index,&value);
a[index]=value;
}
//Main program
int main()
{
int ch;
do
{
printf("\n**** MENU ****");
printf("\n**** ARRAY OPEARTIONS ****");
printf("\n1:insert()\n2:delete()\n3:display()\n4:search()\n5:update()\n");
printf("\nEnter Your Choice:");
scanf("%d",&ch);
switch(ch)
{
case 1: insert();
break;
case 2: delete();
break;
case 3: display();
break;
case 4: search();
break;
case 5: update();
break;
default: printf("choose correct option");
}
}while(ch!=0);
}

Output:

You might also like