Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Download as pdf or txt
Download as pdf or txt
You are on page 1of 111

CSE2011 LAB 1

Introduction to C

Name: MEHALI SAMANTA


Registration Number: 20BEC1204

Program 1

Code
#include<stdio.h>
int main()
{
float weight,height;
scanf("%f",&weight);
scanf("%f",&height);
float b;
b=weight/(height*height);
if(b>25)
{
printf("Obese");
}
else
{
printf("Healthy");
}
return 0;
}

Output
Program 2

Code
#include<stdio.h>
int main()
{
int l,v,a,p,total,t;
scanf("%d\n%d\n%d\n%d",&l,&v,&a,&p);
total=l+v+a+p;
t=(60/100)*50;
if(total>=180)
{
if(l>t && v>t && a>t && p>t)
{
printf("Eligible for Genius level 1");
}
else
{
printf("Elgible for Genius level 2");
}
}
else
{
printf("Not eligible for genius level");
}
return 0;
}

Output
Program 3

Code
#include<stdio.h>
int main()
{
int n;
float t;
scanf("%d",&n);
int h=0;
int p=0;
int c=0;
for(int i=0; i<n; i++)
{
scanf("%f",&t);
if(t>=85)
{
h++;
}
else if(t>=60 && t<85)
{
p++;
}
else
{
c++;
}
}
printf("\nPlaces that are HOT:%d",h);
printf("\nPlaces that are PLEASANT:%d",p);
printf("\nPlaces that are COLD:%d",c);
return 0;
}

Output
Program 4

Code
#include<stdio.h>
int main()
{
int n[10],i;
printf("Enter the elements of the array \n");
for(i=0;i<10;i++)
{
scanf("%d\n",&n[i]);
}
printf("Even numbers in the array are - ");
for(i=0;i<10;i++)
{
if(n[i]%2==0)
{
printf("%d \t",n[i]);
}
}
printf("\n Odd numbers in the array are - ");
for(i=0;i<10;i++)
{
if(n[i]%2!=0)
{
printf("%d \t",n[i]);
}
}
return 0;
}

Output
Program 5
Code
#include<stdio.h>
int main()
{
int A[3][2]={42,54,36,27,31,30};
int B[3][2]={27,31,28,15,28,22};
float C[1][3]={0.75,0.55,1.20};
float profit=0.0;
for(int i=0;i<3;i++)
{
for(int j=0;j<2;j++)
{
profit=profit+((A[i][j]-B[i][j])*C[0][i]);
}
}
printf("Profit:$%.2f",profit);
return 0;
}
Output
CSE2011
LAB II
STACK IMPLEMENTATION

Name: MEHALI SAMANTA


Registration Number: 20BEC1204

Algorithms
isEmpty()
Algorithm to check stack is empty or not
1. If top = -1 then
return 1;
2. Otherwise
return 0;
3. End of if
4. Exit

isFull()
Algorithm to check stack is full or not
1. If top = n – 1 then
return 1;
2. Otherwise
return 0;
3. End of if
4. Exit

push()
Algorithm to push an element into stack
1. If isFull() returns 1
Print “Stack is over flow”;
Exit;
2. Otherwise
Read element to be pushed,x
top=top+1;
stack(top)=x;
3. End of if
4. Exit

pop()
Algorithm to pop an element from stack
1. If isEmpty() returns 1
Print “Stack is under flow”;
Exit;
2. Otherwise
Print the popped element, stack[top];
top=top-1;
3. End of if
4. Exit
display()
Algorithm to display the stack and ask for the user’s choice
1. If top>=0
Start a loop to print the elements of the Stack
Print the elements
Loop ends
Take user’s next choice
Exit;
2. Otherwise
Print “The STACK is empty”;
3. End of if
4. Exit

main()
Algorithm for the execution of the main function
1. Initialize top = -1;
2. Read the sixe of the Stack,n;
3. Display the stack operations available,”1.PUSH 2.POP
3.DISPLAY 4.EXIT”;
4. Start a Do-While loop
Accept the user’s choice of operation,choice
5. switch(choice)
case ‘1’: push();
exit;
case ‘2’: pop();
exit;
case ‘3’: display();
exit;
case ‘4’: Print “EXIT POINT”;
exit;
Print “Please Enter a Valid Choice(1/2/3/4)", if valid
choice is not given
End of switch
6. Continue the loop till the choice value is not equal to 4
7. Otherwise
8. Exit

Code

#include<stdio.h>
int stack[10],choice,n,top,x,i;
int isEmpty(void);
int isFull(void);
void push(void);
void pop(void);
void display(void);
int main()
{
top=-1;
printf("\n Enter the size of STACK[MAX=10]:");
scanf("%d",&n);
printf("\n\t STACK OPERATIONS USING ARRAY");
printf("\n\t 1.PUSH\n\t 2.POP\n\t 3.DISPLAY\n\t 4.EXIT");
do
{
printf("\n Enter the Choice:");
scanf("%d",&choice);
switch(choice)
{
case 1:
{
push();
break;
}
case 2:
{
pop();
break;
}
case 3:
{
display();
break;
}
case 4:
{
printf("\n\t EXIT POINT ");
break;
}
default:
{
printf ("\n\t Please Enter a Valid Choice(1/2/3/4)");
}

}
}
while(choice!=4);
return 0;
}
int isEmpty()
{
if(top==-1)
{
return 1;
}
else
{
return 0;
}
}
int isFull()
{
if(top==n-1)
{
return 1;
}
else
{
return 0;
}
}
void push()
{
if(isFull()==1)
{
printf("\n\tSTACK is over flow");

}
else
{
printf(" Enter a value to be pushed:");
scanf("%d",&x);
top++;
stack[top]=x;
}
}
void pop()
{
if(isEmpty()==1)
{
printf("\n\t Stack is under flow");
}
else
{
printf("\n\t The popped elements is %d",stack[top]);
top--;
}
}
void display()
{
if(top>=0)
{
printf("\n The elements in STACK \n");
for(i=top; i>=0; i--)
printf("\n%d",stack[i]);
printf("\n Press Next Choice");
}
else
{
printf("\n The STACK is empty");
}

}
Output
CSE2011
LAB III
INFIX TO PREFIX CONVERSION

