0% found this document useful (0 votes)
12 views9 pages

22R21A04F7 Tejesh 3 - Week - Ds 1

The document contains three C programming assignments focused on data structures. The first assignment implements a Circular Queue using an array, allowing insertion, deletion, and display of elements. The second assignment implements a Priority Queue, and the third checks if a string is a palindrome using stack operations.

Uploaded by

varshuparupalli
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)
12 views9 pages

22R21A04F7 Tejesh 3 - Week - Ds 1

The document contains three C programming assignments focused on data structures. The first assignment implements a Circular Queue using an array, allowing insertion, deletion, and display of elements. The second assignment implements a Priority Queue, and the third checks if a string is a palindrome using stack operations.

Uploaded by

varshuparupalli
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/ 9

DATA STRUCTURES LAB

WEEK -3
NAME: K. TEJESH K

ROLL NO.: 22R21A04F7

DATE: 10/10/2023

1)Problem statement:Write a C Program to Implement a Circular Queue


using Array and perform
• Insert integers into the Queue
• Delete three elements from Queue
 Display the Queue Elements

input: char ch,a,r,d,e,int values


Syntax:
#define n size
int q[n], front = , rear = ;
bool fF() { return (front ==); }
bool eE() { return (front == rear); }
void enQ() { if (condition) return; }
int deQ() { if (condition)return; }
int main() { char variable;
do { switch(variable) { case 'a': enQ(); break;
case 'r': statement
break;
case 'd':
for (initilization;condition;increment/decrement)
block of statements
break; } } while (condition); return ; }
PROGRAM :
#include <stdio.h>
#include<stdlib.h>
#include<stdbool.h>
#define n 5
int queue[n],front=-1,rear=-1;
bool isfull()
{if (rear==(front+n-1)%n)
return true;
else
return false;}
bool isempty()
{if ((rear==-1)&&(front=-1))
return true;
else
return false;}
void enqueue()
{if(isfull())
printf("over the flow condition");
else
{if (isempty())
front=0;
rear=(rear+1)%n;
printf ("enter data");
scanf("%d",&queue[rear]);}}
void dequeue()
{if(isempty())
printf("under flow condition");
else
{printf("the dequeued element is %d\n",queue[front]);
front=(front+1)%n;}
if(front==(rear+1)%n)
front=rear=-1;}
void display()
{int i;
printf("the elements in queue` are:");
i=front;
do
{printf("%d\t",queue[i]);
i=(i+1)%n;}
while(i!=(rear+1)%n);}
int main()
{char ch;
do
{printf("enter a to add,r to remove,d to display,e to exit\n");
scanf("%s",&ch);
switch(ch)
{case'a': enqueue();
break;
case'r': dequeue();
break;
case'd' : display();
break;
case'e' : exit(0);
break;
default : printf("enter valid input:");
break;}}
while(ch!='e');
return 0;}
Output:-enter a to add,r to remove,d to display,e to exit
a
enter data2
enter a to add,r to remove,d to display,e to exit
a
enter data5
enter a to add,r to remove,d to display,e to exit
a
enter data8
enter a to add,r to remove,d to display,e to exit
a
enter data3
enter a to add,r to remove,d to display,e to exit
a
enter data1
enter a to add,r to remove,d to display,e to exit
r
the dequeued element is 2
enter a to add,r to remove,d to display,e to exit
r
the dequeued element is 5
enter a to add,r to remove,d to display,e to exit
d
the elements in queue` are:8 3 1 enter a to add,r to remove,d to display,e to exit
e
Testcase:

2)Problem statement:Write a C Program to Implement a Priority Queue using


Array and perform
• Insert integers into the Queue
• Delete three elements from Queue
Display the Queue Elements

input: char ch,a,r,d,e,int values


Syntax:
#define N size
Int declarition of variables;
bool I() { block of statements; }
bool F() {block of statements; }
void I(int variable) { if (condition) return;
if (condition) block of statements;
else { for (initilization;condition;incremant / decrement) { if (condition) block of statements;else break; } } }
int D() { if (condition) block of statements;
else increment; return Variable; }
int main() { char C;
while (condition) { blockofstatements;
switch (expression) {
case value1: break;
case value2: break;
default:
Return; }}}
PROGRAM :
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#define n 100
int priorityQueue[n];
int front = -1;
int rear = -1;
int isEmpty() {
return (front == -1 && rear == -1);}
int isFull() {
return (rear == n - 1);}
void insert(int value) {
if (isFull()) {
printf("Queue is full. Cannot insert.\n");
return;}
if (isEmpty()) {
front = rear = 0;
priorityQueue[rear] = value;}
else {
int i;
for (i = rear; i >= front; i--) {
if (value > priorityQueue[i]) {
priorityQueue[i + 1] = priorityQueue[i];
} else {
break;}}
priorityQueue[i + 1] = value;
rear++;}}
int delete() {
if (isEmpty()) {
printf("Queue is empty. Cannot delete.\n");
return -1;}
int deletedValue = priorityQueue[front];
if (front == rear) {
front = rear = -1;
} else {
front++;}
return deletedValue;}
void display() {
if (isEmpty()) {
printf("Queue is empty.\n");
return;}
printf("Queue elements: ");
for (int i = front; i <= rear; i++) {
printf("%d ", priorityQueue[i]);}
printf("\n");}
int main() {
char ch;
int value;
while (1) {
printf("a to add, r to remove,d to display,e to exit\n");
printf("Enter your choice: ");
scanf("%s", &ch);
switch (ch) {
case 'a':
printf("Enter an integer to insert: ");
scanf("%d", &value);
insert(value);
break;
case 'r':
value = delete();
if (value != -1) {
printf("Deleted element: %d\n", value);}
break;
case 'd':
display();
break;
case 'e':
printf("Exiting the program.\n");
return 0;
default:
printf("Invalid choice. Please try again.\n");}}
return 0;}

Output: Enter your choice: a


Enter an integer to insert: 3

a to add, r to remove,d to display,e to exit

Enter your choice: a

Enter an integer to insert: 6

a to add, r to remove,d to display,e to exit

Enter your choice: a

Enter an integer to insert: 1

a to add, r to remove,d to display,e to exit

Enter your choice: a

Enter an integer to insert: -5

a to add, r to remove,d to display,e to exit

Enter your choice: a

Enter an integer to insert: 9

a to add, r to remove,d to display,e to exit

Enter your choice: r

Deleted element: 9

a to add, r to remove,d to display,e to exit

Enter your choice: r

Deleted element: 6

a to add, r to remove,d to display,e to exit

Enter your choice: d

Queue elements: 3 1 -5

a to add, r to remove,d to display,e to exit

Enter your choice: e

Exiting the program


Testcase:

3) Problem statement:Write a C program to check string is


palindrome using stack operations For example, if string is “MADAM”,
the function should print

input: top;
char items[n];
char str[n];
Syntax:
bool isPalindrome(char str[]) {
int len = strlen(str);
for (initilization;condition;incremant / decrement) { if (condition) {
if (condition) {
block of statements
return false;}}
return true;}
int main() {
char str[size];
block of statements;
If(condition){block of statements;
}
Else{
block of statements;}
return 0;
}

PROGRAM :
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#define n 100
struct Stack {
int top;
char items[n];
};
void push(struct Stack *s, char c) { s->items[++s->top] = c; }
char pop(struct Stack *s) { return s->items[s->top--]; }
bool isEmpty(struct Stack *s) { return s->top == -1; }
bool isPalindrome(char str[]) {
struct Stack stack = { -1 };
int len = strlen(str), i;
for (i = 0; i < len / 2; i++) push(&stack, str[i]);
if (len % 2 != 0) i++;
while (str[i]) if (pop(&stack) != str[i++]) return false;
return true;
}
int main() {
char str[n];
printf("Enter a string: ");
scanf("%s", str);
printf(isPalindrome(str) ? "PALINDROME\n" : "NOT a PALINDROME\n");
return 0;}

Output: Enter a string: madam


PALINDROME

Testcase:

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