CSE2011 Combined
CSE2011 Combined
Introduction to C
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
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
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
Algorithms
enqueue()
Step 1: IF (rear+1)%maxsize = front
Write " Overflow "
Goto step 4
[End OF IF]
Step 4: EXIT
dequeue()
Step 1: IF front = -1
Write " Underflow "
Goto Step 4
[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));
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);
}
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
Algorithms
add()
Step 1: IF rear >= MAX - 1
Write " Queue Overflow no more elements can be inserted "
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]
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]
Print que[front]
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]
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;
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 poll()
{
int i=0;
que[i] = -99;
rear--;
if (rear == -1)
front = -1;
return;
}
void display_pqueue()
{
if ((front == -1) && (rear == -1))
{
printf("\nQueue is empty");
return;
}
front = 0;
}
Output
CSE2011
LAB ASSIGNMENT
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
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.
struct product
End
read()
Loop ends
End
selectionsort()
product *p_product,*p_outer,*p_inner,temp_product
p_inner = p_product
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
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
End of if
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;
};
for(int i=0;i<N;i++)
scanf("%s",data[i].name);
printf("Enter Percentage:");
scanf("%f",&data[i].per);
printf("Enter Price:");
scanf("%d",&data[i].price);
}
}
*p_outer,
*p_inner,
temp_product;
p_inner = p_product;
p_inner = p_outer;
temp_product = *p_product;
*p_product = *p_inner;
*p_inner = temp_product;
}
}
p_inner=*(p_product_start+i);
int j=i-1;
p_product_start[j+1]=p_product_start[j];
j=j-1;
*(p_product_start+j+1)=p_inner;
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 NULL;
int main()
int N,i;
char ch[10];
scanf("%d",&N);
read(data,N);
display(N, data);
scanf("%d",&i);
if(i==1)
insertionsort(N,data);
else if(i==2)
selectionsort(N, data);
printf("\nSorted data\n");
display(N, data);
scanf("%s",ch);
p=binarySearch(0,N,data,ch);
if(p!=NULL)
else
return 0;
}
Output
CSE2011
LAB ASSIGNMENT
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;
};
void main ()
int choice=0;
while(choice != 4)
printf("\n1.Push\n2.Pop\n3.Display\n4.Exit");
scanf("%d",&choice);
switch(choice)
case 1:
push();
break;
case 2:
{
pop();
break;
case 3:
display();
break;
case 4:
printf("Exit");
break;
default:
};
void push ()
int val;
else
scanf("%d",&val);
if(head==NULL)
ptr->val = val;
head=ptr;
else
ptr->val = val;
ptr->next = head;
head=ptr;
printf("Item pushed");
}
}
void pop()
int item;
if (head == NULL)
printf("Underflow");
else
item = head->val;
ptr = head;
head = head->next;
free(ptr);
printf("Item popped");
void display()
int i;
ptr=head;
if(ptr == NULL)
printf("Stack is empty\n");
else
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;
};
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;
{
if(head == NULL)
printf("END\n");
else
display(head->next);
if (a->next == NULL)
a->next = b;
else
concatenate(a->next,b);
else
printf("Either a or b is NULL\n");
}
int main()
int n,i,d,t;
scanf("%d",&n);
a=input(&n);
scanf("%d",&n);
b=input(&n);
concatenate(a,b);
display(a);
return 0;
}
Output
CSE2011
LAB ASSIGNMENT
Program 1
Implement Merge Sorting.
Algorithms
Code
#include<stdio.h>
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];
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;
scanf("%d",&n);
for(int i=0;i<n;++i){
scanf("%d",&a[i]);
merge_sort(a,0,n-1);
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
printArray(L,size,pro[])
main()
Code
#include<stdio.h>
int loc;
int t = *a;
*a = *b;
*b = t;
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]);
int i;
printf("%-10s %d\n",pro[i],L[i]);
int main()
{
int L[20];
char pro[20][10];
int n;
scanf("%d",&n);
for(int i=0;i<n;++i)
scanf("%s",&pro[i]);
scanf("%d",&L[i]);
quickSort(L, 0, n-2);
printArray(L, n,pro);
return 0;
Output
CSE2011
LAB ASSIGNMENT
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;
};
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;
while(ptr != NULL) {
printf(" %d =>",ptr->data);
ptr = ptr->next;
printf(" [null]\n");
}
void combine() {
list = even;
link = list;
while(link->next!= NULL) {
link = link->next;
link->next = odd;
int main() {
int i;
insert(i);
printf("List 1 : ");
display(even);
printf("List 2 : ");
display(odd);
combine();
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;
};
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);
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)
next = current->next;
current->next = prev;
prev = current;
current = next;
*head_ref = prev;
if (temp1->data == temp2->data) {
temp1 = temp1->next;
temp2 = temp2->next;
else
return 0;
return 1;
return 0;
new_node->data = new_data;
new_node->next = (*head_ref);
(*head_ref) = new_node;
printf("%c->", ptr->data);
ptr = ptr->next;
printf("NULL\n");
int main()
{
char str[20];
gets(str);
int i;
push(&head, str[i]);
printList(head);
return 0;
Output
CSE2011
LAB ASSIGNMENT
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
char key[100]
}
Decalre struct Node* newNode with argument as char *ele
if(root!=NULL)
inorderTravel(root->left)
printf("%s\n",root->key)
inorderTravel(root->right)
End if
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
#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