Name: MEHALI SAMANTA


Registration Number: 20BEC1204

Algorithm
1. Start
2. Push “)” onto STACK, and add “(“ to end of the A
3. Scan A from right to left and repeat step 3 to 6 for each
element of A until the STACK is empty
4. If an operand is encountered add it to B
5. If a right parenthesis is encountered push it onto STACK
6. If an operator is encountered then, Repeatedly pop from
STACK and add to B each operator (on the top of STACK)
which has same or higher precedence than the operator,
otherwise add operator to STACK.
7. If left parenthesis is encountered then, Repeatedly pop from the
STACK and add to B (each operator on top of stack until a left
parenthesis is encountered), otherwise remove the left
parenthesis
8. Stop.
Code

# include <stdio.h>
# include <string.h>
# define MAX 20
void infixtoprefix(char infix[20],char prefix[20]);
void reverse(char array[30]);
char pop();
void push(char symbol);
int isOperator(char symbol);
int prcd(symbol);
int top=-1;
char stack[MAX];
main() {
char infix[20],prefix[20],temp;
printf("Enter infix operation: ");
gets(infix);
infixtoprefix(infix,prefix);
reverse(prefix);
puts((prefix));
}
void infixtoprefix(char infix[20],char prefix[20]) {
int i,j=0;
char symbol;
stack[++top]='#';
reverse(infix);
for (i=0;i<strlen(infix);i++) {
symbol=infix[i];
if (isOperator(symbol)==0) {
prefix[j]=symbol;
j++;
} else {
if (symbol==')') {
push(symbol);
} else if(symbol == '(') {
while (stack[top]!=')') {
prefix[j]=pop();
j++;
}
pop();
} else {
if (prcd(stack[top])<=prcd(symbol)) {
push(symbol);
} else {
while(prcd(stack[top])>=prcd(symbol))
{
prefix[j]=pop();
j++;
}
push(symbol);
}
//end for else
}
}
//end for else
}
//end for for
while (stack[top]!='#') {
prefix[j]=pop();
j++;
}
prefix[j]='\0';
}
void reverse(char array[30]) {
int i,j;
char temp[100];
for (i=strlen(array)-1,j=0;i+1!=0;--i,++j) {
temp[j]=array[i];
}
temp[j]='\0';
strcpy(array,temp);
return array;
}
char pop() {
char a;
a=stack[top];
top--;
return a;
}
void push(char symbol) {
top++;
stack[top]=symbol;
}
int prcd(symbol)
{
switch(symbol) {
case '+':
case '-':
return 2;
break;
case '*':
case '/':
return 4;
break;
case '$':
case '^':
return 6;
break;
case '#':
case '(':
case ')':
return 1;
break;
}
}
int isOperator(char symbol) {
switch(symbol) {
case '+':
case '-':
case '*':
case '/':
case '^':
case '$':
case '&':
case '(':
case ')':
return 1;
break;
default:
return 0;
// returns 0 if the symbol is other than given above
}
}
Output
CSE2011
LAB IV
PRINTING JOBS USING QUEUE

Name: MEHALI SAMANTA


Registration Number: 20BEC1204

Algorithms

enqueue()
Step 1: IF (rear+1)%maxsize = front
Write " Overflow "
Goto step 4
[End OF IF]

Step 2: IF front = -1 and rear = -1


Set front = rear = 0
Else If rear = maxsize - 1 and front ! = 0
Set rear = 0
ELSE
Set rear = (rear + 1) % maxsize
[END OF IF]

Step 3: Set queue[rear] = value

Step 4: EXIT
dequeue()
Step 1: IF front = -1
Write " Underflow "
Goto Step 4
[END of IF]

Step 2: Set Value = queue[front]

Step 3: IF front = rear


Set front = rear = -1
ELSE
IF front = maxsize -1
SET front = 0
ELSE
SET front = front + 1
[END of IF]
[END OF IF]

Step 4: EXIT

Code

#include <stdio.h>
#include <stdlib.h>
struct queue
{
int *items;
int maxsize;
int front;
int rear;
int size;
};
struct queue* newQueue(int size)
{
struct queue *pt = NULL;
pt = (struct queue*)malloc(sizeof(struct queue));

pt->items = (int*)malloc(size * sizeof(int));


pt->maxsize = size;
pt->front = 0;
pt->rear = -1;
pt->size = 0;

return pt;
}
int size(struct queue *pt) {
return pt->size;
}
int isEmpty(struct queue *pt) {
return !size(pt);
}
int front(struct queue *pt)
{
if (isEmpty(pt))
{
printf("Underflow\n");
exit(EXIT_FAILURE);
}

return pt->items[pt->front];
}
void enqueue(struct queue *pt, int x)
{
if (size(pt) == pt->maxsize)
{
printf("Overflow\n");
exit(EXIT_FAILURE);
}

printf("Inserting %d\t", x);

pt->rear = (pt->rear + 1) % pt->maxsize;


pt->items[pt->rear] = x;
pt->size++;

printf("front = %d, rear = %d\n", pt->front, pt->rear);


}
void dequeue(struct queue *pt)
{
if (isEmpty(pt))
{
printf("Underflow\n");
exit(EXIT_FAILURE);
}

printf("Removing %d\t", front(pt));


pt->front = (pt->front + 1) % pt->maxsize;
pt->size--;

printf("front = %d, rear = %d\n", pt->front, pt->rear);


}

int main()
{
struct queue *pt = newQueue(10);
char ch;

enqueue(pt, 1);
enqueue(pt, 2);
enqueue(pt, 3);
enqueue(pt, 4);
enqueue(pt, 5);
enqueue(pt, 6);
enqueue(pt, 7);
enqueue(pt, 8);
enqueue(pt, 9);
enqueue(pt, 10);
printf("size = %d\n", size(pt));

if (isEmpty(pt)) {
printf("The queue is empty");
}
else {
dequeue(pt);
enqueue(pt, 11);
dequeue(pt);
enqueue(pt, 12);
dequeue(pt);
enqueue(pt, 13);
dequeue(pt);
enqueue(pt, 14);
dequeue(pt);
enqueue(pt, 15);
dequeue(pt);
enqueue(pt, 16);
dequeue(pt);
enqueue(pt, 17);
dequeue(pt);
enqueue(pt, 18);
dequeue(pt);
enqueue(pt, 19);
dequeue(pt);
enqueue(pt, 20);
dequeue(pt);
enqueue(pt, 21);
dequeue(pt);
enqueue(pt, 22);
dequeue(pt);
enqueue(pt, 23);
dequeue(pt);
enqueue(pt, 24);
dequeue(pt);
enqueue(pt, 25);
dequeue(pt);
enqueue(pt, 26);
dequeue(pt);
enqueue(pt, 27);
dequeue(pt);
enqueue(pt, 28);
dequeue(pt);
enqueue(pt, 29);
dequeue(pt);
enqueue(pt, 30);
dequeue(pt);
enqueue(pt, 31);
dequeue(pt);
enqueue(pt, 32);
dequeue(pt);
enqueue(pt, 33);
dequeue(pt);
enqueue(pt, 34);
dequeue(pt);
enqueue(pt, 35);
dequeue(pt);
enqueue(pt, 36);
dequeue(pt);
enqueue(pt, 37);
dequeue(pt);
enqueue(pt, 38);
dequeue(pt);
enqueue(pt, 39);
dequeue(pt);
enqueue(pt, 40);
dequeue(pt);
enqueue(pt, 41);
dequeue(pt);
enqueue(pt, 42);
dequeue(pt);
enqueue(pt, 43);
dequeue(pt);
enqueue(pt, 44);
dequeue(pt);
enqueue(pt, 45);
dequeue(pt);
enqueue(pt, 46);
dequeue(pt);
enqueue(pt, 47);
dequeue(pt);
enqueue(pt, 48);
dequeue(pt);
enqueue(pt, 49);
dequeue(pt);
enqueue(pt, 50);
dequeue(pt);
enqueue(pt, 51);
dequeue(pt);
enqueue(pt, 52);
dequeue(pt);
enqueue(pt, 53);
dequeue(pt);
enqueue(pt, 54);
dequeue(pt);
enqueue(pt, 55);
dequeue(pt);
enqueue(pt, 56);
dequeue(pt);
enqueue(pt, 57);
dequeue(pt);
enqueue(pt, 58);
dequeue(pt);
enqueue(pt, 59);
dequeue(pt);
enqueue(pt, 60);
}
return 0;
}

Output
CSE2011
LAB V
APPLICATIONS OF QUEUE USING PRIORITY
QUEUE

Name: MEHALI SAMANTA


Registration Number: 20BEC1204

Algorithms

add()
Step 1: IF rear >= MAX - 1
Write " Queue Overflow no more elements can be inserted "
return

Step 2: IF front = -1 and rear = -1


front = front + 1
rear = rear + 1
que[rear] = data
return

Step 3: check(data)

rear = rear + 1

Step 4: EXIT
check()
Step 1: for i=0 to <=rear
If data < = que[i]
for j = rear + 1 to i; reverse(j--)
que[j] = que[j-1]

[End of loop j]

Step 2: que[i] = data

Return

[End of if]

Step 3:que[i]=data

[End of loop i]

Step 4: EXIT

poll()
Step 1: if front = -1 and rear = -1
Write “Queue is empty no elements to delete”
return
[End of if]

Step 2: Display “The element deleted is :”

Print que[front]

Step 3:for i < rear

que[i]=que[i+1]

[End of loop i]

que[i]=-99

rear = rear – 1

Step 4: if rear = -1
front = -1
[End of if]
return

Step 5: EXIT
display_pqueue()
Step 1: if front = -1 and rear = -1
Write “Queue is empty”
return
[End of if]

Step 2:for front <= rear; front = front+1

Print que[front]

[End of loop]

Step 4: front = 0

Step 5: EXIT

create()
Step 1: front = rear = -1

Step 2: EXIT

main()
Asks for the users choices and accordingly calls the required functions and
displays the required ouput.
Code

#include <stdio.h>
#include <stdlib.h>

#define MAX 10

void add(int);
void poll();
void create();
void check(int);
void display_pqueue();

int que[MAX];
int front, rear;

void main()
{
int n, ch;

printf("\n1 - Insert an element into queue");


printf("\n2 - Delete an element from queue");
printf("\n3 - Display queue elements");
printf("\n4 - Exit");
create();

while (1)
{
printf("\nEnter your choice : ");
scanf("%d", &ch);

switch (ch)
{
case 1:
printf("\nEnter value to be inserted : ");
scanf("%d",&n);
add(n);
break;
case 2:
printf("\nDeleting first priority element......");
poll();
printf("\n\nRemaing elements:");
display_pqueue();
break;
case 3:
display_pqueue();
break;
case 4:
exit(0);
default:
printf("\nChoice is incorrect, Enter a correct choice");
}
}
}

void create()
{
front = rear = -1;
}

void add(int data)


{
if (rear >= MAX - 1)
{
printf("\nQueue overflow no more elements can be
inserted");
return;
}
if ((front == -1) && (rear == -1))
{
front++;
rear++;
que[rear] = data;
return;
}
else
check(data);
rear++;
}

void check(int data)


{
int i,j;

for (i = 0; i <= rear; i++)


{
if (data <= que[i])
{
for (j = rear + 1; j > i; j--)
{
que[j] = que[j - 1];
}
que[i] = data;
return;
}
}
que[i] = data;
}

void poll()
{
int i=0;

if ((front==-1) && (rear==-1))


{
printf("\nQueue is empty no elements to delete");
return;
}
printf("\nThe element deleted is:%d",que[front]);

for (; i < rear; i++)


{
que[i] = que[i + 1];
}

que[i] = -99;
rear--;

if (rear == -1)
front = -1;
return;
}

void display_pqueue()
{
if ((front == -1) && (rear == -1))
{
printf("\nQueue is empty");
return;
}

for (; front <= rear; front++)


{
printf(" %d ", que[front]);
}

front = 0;
}
Output
CSE2011
LAB ASSIGNMENT

Name: MEHALI SAMANTA


Registration Number: 20BEC1204

LONGEST COMMON PREFIX USING BINARY SEARCH

Code
#include <stdio.h>

#include <string.h>

int main()

int n;

scanf("%d",&n);

char a[n][100];

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

scanf("%s",a[i]);

}
int t=100;

char min[100];

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

int m=strlen(a[i]);

if(m<t)

t=m;

if(t%2!=0)

t=t+1;

char prefix[100]="-1";

int front=0,back=t;

while(front<=back)

char temp[100];

int mid=(front+back)/2;

strncpy(temp,a[0],mid);

int i;

for(i=1;i<n;i++)

{
if(strncmp(temp,a[i],mid+1)!=0)

back=mid-1;

break;

if(i==n)

strcpy(prefix,temp); front=mid+1;

printf("%s",prefix);

return 0;

Output
CSE2011
LAB ASSIGNMENT

Name: MEHALI SAMANTA


Registration Number: 20BEC1204

Question
According to an XYZ share market blog the market sale
percentage of a Various Products X varies over time interval of
one hour inclusive of opening and closing time during one
particular day. The data is recorded from 9 AM to 4 PM is of
those products are represented as follows in the graph and
share sale percentage. Sort the share percentage according to
their product names in ascending order . Then perform search
for a given product name should list its price value. Implement
all the below given algorithms.

Sorting - (Insertion and Selection)

Searching – Binary Search


Algorithms
Declare an enumeration MAX_NAME_LENGTH of value 10

Declare a structure product to store the product details

struct product

name[MAX_NAME_LENGTH], per ,price

End

read()

For i = 0 to N (Total number of products)

Read the name, sale percentage and price of each product

Loop ends

End

selectionsort()

product *p_product,*p_outer,*p_inner,temp_product

For p_product = p_product_start to n

p_inner = p_product

for p_outer = p_inner + 1 to n

if p_inner->name, p_outer->name, then

p_inner = p_outer

End of if

Loop ends

temp_product = *p_product

*p_product = *p_inner

*p_inner = temp_product
Loop ends

End

insertionsort()

product *p_product,p_inner

For i=1 to N

p_inner=*(p_product_start+i)

j=i-1

While j>=0 AND (p_product_start+j)->name > p_inner.name

p_product_start[j+1]=p_product_start[j]

j=j-1

End of while

*(p_product_start+j+1)=p_inner

Loop ends

End

binarySearch()

if r >= l, then
mid = l + (r - l) / 2

if (p_product_start+mid)->name = x, then

Return (p_product_start+mid)

End of If

if (p_product_start+mid)->name = x, then

Return the function binarySearch with l, mid-1,p_product_start, x as parameters

End of if

Return the fuction binarySearch with mid+1,r,p_product_start, x as parameters

End of if
Return Null Value
End

display()
For i=0 to N
Display the name, sale percentage and price of each product in a tabular form
Loop ends
End

main()
Reads the value of N (Total number of Products)
Calls the function read() to read all the details of every product
Calls the function display() to display details of all products before sorting
Asks the user’s choice for the type of sorting to be performed, i
If i = 1, then
Calls the function insertionsort()
Else if i = 2, then
Calls the function selectiosort()
End of if
Calls the display() function to print the sorted details of all products
Reads the name of the product to be searched for
Calls the function binarySearch() to search for the product
If the value returned by the function binarySearch() is not Null, then
Print the product name alongwith its price
End of if
Else
Print ‘Product Not Found’
End of else
End
Code
#include <stdio.h>

#include <string.h>

enum { MAX_NAME_LENGTH = 10 };

struct product

char name[MAX_NAME_LENGTH];

float per;

int price;

};

void read(struct product data[], int N)

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

printf("Enter Product Name:");

scanf("%s",data[i].name);

printf("Enter Percentage:");

scanf("%f",&data[i].per);

printf("Enter Price:");

scanf("%d",&data[i].price);

}
}

