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

Data Structures

example code

Uploaded by

tharun26122005
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Data Structures

example code

Uploaded by

tharun26122005
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

DSA

1. CREATE MENU DRIVEN PROGRAM WITH


a. Creation of List.
b. Insertion of data in the List
c. Deletion of data from the List
d. Display all data‟s in the List
e. Searching for a data in the list

CODE:
#include<stdio.h>
#include<stdlib.h>
#define MAX 10

void Insert();
void Delete();
void Search();
void Display();
void Create();
void Reverse();
int arr[MAX],i,n,pos,x,s;
main()
{
int choice;
while(1){
printf("\n");

printf(" MAIN MENU");


printf("\n 1.CREATE \n 2.INSERT \n 3.DELETE \n 4.SEARCH \n 5.DISPLAY \n 6.EXIT \n
7.reverse");
printf("\n Enter the Choice\n");
scanf("%d",&choice);
switch(choice)
{
case 2:
{
Insert();
break;
}
case 3:
{
Delete();
break;
}
case 4:
{
Search();
break;
}
case 5:
{
Display();
break;
}
case 1:
{
Create();
break;
}
case 6:
{
exit;
break;
}
case 7:
{
Reverse(arr);
break;
}
default:
{
printf("Enter correct choice");
break;
}
}
}

}
void Create()
{
printf("Enter size of array");
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%d",&arr[i]);
}
}
void Insert()
{
printf("Enter the position");
scanf("%d",&pos);
if(pos>=n)
{
printf("error");
}
else
{
for(i=n-1;i>=pos;i--)
{
arr[i+1]=arr[i];
}
printf("Enter the data");
scanf("%d",&x);
arr[pos-1]=x;
n++;
}
}

void Delete()
{
printf("Enter the position to delete");
scanf("%d",&pos);
if(pos>=n)
{
printf("error");
}
else
{
for(i=pos;i<n;i++)
{
arr[i-1]=arr[i];
}
printf("Enter the data");
scanf("%d",&x);
arr[pos-1]=x;
n--;
}
}
void Search()
{ int flag=0;
printf("Enter the number to be find");
scanf("%d",&s);
for(i=0;i<n;i++)
{
if(arr[i]==s)
{
printf("Element Found at position %d",i+1);
flag=0;
break;

}
else
flag=1;
}
if(flag==0)
{
printf("");
}
else{
printf("element not found");
}

/*if(i==n)
{
printf("ELement not found");
}
*/
}
void Display()
{
printf("Elements are:\n");
for(i=0;i<n;i++)
{
printf("%d\t",arr[i]);
}
}
void Reverse(int a[])
{
int end,start,temp;
printf("Entered ordered\n");
for(i=0;i<n;i++)
{
printf("%d\t",a[i]);
}
printf("\n");
printf("the reversed array:\n");
end=n-1;
start=0;
while(start<end){
temp=a[start];
a[start]=a[end];
a[end]=temp;
start++;
end--;

for(i=0;i<n;i++)
{
printf("%d\t",a[i]);
}
}

OUTPUT:
2.

CODE:

#include<stdio.h>
#include<stdlib.h>
#define MAX 50
int top=-1;
int stack[MAX];
void peek(){
printf("The top is %d",stack[top]);
}

int isempty()
{
if(top==-1)
{
return 0;
}
else{
return 1;
}
}

int isfull()
{
if(top==MAX-1){
return 1;
}

else
{
return 0;
}
}

void push(){
int x;
printf("Enter the data to be pushed"); scanf("%d",&x);
if(!isfull()){
top=top+1;
stack[top]=x;
}
else
printf("the stack is full!!");
}

void pop(){
if(!isempty())
{
printf("The popped element is %d",top);
top=0;
top=top-1;
}
else

printf("the stack is empty!!");


}

void display(){
int i;
for(i=top;i>=0;i--)
{
printf("%d\n",stack[i]);

}
}
void top10()
{
int i; int j=9;

for(i=top;i>j;i--)
{
pop();
}
for(i=j;i>=0;i--)
{
printf("\n");
printf("The %d student's rego. is %d\n",i,stack[i]);
}
}

void main()

{
int choice; while(1){

printf("MAIN MENU");
printf("\n 1.push \n 2.pop \n 3.peek \n 4.display \n 5.EXIT \n 6.top10"); printf("\n Enter the
Choice\n");
scanf("%d",&choice);

switch(choice)
{
case 1:
{
push();
break;
}

case 2:
{
pop();
break;

case 3:
{
peek();
break;

case 4:
{
display();
break;
}
case 5:
{
exit;
break;
}
case 6:
{
top10();
break;
}
default:{

printf("enter proper value my child!!");


break;
}
}
}
}

OUTPUT
3. Most of the bugs in scientific and engineering applications are
due to improper usage of precedence order in arithmetic
expressions. Thus it is necessary to use an appropriate notation
that would evaluate the expression without taking into account
the precedence order and parenthesis.
a) Write a program to convert the given arithmetic expression
into
i) Reverse Polish notation(Postfix)
ii) Polish notation(Prefix)
b) Evaluate the above notations with necessary input