void selectionsort(int n, struct product *p_product_start)

struct product *p_product,

*p_outer,

*p_inner,

temp_product;

for (p_product = p_product_start;

(p_product-p_product_start) < n; p_product++)

p_inner = p_product;

for (p_outer = p_inner + 1;

(p_outer-p_product_start) < n; p_outer++)

if (strcmp(p_inner->name, p_outer->name) > 0)

p_inner = p_outer;

temp_product = *p_product;

*p_product = *p_inner;

*p_inner = temp_product;

}
}

void insertionsort(int N, struct product *p_product_start)

struct product *p_product,p_inner;

for (int i=1;i<N;++i)

p_inner=*(p_product_start+i);

int j=i-1;

while(j>=0&&strcmp((p_product_start+j)->name,p_inner.name) > 0){

p_product_start[j+1]=p_product_start[j];

j=j-1;

*(p_product_start+j+1)=p_inner;

struct product* binarySearch(int l, int r,struct product *p_product_start, char


x[10])

if (r >= l) {

int mid = l + (r - l) / 2;
if (strcmp((p_product_start+mid)->name,x) == 0)

return (p_product_start+mid);

if (strcmp((p_product_start+mid)->name,x)>0)

return binarySearch(l, mid-1,p_product_start, x);

return binarySearch(mid+1,r,p_product_start, x);

return NULL;

void display(int N, struct product *c)

printf("Products (%d):\n", N);

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

printf("%-10s %6.2f %4d\n", c[i].name, c[i].per, c[i].price);

int main()

int N,i;

char ch[10];

printf("\nEnter Number of Products:\n");

scanf("%d",&N);

struct product data[N],*p;

read(data,N);
display(N, data);

printf("\nSelect a Sorting Method\n1.Insertion sort\n2.Selection sort\nEnter


your Choice:\n");

scanf("%d",&i);

if(i==1)

insertionsort(N,data);

else if(i==2)

selectionsort(N, data);

printf("\nSorted data\n");

display(N, data);

printf("\nEnter the product name to be searched : ");

scanf("%s",ch);

p=binarySearch(0,N,data,ch);

if(p!=NULL)

printf("\nThe price of product %s is : %d",ch,p->price);

else

printf("\nProduct Not Found");

return 0;

}
Output
CSE2011
LAB ASSIGNMENT

Linked List Submission

Name: MEHALI SAMANTA


Registration Number: 20BEC1204

Question 1
Implement the stack data structure using Linked lists.

Code

#include <stdio.h>

#include <stdlib.h>

void push();

void pop();

void display();

struct node

{
int val;

struct node *next;

};

struct node *head;

void main ()

int choice=0;

printf("\n**Stack operations using Linked list**\n");

while(choice != 4)

printf("\n\nChose one from the below options:\n");

printf("\n1.Push\n2.Pop\n3.Display\n4.Exit");

printf("\n Enter your choice \n");

scanf("%d",&choice);

switch(choice)

case 1:

push();

break;

case 2:

{
pop();

break;

case 3:

display();

break;

case 4:

printf("Exit");

break;

default:

printf("Please Enter valid choice ");

};

void push ()

int val;

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


if(ptr == NULL)

printf("Not able to push the element");

else

printf("Enter the value");

scanf("%d",&val);

if(head==NULL)

ptr->val = val;

ptr -> next = NULL;

head=ptr;

else

ptr->val = val;

ptr->next = head;

head=ptr;

printf("Item pushed");

}
}

void pop()

int item;

struct node *ptr;

if (head == NULL)

printf("Underflow");

else

item = head->val;

ptr = head;

head = head->next;

free(ptr);

printf("Item popped");

void display()

int i;

struct node *ptr;

ptr=head;
if(ptr == NULL)

printf("Stack is empty\n");

else

printf("Printing Stack elements \n");

while(ptr!=NULL)

printf("%d\n",ptr->val);

ptr = ptr->next;

}
Output
Question 2
Create two linked list , Let the first linked list be 1->3->5 and
second linked list be 2->4->6->8, Write a program to merge the two
lists and return a third list as 1->3->5 ->2->4->6->8.

Code
#include <stdio.h>

#include <stdlib.h>

struct node

int data;

struct node *next;

};

struct node* input(int *n){

struct node *p;struct node *a;

int d,t;

a=malloc(sizeof(struct node));

p=a;

for(int i=0;i<*n;i++)

scanf("%d",&d);

while(p!=NULL){
if(d<p->data){

while(p->next!=NULL){

t=p->data;

p->data=d;

d=t;

p=p->next;

else if(p->next==NULL){

p->data=d;

if(i<*n-1)p->next=malloc(sizeof(struct node));

break;

else

p=p->next;

p=a;

return a;

void display(struct node *head)

{
if(head == NULL)

printf("END\n");

else

printf("%d->", head -> data);

display(head->next);

void concatenate(struct node *a,struct node *b)

if( a != NULL && b!= NULL )

if (a->next == NULL)

a->next = b;

else

concatenate(a->next,b);

else

printf("Either a or b is NULL\n");
}

int main()

struct node *a, *b;

int n,i,d,t;

printf ("Number of elements in a:");

scanf("%d",&n);

a=input(&n);

printf ("Number of elements in b:");

scanf("%d",&n);

b=input(&n);

concatenate(a,b);

printf("\n\n**The Merged List**\n");

display(a);

return 0;

}
Output
CSE2011
LAB ASSIGNMENT

Name: MEHALI SAMANTA


Registration Number: 20BEC1204

Program 1
Implement Merge Sorting.

Algorithms

merge(x, first, mid, last )


/* x[first:mid] and x[mid+1:last] are ordered lists of data
elements to be merged into a single ordered list x[first:last] */
• first1 = first
• last1 = mid
• first2 = mid + 1
• last2 = last /* set the beginning and the ending indexes of the two
lists into the appropriate variables*/
• i = first; /* i is the index variable for the temporary output list
temp*/
/* begin pair wise comparisons of elements from the two lists*/
• while (first1 ≤ last1) and (first2 ≤ last2) do
• Case /* if case 1*/
: x [first1] < x[first2]: { temp[i]= x[first1]
first1 = first1 + 1
i=i+1
• /* if case 2*/
: x [first1] > x[first2]: { temp[i]= x[first2]
first2 = first2 + 1
i=i+1
• /*if case 3*/
: x [first1] = x[first2]: { temp[i]= x[first1]
temp[i + 1] = x[first2]
first1 = first1 + 1
first2 = first2 + 1
i=i+2
end case
end while
/* the first list gets exhausted*/
• while (first2 ≤last2) do
temp[i]= x[first2]
first2 = first2 + 1
i=i+1
• end
/* the second list gets exhausted*/
• while (first1 ≤ last1) do
temp[i]= x[first1]
first1 = first1 + 1
i=i+1
• end
/* copy list temp to list x*/
• for j = first to last do
• x[j] = temp[j]
• end
• END merge.

merge_sort(a, first, last )


/* a[first:last] is the unordered list of elements to be merge
sorted. The call to the procedure to sort the list a[1:n] would be
merge_sort(a, 1, n)*/
• if (first < last) then
Mid = (first+last) /2 /* divide the list into two sublists*/
merge_sort(a, first, mid) /* merge sort the sublist a[first,mid]*/
merge_sort(a, mid+1, last) /* merge sort the sublist a[mid+1,
last]*/
merge(a, first, mid, last) /* merge the two sublists a[first,mid]
and a[mid+1, last]*/
• End if
• END merge_sort.
main()

 Declare integer array a of size 20 and variable n to store the


number of elements
 Read the value of n /* input of array of elements to be sorted */
 For i = 1 to n
Read value of a[i]
 End of loop
 merge_sort(a,0,n-1) /* sorting the array */
/* printing the merged sorted array */
 for i=1 to n
print a[i]
 End of loop
 END

Code

#include<stdio.h>

void merge(int x[],int first,int mid,int last){

int first1=first;

int last1=mid;

int first2=mid+1;

int last2=last;

int i=first;

int temp[50];
while(first1<=last1&&first2<=last2){

if(x[first1]<x[first2]){

temp[i]=x[first1];

first1+=1;

i+=1;

else if(x[first1]>x[first2]){

temp[i]=x[first2];

first2+=1;

i+=1;

else if(x[first1]==x[first2]){

temp[i]=x[first1];

temp[i+1]=x[first2];

first1+=1;

first2+=1;

i+=2;

while(first2<=last2){

temp[i]=x[first2];

first2+=1;

i+=1;

}
while(first1<=last1){

temp[i]=x[first1];

first1+=1;

i+=1;

for(int j=first;j<=last;j++)

x[j]=temp[j];

void merge_sort(int a[],int first,int last)

if(first<last)

int mid=(first+last)/2;

merge_sort(a,first,mid);

merge_sort(a,mid+1,last);

merge(a,first,mid,last);

int main(){

int a[20],n;

printf("Enter the Total No. of Elements:");

scanf("%d",&n);

printf("Input the Elements:\n");

for(int i=0;i<n;++i){
scanf("%d",&a[i]);

merge_sort(a,0,n-1);

printf("Sorted Merged Array:\n");

for(int i=0;i<n;++i){

printf("%d\n",a[i]);

Output
Program 2
According to an XYZ share market blog the market sale
percentage of a Various Products X varies over time interval
of one hour inclusive of opening and closing time during one
particular day. The data is recorded from 9 AM to 4 PM is of
those products are represented as follows in the graph and
share sale percentage. Sort the share percentage according
to their product names in ascending order using Quick
sorting.

Algorithms

void swap(int* a, int* b)


 int t = *a
 *a = *b
 *b = t
partition(L, first, last,loc)
/* L[first:last] is the list to be partitioned. */
• left = first
• right = last+1
• pivot_elt = L[first] /* set the pivot element to the first element in
list L*/
• while (left < right) do
• repeat
• until L[left] ≤ pivot_elt
• left = left+1 /* pivot element moves left to right*/
• repeat
• until L[right] > pivot_elt
• right = right -1 /* pivot element moves right to left*/
• if (left < right)
then swap(L[left], L[right]) /*arrows face each other*/
• End
• Loc=right
• swap(L[first], L[right]) /* arrows have crossed each other –
exchange pivot element L[first] with L[right]*/
• END .

quickSort(L, first, last )


/* L[first:last] is the unordered list of elements to be quick
sorted. The call to the procedure to sort the list L[1:n] would be
quickSort(L, 1, n) */
• if (first < last) then
{ partition(L, first, last) /* partition the list into two sublists at
loc*/
quickSort(L, first, loc-1 ) /* quick sort the sublist L[first,loc-1]*/
quickSort(L, loc+1, last ) /* quick sort the sublist L[loc+1, last]*/
• End if
• END.

printArray(L,size,pro[])

 Declare integer loop variable i


 for i = 0 to size
print product name and share percentage
 Loop ends
 END.

main()

 Declare integer array to store the share percentages


 Declare character array to store product names
 Declare integer variable n to store the number of products
 Read the value of n from user
/* Taking the input of product names and share percebtages from
user */
 For i = 0 to n
Read product name
Read share percentage
 Loop ends
 Call the function quickSort(L, 0, n-2) /* for sorting the share
percentages */
 Call the function printArray(L, n,pro) /*printing the sorted array*/
 END.

Code
#include<stdio.h>

int loc;

void swap(int* a, int* b)

int t = *a;

*a = *b;

*b = t;

int partition (int* L, int first, int last)

int left = first;

int right = last + 1;

int pivot = L[first];

while(left<right){

while(L[left]<=pivot)

left+=1;

while(L[right]>=pivot)

right-=1;

if(left<right)

swap(&L[left],&L[right]);

loc= right;
swap(&L[first],&L[right]);

void quickSort(int* L, int first, int last)

if (first < last)

partition(L, first, last);

quickSort(L, first, loc - 1);

quickSort(L, loc + 1, last);

void printArray(int L[], int size,char pro[][10])

int i;

for (i = 0; i < size; i++)

printf("%-10s %d\n",pro[i],L[i]);

int main()
{

int L[20];

char pro[20][10];

int n;

printf("Enter the no. of shares:");

scanf("%d",&n);

printf("Enter the product names and share percentages:-\n");

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

printf("Enter product name:");

scanf("%s",&pro[i]);

printf("Enter share percentage:");

scanf("%d",&L[i]);

quickSort(L, 0, n-2);

printf("Sorted array: \n");

printArray(L, n,pro);

return 0;

Output
CSE2011
LAB ASSIGNMENT

Name: MEHALI SAMANTA


Registration Number: 20BEC1204

Program 3
Given two lists sorted in increasing order, create and return a new list
representing the intersection of the two lists.The new list should be
made with its own memory. The original lists should not be changed.

Code
#include <stdio.h>

#include <stdlib.h>

struct node {

int data;

struct node *next;

};

struct node *even = NULL;

struct node *odd = NULL;


struct node *list = NULL;

void insert(int data) {

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

struct node *current;

link->data = data;

link->next = NULL;

if(data%2 == 0) {

if(even == NULL) {

even = link;

return;

} else {

current = even;

while(current->next != NULL)

current = current->next;

current->next = link;

} else {
if(odd == NULL) {

odd = link;

return;

} else {

current = odd;

while(current->next!=NULL)

current = current->next;

current->next = link;

void display(struct node *head) {

struct node *ptr = head;

while(ptr != NULL) {

printf(" %d =>",ptr->data);

ptr = ptr->next;

printf(" [null]\n");
}

void combine() {

struct node *link;

list = even;

link = list;

while(link->next!= NULL) {

link = link->next;

link->next = odd;

int main() {

int i;

for(i = 1; i <= 10; i++){

insert(i);

printf("List 1 : ");

display(even);
printf("List 2 : ");

display(odd);

combine();

printf("Combined List :\n");

display(list);

return 0;

Output
Program 4
Given a linked list of characters,write a function that returns true if the
given list is a palindrome,else false.

Code

#include <stdbool.h>

#include <stdio.h>

#include <stdlib.h>

struct Node {

char data;

struct Node* next;

};

void reverse(struct Node**);

bool compareLists(struct Node*, struct Node*);

bool isPalindrome(struct Node* head)

struct Node *slow_ptr = head, *fast_ptr = head;

struct Node *second_half, *prev_of_slow_ptr = head;

struct Node* midnode = NULL;

bool res = true;


if (head != NULL && head->next != NULL) {

while (fast_ptr != NULL && fast_ptr->next != NULL) {

fast_ptr = fast_ptr->next->next;

prev_of_slow_ptr = slow_ptr;

slow_ptr = slow_ptr->next;

if (fast_ptr != NULL) {

midnode = slow_ptr;

slow_ptr = slow_ptr->next;

second_half = slow_ptr;

prev_of_slow_ptr->next = NULL;

reverse(&second_half);

res = compareLists(head, second_half);

reverse(&second_half);

if (midnode != NULL) {

prev_of_slow_ptr->next = midnode;

midnode->next = second_half;

else

prev_of_slow_ptr->next = second_half;

return res;

}
void reverse(struct Node** head_ref)

struct Node* prev = NULL;

struct Node* current = *head_ref;

struct Node* next;

while (current != NULL) {

next = current->next;

current->next = prev;

prev = current;

current = next;

*head_ref = prev;

bool compareLists(struct Node* head1, struct Node* head2)

struct Node* temp1 = head1;

struct Node* temp2 = head2;

while (temp1 && temp2) {

if (temp1->data == temp2->data) {

temp1 = temp1->next;

temp2 = temp2->next;

else
return 0;

if (temp1 == NULL && temp2 == NULL)

return 1;

return 0;

void push(struct Node** head_ref, char new_data)

struct Node* new_node = (struct Node*)malloc(sizeof(struct Node));

new_node->data = new_data;

new_node->next = (*head_ref);

(*head_ref) = new_node;

void printList(struct Node* ptr)

while (ptr != NULL) {

printf("%c->", ptr->data);

ptr = ptr->next;

printf("NULL\n");

int main()
{

struct Node* head = NULL;

char str[20];

gets(str);

int i;

for (i = 0; str[i] != '\0'; i++) {

push(&head, str[i]);

printList(head);

isPalindrome(head) ? printf("True\n\n") : printf("False\n\n");

return 0;

Output
CSE2011
LAB ASSIGNMENT

Name: MEHALI SAMANTA


Registration Number: 20BEC1204

Program
Construct a binary search tree T for the following set S of
elements in the order given:S = { INDIGO, GREEN, CYAN,
YELLOW, RED, ORANGE, VIOLET}. Find the position of the
ORANGE from the tree. Display the order of the tree in
increasing order.

Algorithms

Decalre struct Node

char key[100]

struct Node *left

struct Node *right

}
Decalre struct Node* newNode with argument as char *ele

 set struct Node* temp= (struct Node*)malloc(sizeof(struct


Node*))
 set temp->left=NULL
 set *temp->key=*ele
 set temp->right=NULL
 return temp

 decalre struct Node* Search with arguments as struct Node* root


and char ele[]
 if( (root==NULL) || strcmp(ele,root->key)==0)
return root
 End if
 if(strcmp(ele,root->key)<0)
return Search(root->left,ele)
 End if
 else if(strcmp(ele,root->key)>0)
return Search(root->right,ele)
 return NULL
 END.

declare function inorderTravelof void type with arguments as struct


Node* root

if(root!=NULL)

 inorderTravel(root->left)
 printf("%s\n",root->key)
 inorderTravel(root->right)
End if

declare struct Node* Insertion(struct Node* root, char ele[])

if(root==NULL)

 return newNode(ele)

End if

if(strcmp(ele,root->key)<0)

 root->left=Insertion(root->left,ele)

End if

else if(strcmp(ele,root->key)>0)

 root->right=Insertion(root->right,ele)

return root

END.

main()
 declare choice of int type
 declare ele of char type with size 100
 declare struct Node* root=NULL
 do

display ("* MENU *\n1.Insert Element\n2.View Inorder


Sequence\n3.Search Element\n4.Exit\nEnter Choice:")
get (choice)
begin switch(choice)
case 1:
display("Enter word:")
get(ele)
set root=Insertion(root,ele)
break
case 2:
display("Inoder Traversal is:\n")
inorderTravel(root)
break
case 3:
display("Enter word:")
get(ele)
set struct Node* word=Search(root,ele)
display("Element Found at address:%p\n",word)
break
case 4:
Exit
break
default:
display("\nEnter choice as 1/2/3/4")
break
 while (choice is not equal to 4)
 End of do-while
 END.
Code

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct node{
char *value;
struct node *left;
struct node *right;
};
typedef int (*Compare)(const char *, const char *);
void display(struct node *root);
void insert(char* key, struct node** leaf, Compare cmp);
void search(char* key, struct node* leaf, Compare cmp);
int Cmpstr(const char *a, const char *b);
int main()
{
struct node *root = NULL;
char *value;
int ch;
char val[10];
int c=1;
do
{
printf("MENU\n");
printf("1.Insert\n2.Inorder Traversal\n3.Search\n4.Exit\n");
printf("Enter choice:\n");
scanf("%d",&ch);
switch(ch)
{
case 1:printf("Enter string:\n");
scanf("%s",val);
insert(val, &root, (Compare)Cmpstr);
break;
case 2:display(root);
break;
case 3:printf("Enter element to search:\n");
scanf("%s",val);
search(val, root, (Compare)Cmpstr);
break;
case 4:exit(0);
default:printf("Invalid choice.\n");
}
printf("Enter 1 to continue or 0 to exit:\n");
scanf("%d",&c);
}while(c==1);
return 0;
}
void insert(char* key, struct node** leaf, Compare cmp)
{
int res;
if( *leaf == NULL )
{
*leaf = (struct node*)malloc(sizeof(struct node));
(*leaf)->value =(char*) malloc(strlen(key)+1);
strcpy ((*leaf)->value, key);
(*leaf)->left = NULL;
(*leaf)->right = NULL;
printf( "\nnew node for %s\n",key);
}
else
{
res = cmp (key, (*leaf)->value);
if( res < 0)
insert( key, &(*leaf)->left, cmp);
else if( res > 0)
insert( key, &(*leaf)->right, cmp);
else
printf ("Key '%s' already in tree\n", key);
}
}
int Cmpstr(const char *a, const char *b)
{
return(strcmp(a,b));
}
void search(char* key, struct node* leaf, Compare cmp)
{
int res;
if( leaf!=NULL)
{
res = cmp(key, (leaf)->value);
if( res < 0)
search( key, leaf->left, cmp);
else if( res > 0)
search( key, leaf->right, cmp);
else
printf("\n%s found at %d\n", key,&key);
}
else
{
printf("\nNot in tree\n");
}

}
void display(struct node *root)
{
if( root != NULL )
{
display(root->left);
printf("%s\n", root->value);
display(root->right);
}
}
Output

You might also like