CODE

#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
int precedence(char elem);
void push(char a);
char pop();
void postFix();
void preFix();
float postEval(char *arr);
float preEval(char *arr);
char postfx[100] = {'\0'}, prefx[100] = {'\0'},symStack[100];
float valStack[100];
int top = -1;
int main()
{
int x;
float ans, ans1;

while(1)
{
printf("\nSelect the required operation:\n1.Convert to postfix expression\n2.Convert to
prefix expression\n3.Evaluate the postfix expression\n4.Evalute the prefix expression\n5.Exit");
scanf("%d",&x);

switch(x)
{
case 1:
postFix();
break;
case 2:
preFix();
break;
case 3:
if(postfx[0] == '\0')
printf("\nPostFix expression is not found! Please provide a infix expression");
else
{
ans1 = postEval(postfx);
printf("\nThe answer is: %.2f\n",ans1);
postfx[0]='\0';
}
break;
case 4:
if(prefx[0] == '\0')
printf("\nPreFix expression is not found! Please provide a infix expression");
else
{
ans = preEval(prefx);
printf("\nThe answer is: %.2f\n",ans);
prefx[0]='\0';
}
break;
case 5:
exit(0);
break;
default:
printf("\nInvalid selection!");
}
}

return 0;
}

float preEval(char *arr)


{
int length,val, top = -1;
strrev(arr);
length = strlen(arr);
for(int i = 0; i<length; i++)
{
if(isalpha(arr[i]))
{
printf("Enter the value for %c: ",arr[i]);
scanf("%d",&val);
valStack[++top]=val;
} else if(arr[i] == '+' || arr[i] == '-' || arr[i] == '*' || arr[i] == '/')
{
if(arr[i]=='+')
{
valStack[top-1] = valStack[top] + valStack[top-1];
top--;
} else if(arr[i]=='-')
{
valStack[top-1] = valStack[top] - valStack[top-1];
top--;
} else if(arr[i]=='*')
{
valStack[top-1] = valStack[top] * valStack[top-1];
top--;
} else if(arr[i]=='/')
{
valStack[top-1] = valStack[top] / valStack[top-1];
top--;
}
}
}
return valStack[top];
}

float postEval(char *arr)


{
int length,val,top = -1;

length = strlen(arr);
for(int i = 0; i<length; i++)
{
if(isalpha(arr[i]))
{
printf("Enter the value for %c: ",arr[i]);
scanf("%d",&val);
valStack[++top]=val;
} else if(arr[i] == '+' || arr[i] == '-' || arr[i] == '*' || arr[i] == '/')
{
if(arr[i]=='+')
{
valStack[top-1] = valStack[top-1] + valStack[top];
top--;
} else if(arr[i]=='-')
{
valStack[top-1] = valStack[top-1] - valStack[top];
top--;
} else if(arr[i]=='*')
{
valStack[top-1] = valStack[top-1] * valStack[top];
top--;
} else if(arr[i]=='/')
{
valStack[top-1] = valStack[top-1] / valStack[top];
top--;
}
}
}
return valStack[top];
}

void preFix()
{
int j = 0, i =0, length;
char infix[100];

printf("Enter your infix expression: ");


scanf("%s",infix);
length = strlen(infix);
strrev(infix);
length = strlen(infix);
for(int i=0; i<length; i++)
{
if(isalpha(infix[i]))
{
prefx[j++] = infix[i];
} else if(infix[i] == ')')
{
push(infix[i]);
} else if(infix[i] == '(')
{
while(symStack[top]!= ')')
{
prefx[j++] = pop();
}
pop();
} else
{
while(precedence(symStack[top]) > precedence(infix[i]))
{
prefx[j++] = pop();
}
push(infix[i]);
}
}

while(top!= -1)
prefx[j++] = pop();
prefx[j] = '\0';
strrev(prefx);

printf("\nThe prefix expression is %s\n",prefx);


}

void postFix()
{
int length, j = 0;
char infix[100];

printf("Enter your infix expression: ");


scanf("%s",infix);
length = strlen(infix);

for(int i=0; i<length; i++)


{
if(isalpha(infix[i]))
{
postfx[j++] = infix[i];
} else if(infix[i] == '(')
{
push(infix[i]);
} else if(infix[i] == ')')
{
while(symStack[top]!= '(')
{
postfx[j++] = pop();
}
pop();
} else
{
while(precedence(symStack[top]) >= precedence(infix[i]))
{
postfx[j++] = pop();
}
push(infix[i]);
}
}

while(top != -1)
postfx[j++] = pop();
postfx[j] = '\0';

printf("\nThe postfix expression is %s\n",postfx);


}

void push(char a)
{
top++;
symStack[top] = a;
}

char pop()
{
return (symStack[top--]);
}

int precedence(char elem)


{
switch(elem)
{
case '(':
case ')':
return 0;

case '+':
case '-':
return 1;

case '/':
case '*':
return 2;

}
}

OUTPUT

4. Implement Priority Queue for the following operations


1. Isempty
2. Isfull
3. Enueue
4. Dequeue
5. Display

CODE;
#include<stdio.h>
#include<stdlib.h>
#define SIZE 5
int f=0,r=-1;
typedef struct PRQ
{
int ele;
int pr;
}PriorityQ;

PriorityQ PQ[SIZE];

int Qfull()
{
if(r==SIZE-1) return 1;
return 0;
}

int Qempty()
{
if(f > r) return 1;
return 0;
}

void display()
{
int i;
if(Qempty()) printf(" \n Empty Queue\n");
else
{
printf("Front->");
for(i=f;i<=r;i++)
printf("[%d,%d] ",PQ[i].ele,PQ[i].pr);
printf("<-Rear");
}
}

void PQinsert()
{
int elem,pre;
printf("enter the element");
scanf("%d",&elem);
printf("Enter its precedency");
scanf("%d",&pre);
int i;
if( Qfull()) printf("\n\n Overflow!!!!\n\n");
else
{
i=r;
++r;
while(PQ[i].pr >= pre && i >= 0) /* Find location for new elem */
{
PQ[i+1]=PQ[i];
i--;
}
PQ[i+1].ele=elem;
PQ[i+1].pr=pre;
}
}

PriorityQ PQdelete()
{ /* Function for Delete operation */
PriorityQ p;
if(Qempty()){ printf("\n\nUnderflow!!!!\n\n");
p.ele=-1;p.pr=-1;
return(p); }
else
{
p=PQ[f];
f=f+1;
return(p);
}
}
main(){
int choice;
int flag=1;
while(flag==1)
{
printf(" MENU ");
printf("\n 1.Insert\n 2.Delete\n 3.Display\n 4.isfull \n 5.isempty\n");
printf("Enter the choice\n ");
scanf("%d",&choice);
switch(choice)
{
case 1:
PQinsert();
break;

case 2:
PQdelete();
break;
case 3:
display();
break;

case 4:
printf("True if its 0");
Qfull();
break;

case 5:
printf("True if its 0");
Qempty();
break;

case 6:
flag=0;
break;

default:
printf("enter proper value");
break;
}
}
}

Output:
5. In a theme park, the Roller-Coaster ride is started only when a
good number of riders line up in the counter (say 20 members).
When the ride proceeds with these 20 members, a new set of riders
will line up in the counter. This keeps continuing. Implement the
above scenario of lining up and processing using arrays with Queue
ADT.
Low Level: Implement the aforementioned problem [6 Marks]
Middle Level: Also count the number of adults and children [2
Marks]
High Level: If a VIP family arrives, process them before processing
others who are already waiting in the queue

CODE;
#include <stdio.h>
#include <stdlib.h>
#define MAX_SIZE 20
typedef struct {
int age; // Age of the rider
int is_vip; // 1 if VIP, 0 if not
} Rider;
Rider queue[MAX_SIZE];
int rear = -1;
int front = -1;
int num_children = 0;
int num_adults = 0;
void insert();
void delete();
void display();
void process_vip();

int main() {
int choice;
while (1) {
printf("\n1. Insert rider to queue\n");
printf("2. Delete rider from queue\n");
printf("3. Display all riders in queue\n");
printf("4. Process VIP family\n");
printf("5. Quit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
insert();
break;
case 2:
delete();
break;
case 3:
display();
break;
case 4:
process_vip();
break;
case 5:
return 0;
default:
printf("Wrong choice\n");
}
}
}

void insert() {
if (rear == MAX_SIZE - 1) {
printf("Queue is full. Come in next ride.\n");
return;
}

Rider new_rider;
printf("Enter rider age: ");
scanf("%d", &new_rider.age);

if (new_rider.age < 18)


num_children++;
else
num_adults++;

printf("Is rider a VIP? (1 for yes, 0 for no): ");


scanf("%d", &new_rider.is_vip);

rear++;
queue[rear] = new_rider;
}
void delete() {
if (front == rear) {
printf("Queue is empty\n");
return;
}

Rider deleted_rider = queue[++front];


if (deleted_rider.age < 18)
num_children--;
else
num_adults--;

printf("Rider deleted from queue: Age %d, VIP %d\n",


deleted_rider.age,
deleted_rider.is_vip);
}

void display() {
if (front == rear) {
printf("Queue is empty\n");
return;
}

printf("Queue is:\n");
for (int i = front + 1; i <= rear; i++) {
printf("Age %d, VIP %d\n", queue[i].age, queue[i].is_vip);
}
printf("Total Adults: %d, Total Children: %d\n", num_adults,
num_children);
}

void process_vip() /*allowing vip for ride*/


{
if (front == rear) {
printf("Queue is empty\n");
return;
}

Rider vip_rider = queue[++front];


if (vip_rider.age < 18)
num_children--;
else
num_adults--;
printf("Processing VIP family: Age %d, VIP %d\n", vip_rider.age,
vip_rider.is_vip);
}
OUTPUT:

You might also like