Académique Documents
Professionnel Documents
Culture Documents
AIM:
ALGORITHM:
PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
{
char name, fathername, city;
int age;
clrscr();
printf(“Enter the name:”);
scanf(“%s”,name);
printf(“\n Enter the father’s name:”);
scanf(“%s”,fathername);
printf(\n Enter the city”);
scanf(“%s”,city);
printf(“\n Enter the age:”);
scanf(“%d”,&age);
printf(“\n Name:%s”,Name);
printf(“\n Father’s Name:%s”,fathername);
printf(“\n City:%s”,city);
printf(“\n Age:%d”,age);
getch();
}
RESULT:
Thus the C-program to print the bio-data had been executed and the output was verified.
FLOWCHART:
START
STOP
OUTPUT:
AIM:
ALGORITHM:
PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
{
int a[20],i,n;
float avg,s=0;
printf(“Enter the number of element to be added”);
scanf(“%d”,&n);
printf(“\n Enter the numbers”);
for(i=0;i<n;i++)
{
Scanf(“%d”,&a[i]);
}
for(i=0;i<n;i++)
{
s=s+a[i];
}
avg=s/n;
printf(“The average of %d numbers=%f”,n,avg);
getch();
}
RESULT:
Thus the C-program to find the average of n numbers had been written and it is executed.
FLOWCHART:
START
Read n
For(i=0;i<n;i++)
Read a[i]
For(i=0;i<n;i++)
s=s+a[i]
Avg=s/n
Print avg
stop
OUTPUT:
AIM:
ALGORITHM:
Step:1 Start.
Step:2 Read ‘n’ number element of array.
Step:3 Save the numbers in ascending order using temp’t’.
Step:4 Print the first element (i.e) smallest end largest elements.
Step:5 Stop.
PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,j,a[15],t;
clrscr();
printf(“Enter the numbers to be sort:”);
scanf(“%d”,&n);
for(i=1;i<=n;i++)
{
scanf(“%d”,&a[i]);
}
for(i=1;i<=n;i++)
{
for(j=i+1;j<=n;j++)
{
if(a[i]>a[j])
{
t=a[i];
a[i]=a[j];
a[j]=t;
}
}
}
printf(“The minimum value is %d”,a[1]);
printf(“The maximum value is %d”,a[n]);
getch();
}
RESULT:
Thus the C-program to print the largest and smallest of ‘N’ numbers is executed and the output is
verified.
FLOW CHART:
start
Read n
For(i=1;i<=n;i++)
For(j=i+1;j<=n;j++)
Ifa[i]>a[j
]
I=a[i]
A[i]=a[j]
A[j]=n
Print minimum
value a[i]
stop
OUTPUT:
AIM:
To write a ‘c’ program to find the total value of each item by each girl.
ALGORITHM:
PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
{
int b[10][10],m, a, s=0,c=0;
clrscr();
printf(“ Enter m, n”);
scanf(“%d%d”,&m,&n);
for(i=0;i<m;i++)
{
For(j=0;j<n;j++)
{
Scanf(“%d”,&b[i][j]);
a=0;
a=a+b[i][j];
printf(“Total value of each item by %d girl=%d”,i++,c);
}
For(i=0;i<n;i++)
{
For(j=0;j<m;j++)
{
c=0;
c=a+b[i][j];
printf(“ Total value of sales by each girl=%d”,c);
}
s=s+c;
}
printf(“Grand total of each item sold by each girl=%d”,s);
getch();
}
RESULT:
START
Read m,n
for(i=0;i<m;i++)
for(i=0;i<n;i++)
a=a+b[i][j]
Print a
for(j=0;j<n;j++)
for(i=0;i<n;i++)
c=c+b[i][[j]
Print c
A
A
s=s+c
Print s
STOP
OUTPUT:
Enter m,n: 2 2
Enter the value of each item by 1 girl=8
Total value of each item by 2 girl=8
Total value of sales by 1 girl=5
Total value of sales by 2 girl=11
Grand total of each item sold by all girl=16
STUDENT DETAILS
AIM:
ALGORITHM:
PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
{
int a[5][5], i, j;
float avg, b, s, t=0;
clrscr();
for(i=1;i<4;i++)
{
printf(“\n Enter the mark for %d student”,i);
for(j=1;j<6;j++)
{
scanf(“%d”,a[i][j]);
s=s+a[i][j];
}
avg=s/5;
Ss0;
printf(“ The no %d student avg is %f”,I,avg);
t=t+avg;
}
b=t/3;
printf(“The overall avg is %f”,b);
getch();
}
RESULT:
Thus the ‘C’ program to print the student details was written, executed and output was verified
FLOWCHART:
START
S=0;t=0
For(j=0;j<4;j++)
For(j=1;j<6;j++)
s=s+a[i][j]
Avg=s/5
A
A
Print avg
t=t+avg
b=avg/5
Write b
STOP
OUTPUT:
AIM:
To find the factorial of given numbers using recursive function in ‘c’ program.
ALGORITHM:
PROGRAM:
#include<stdio.h>
#include<conio.h>
int fact (int n)
void main()
{
int n;
clrscr();
printf(“ enter the no:”);
scanf(“%d”,&n);
printf(“the factorial of %d is %d” ,n ,fact(n));
getch();
}
int fact (int n)
{
int f=1;
if(n<0);
{
printf(“cannot find factorial”);
}
else if(n>1)
{
f=n*fact n-1;
return(f);
}
else
return (1);
}
RESULT:
Thus the ‘c’ program to find the factorial of given number using recursion function is executed
and output was verified.
FLOW CHART:
Main program
start
Read
n
fact
Print
fact
stop
Sub program
Int fact(int n)
yes
If
n
<
0
no
If
(n>
Print
1)
Cannot find factorial
F=n*fact(n-1)
Return f
Return 1
OUTPUT:
AIM:
ALGORITHM:
PROGRAM:
#include<stdio.h>
include<conio.h>
void main()
{
int n;
int fib(int n);
printf("enter n/n");
scanf("%d",&n);
fib(n);
}
int fib(int n);
{
int a=0;b=1;f,i,x;
printf("\n%d\t%d",a,b);
for(i=0;i<=n:i++)
{
x=a;
a=b;
b=x+a;
f=printf("\t%d",b);
}
return f;
}
RESULT:
x=a
a=b
b=a+b
next i
stop
OUTPUT:
AIM:
To write a C-program for solving tower of Hanoi.
ALGORTHIM:
PROGRAM.
#include<stdio.h>
#include<conio.h>
void hanoi(int n,char initial,char final,char temp)
{
if(n==1)
{
printf("move disc one from middle %c\n",initial,final);
return;
hanoi(n-1;initial, final,temp);
printf("move disc %d from %c \n"n,initial,final);
hanoi(n-1,temp,final,initial);
printf("move disc %d from %c \n",n,temp, final);
}
void main()
{
int n;
clrscr();
printf("enter the no of disc to move");
scanf("%d",&n);
hanoi(n,'A','B','C');
getch();
}
RESULT:
Main program:
START
Read n
Tower(n,’A’,’B’,’C,)
STOP
Sub-program:
Tower(int,char,char,char)
if(n==
1)
Tower(n-1,from reg
auxillary to reg)
Tower(n-1,aux reg to
reg from reg)
return
OUTPUT:
AIM:
ALGORITHM:
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<string.h>
static int top=0
static char stack[100]
void main()
{
void push(char);
char(pop);
char stack[100],char str[100],l;
int top i,j,l,c;
clrscr();
printf("\n Enter the string");
scanf("%s",str);
l=strlenght(str);
for(i=0;i<l;i++)
push(str[i]);
for(i=0;i<l;i++)
{
k=loop;
if(str[i]!=k)
{
printf("The string is not palindrome");
end;
}
else
printf(“The string is Palindrome”);
}
getch();
}
void push(char c)
{
stack[top]=c;
top=top++;
}
char pop()
{
top=top-1;
return(stack top)
}
RESULT:
Thus a C-program is written to check whether the given string is palindrome or not.
FLOWCHART:
start
Read the
string
L=strlen[i]
For(i=1;i<=l;i+
+)
For(i=1;i<=l;i+
+)
no
If
I=k
Print not
yes
palindrome
Print palindrome
stop
OUTPUT:
Madam
AIM:
ALGORITHM:
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
#include<stdlib.h>
#include<process.h>
#include<string.h>
char pre[40],post[40];
int top=0,st[20];
void postfix();
void push(int);
char pop();
void main()
{
clrscr();
printf(“Enter the expression”);
scanf(“%s”,&pre);
postfix();
getch();
}
void postfix()
{
int i,j=0;
fro(i=0;pre[i]!=’\0’;i++)
{
switch(inf[i])
{
case ‘+’:
while(st[top]>=1)
post[j++]=pop();
push(1);
break;
case ‘-’:
while(st[top]>=1)
post[j++]=pop();
push(2);
break;
case ‘*’:
while(st[top]>=3)
post[j++]=pop();
push(3);
break;
case ‘/’:
while(st[top]>=3)
post[j++]=pop();
push(4);
break;
case ‘^’:
while(st[top]>=1)
post[j++]=pop();
push(5);
break;
case ‘(’:
push(0);
break;
case ‘)’:
while(st[top]!=0)
post[j++]=pop();
top--;
break;
default:
post[j++]=pre[i];
}
}
while(top>0)
post[j++]=pop();
printf(“\nThe postfix expression is”,post);
}
void push(int ele)
{
top++;
st[top]=ele;
}
char pop()
{
int el;
char e;
el=st[top] ;
top--;
switch(el)
{
case 1:
e= ‘+’;
break
case 2:
e= ‘-’;
break;
case 3:
e= ‘*’;
break;
case 4:
e= ‘/’;
break;
case 5:
e= ‘^’;
break;
}
return(e);
}
RESULT:
Thus a C program to convert the prefix expression to postfix expression was written, executed and
the output was verfied.
FLOWCHART:
START
Main
program
Read input
Pre to post
STOP
Sub-Program
Pre to post
while yes
input()!
=10
no
if
operat
ed
Print the expression
op2=pop()
op1=pop()
print
postfix
exp()
i++
OUTPUT:
AIM:
ALGORITHM:
Step 1: Start.
Step 2: Define structure & size of stack.
Step 3: Initialize st.top = -1.
Step 4: Read choice for 1.push 2.pop 3.display 4.Exit.
Step 5: If choice is 1,then perform push operation.
If choice is 2,then perform pop operation.
If choice is 3,then perform display operation.
If choice is 4,then perform Exit.
Step 6: Read the choice Y_continue or N_not continue.
Step 7: If Y_continue else exit.
Step 8: STOP.
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define size 5
struck stack
{ int s[size]
int top;
} st;
int st full()
{
If (st.top>=size-1)
return 1;
else
return 0;
}
void push(int item)
{
st.top++
st.s[st.top]=item;
}
int stempty()
{
if(st.top==-1)
return 1;
else
return 0;
}
int pop()
{
int item:
item = st.s[st.top];
st.top--;
return(item);
}
void display()
{
int i;
If (stempty())
printf(" \n stack is empty");
else
{
for(i=st.top;i>=0;i++)
printf(" \n %d",st.s[i]);
}
}
void main(void)
{
int item,choice;
char ans;
st.top =-1;
clrscr();
printf(" \n\t\t Implementation of stack");
do
{
printf("\n Main menu");
printf("\n 1.Push \n 2.Pop \n 3.Display \n 4.Exit");
printf("Enter your choice");
scanf("%d",& choice);
switch (choice)
{
case 1:
{
printf("\n Enter the item to be pushed");
scanf("%d",&item):
if(st.full())
printf("\n stack is full");
else
push(item);
}
break;
case 2:
{
If (stempty())
printf("\n Empty stack!underflow!!");
else
{
item = pop();
printf("\n the popped element is %d",item);
}
break;
case 3:
display();
break;
case 4:
exit();
}
printf("\n Do you want to continue");
ans=getchar();
}while (ans=='y' || ans=='Y');
getch();
}
RESULT:
Thus the C-program to implement the element of stack using arrays was done and the
output was verified.
FLOWCHART:
Main
program:
start
top=null
print
1.push
2.pop
3.display
read choice
switch
(choic
e)
yes
if
choice<=
y
no
stop
Sub-program
push()
read item
temp=malloc(size of stack)
temp->data=item
temp->next=top
return
OUTPUT:
Implementation of stack
Main menu
1.Push
2.Pop
3.Display
4.Exit
AIM:
ALGORITHM:
Step 1: Start.
Step 2: Define structure & size of queue.
Step 3: Initialize Q.rear=0,Q.front=-1.
Step 4: Read choice for 1.insert 2.delete 3.display 4.Exit.
Step 5: If choice is 1,then perform insert operation.
If choice is 2,then perform delete operation.
If choice is 3,then perform display operation.
If choice is 4,then perform Exit.
Step 6: Read the choice Y_continue or N_not continue.
Step 7: If Y_continue else exit.
Step 8: Stop.
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define size 5
struck queue
{
int Q[size]
int front,rear;
} Q;
int Q full()
{
If (Q.rear>=size-1)
return 1;
else
return 0;
}
void insert(int item)
{
st.rear++;
Q.que[Q.rear]=item;
}
int Qetempty()
{
If(Q.rear==-1)
return 1;
else
return 0;
}
int delete()
{
int item:
item = Q.que[Q.rear];
st.front++;
return(item);
}
void display()
{
int i;
If (stempty())
printf(" \n queue is empty");
else
{
for(i=Q.front;i<=Q.rear;i++)
printf(" \n %d",Q.que[i]);
}
}
void main(void)
{
int item,choice;
char ans;
Q.front==0;
Q.rear==0;
clrscr();
printf(" \n\t\t Implementation of queue");
do
{
printf("\n Main menu");
printf("\n 1.Insert \n 2.Delete \n 3.Display \n 4.Exit");
printf("Enter your choice");
scanf("%d",& choice);
switch (choice)
{
case 1:
{
printf("\n Enter the item to be inserted");
scanf("%d",&item):
if(Q.full())
printf("\n queue is full");
else
insert(item);
}
break;
case 2:
{
If (Qetempty())
printf("\n Empty queue!underflow!!");
else
{
item = delete();
printf("\n the deleted element is %d",item);
}
}
break;
case 3:
display();
break;
case 4:
exit();
}
printf("\n Do you want to continue");
ans=getchar();
}while (ans=='y' || ans=='Y');
getch();
}
RESULT:
Thus the C-program to implement the element of stack using arrays was done and the
output was verified.
FLOWCHART:
Main Program:
Sub Program:
OUTPUT:
Implementation of queue
Main menu
1.Insert
2.Delete
3.Display
4.Exit
AIM:
To write a ‘C’ program to perform operations like create, insert,delete, seach,and display in a
singly linked list.
ALGORITHM:
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define TRUE 1
#define FALSE 0
typedef struct SLL
{
int data;
struct SLL*next;
}node;
Node*create();
void main()
{
int choice, val;
char ans;
node*head;
void display(node*);
node*search(node*,int);
void insert(node*)
void delete(node**);
head=NULL;
do
{
clrscr();
printf(“ program to perform various operations on linked list”);
printf(“\n 1. create \n 2. display\n 3. search\n 4. insert\n 5. delete \n 6.quit”);
printf(“ \n Enter your choice”);
scanf(“%d”,& choice);
switch(choice)
{
case1:
head=create();
break;
case 2:
display (head);
break;
case 3:
printf(“ enter the element you want to search”);
scanf(“%d”,val);
scanf(“head,val);
break;
case 4:
insert(head);
break;
case 5:
dele (head);
break;
case 6:
exit(0);
default;
clrscr();
printf(“ Invalid choice,try again”);
getch();
}
}
while(choice!=t);
}
node*create()
{
node*temp,*new,*head;
int val, flag;
char ans=’Y’;
node*get_node();
temp=NULL;
flag=TRUE;
do
{
{
printf(“\n Enter the element:”);
scanf(“%d”,&val);
new=get_node();
If(new ==NULL)
printf(“ \n memory is not allocated”);
new->data=val;
if(flag)
{
head= new;
temp=head;
flag=FALSE;
}
else
{
temp->next=new;
temp=new;
}
printf(“\n do you want to enter more element?(y/n)”);
ans=getche();
}
while (ans==’Y’);
printf(“The singly linked list is created”);
getch();
clrscr();
return head;
}
node*get_node()
{
node*temp;
temp=(node*)malloc(size of node));
temp->next=NULL;
return temp;
}
void display(node*head)
{
node*temp;
temp=head;
if(temp==NULL)
{
printf(“\n The list is empty”);
getch();
clrscr();
return;
}
while(temp!=NULL)
}
printf(“%d->”,temp->data);
temp=temp->next;
}
printf(“NULL”);
}
node*searchnode(node*head,int key)
{
node*temp;
int found;
temp=head;
if(temp==NULL)
{
printf(“The linked list is empty”);
getch();
clrscr();
return NULL;
}
found=FALSE;
while(temp!=NULL&&!found){
if(found)
{
printf(“The element is present in the list\n”);
getch();
return temp;
}
else
{
printf(“The element is not present in the list\n”);
getch();
return NULL;
}
}
RESULT:
Thus the c-program for singly linked list had been written and the operations such as creation
,deletion, insertion, display was performed.
FLOWCHART:
Main Program
start
Start=null
Read
choice
Switch
(choic
e)
yes
While
Ch<=
0
no
stop
Sub-Program:
view()
list=head
if
list=NUL
L
print roll no
name
for(list!=NULL;list=list->link)
print roll no
name
next list
return
OUTPUT:
AIM:
To write and execute a program to implement the operation in double linked list.
ALGORITHM:
Step:1 Start.
Step:2 Read.
Step:3 If c==1,create new node.
Step:4 If c==2,insert new node at specific location.
Step:5 If c==3,delete a particular node.
Step:6 If c==4,display the list.
Step:7 If c==5,exit.
Step:8 Stop.
PROGRAM:
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
struct node
{
int data;
struct node*next,*prev;
}
*New,*new1,*temp,*start,*dummy;
void add(void)
struct node*get_node();
void display(void);
void delete(void);
int find(int);
int first=1;
void main()
{
char ans;
int choice,num,found=0;
struct=NULL;
do{
clrscr();
printf("Program for double link list");
printf("\n1.insert\n2.delete\n3.display\n4.searching\n5.exit");
printf("\n enter your choice:");
scanf("%d",&choice);
switch(choice);
{
case 1:add()
break;
case 2:delete()
break;
case 3:display()
break;
case 4:printf("enter no to be search:");
scanf("%d",&num);
temp=start;
while((temp!=NULL)&&(found==0))
found=find(num);
if(found)
printf("\n no is present");
break;
case 5:exit(0)
}
printf("\n do u want to continue?");
ans=getch();
}
while(ans=='y'||ans=='Y')
getch();
}
void add(void)
{
clrscr();
New=get_node();
printf("\n\n\tEnter the element:");
scanf("%d",&New->data);
if(first==1)
{
start=New;
first=0;
}
else
{
dummy=start;
while(dummy->next!=NULL)
dummy=dummy->next;
dummy->next=New;
New->pre=dummy;
}
}
struct node*get_node()
{
new1=(node*)malloc(sizeof(struct node));
new1->next=NULL;
new1->prev=NULL;
return(new1);
}
void display(void)
{
clrscr();
temp=stat;
if(temp==NULL);
printf("\n double linked list is empty");
else
{
while(temp!=NULL)
{
printf("%d",temp->data);
temp=temp->next;
}
printf("NULL");
}
getch();
}
int find(int num)
{
if(temp->data==num)
return(1)
else
temp=temp>next;
return 0;
}
void delete(void)
{
int num,flag=0;
int found;
int least=0;
clrscr();
temp=start;
if(temp==NULL)
printf("\n sorry dll not created");
else
{
printf("\n Enter the no of deleated");
scanf("%d",&num);
while((flag==0)&&temp!=NULL))
{
found=find(num);
flag=found;
}
if(found==0)
printf("\n no not found");
else
{
if(temp=start)
{
start=start->next;
temp->next=NULL;
start->next=NULL;
free(temp);
getch();
printf("\n the starting node is deleted");
}
else
{
if(temp->next==NULL)
last=1;
else
last=0;
(temp->next)->prev=temp->prev;
(temp->prev)->next=temp->next;
temp->prev=NULL;
free(temp);
if(last)
printf("\n the last node is deleted");
else
printf("\n the intermediate node is deleted");
}
}
}
}
RESULT:
Thus,a program to implement double linked list was written,executed and output was
verified.
FLOWCHART:
Main Program:
START
Read choice
Switch
choice
while
(choice!=6)
STOP
f data=num
i next=null
rprev=r
qnext=r
Return
OUTPUT:
AIM:
To write a program to implement circularly linked list and perform the operations such as creation,
insertion, deletion and view
ALGORITHM:
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
struct node
{
int data;
struct node *link;
};
void addcirq(struct node**,struct node**,int);
int delcirq(struct node**,struct node**);
void cirq_display(struct node*);
void main()
struct node *front,*rear;
front=rear=NULL;
addcirq(&front,&rear,10);
addcirq(&front,&rear,17);
addcirq(&front,&rear,18);
addcirq(&front,&rear,23);
addcirq(&front,&rear,12);
clrscr();
printf(“\n Before deletion:\n”);
cirq_display(front);
delcirq(&front,&rear);
delcirq(&front,&rear);
printf(“\n After deletion:\n”);
cirq_display(front);
getch();
}
Void addcirq (struct node **f,struct node **r,int item)
{
struct node *q;
q=malloc (sizeof(struct node));
qdata+=item;
if(*f==null)
*f=q;
else
(*r)link=q;
(*r)q;
(*r)link=(*f);
}
int delcirq (struct node **f,struct node **r)
{
struct node *q;
int item;
if(*f==null)
printf(“queue is empty”);
else
{
if(*f==*r)
{
item=(*f)data;
free(*f);
*f=null;
*r=null;
}
else
{
q=*f;
item=qdata;
*f=(*f)link;
(*r)link=*f;
free(q);
}
return(item);
}
return Null;
}
void cirq_display(struct node *f)
{
struct node *q=f,*p=Null;
while(q1=p)
{
printf(“%d”,qdata);
qqlink;
p=f;
}
}
RESULT:
Thus a C program to implement circularly linked list was written, executed and the output was
verified.
FLOW CHART:
start
Start=null
Read
choice
Switch
(choic
e)
yes
While
Ch<=
0
no
stop
BINARY TREE
AIM:
To write a C-program to create the simple binary tree and recursive traversal.
ALGORITHM:
PROGRAM:
#include<stdio.h>
#include<conio.h>
node *get_node ( )
{
node *temp;
temp = (node *) malloc (size of(node));
temp ->left = null;
temp->right = null;
return temp;
}
void insert (node *root, node *new)
{
char ch;
printf(“\n where to insert left/right of %d”, root->data);
ch=getch( );
if((ch==’r’) || (ch==’r’))
{
if(root->right == null)
{
root->right=new;
}
else
insert (root->right,new);
}
else
{
if (root->left ==null)
{
root->left=new;
}
else
insert (root->left,new);
}
}
RESULT:
Thus a C-program to create the simple binary tree and recursive traversal had been executed and
verified.
FLOW CHART:
Start
Read ch
A
A
switc
h
(ch)
Create
post
node(b preorder insert(
order(
tree, (btree) btree)
btree)
temp)
while(c
h <=8)
Stop
tree *insert
node (tree
*btree,tree*
temp)
if(btree=
=NULL)
if(temp->
data> btree-
>data)
if(temp->
data<
btree-
>data)
if(temp-
>
data=btr
ee-
>data)
Print “Data
already exists”
return
OUTPUT:
5
SEQUENTIAL SEARCH
AIM:
To write a C-program to perform the linear search operation on some number of elements.
ALGORITHM:
PROGRAM:
#include<stdio.h>
#include<conio.h>
#define max 10
int a[max] , n , i;
void create ( )
{
printf (“ \n how many elements”);
scanf (“%d”,&n);
printf (“\n enter the elements”);
for (i=0;i<n;i++)
scanf (“%d”.& a[i]);
}
void display ( )
{
printf (“\n the elements are”);
for (i=0;i<n;i++)
printf (“\n %d ”,a[i]);
}
search (int k)
{
for(i=0;i<n;i++)
{
if(a[i]= =k)
return 1;
}
return 0;
}
void main( )
{
int status, key;
clrscr ( );
create ( );
display ( );
printf (“\m enter the element which you wish to search”);
scanf (“%d”,& key);
status = search (key);
if (status = = 1)
printf(“\n the element is present”);
else
printf(“\n the element is not found”);
getch ( );
}
RESULT:
Thus the c – program to perform the linear search operation on some number of elements had
been executed and verified.
FLOW CHART:
Main program:
for(i=0;i<n;
i++)
Enter the
element
linearsearch()
if
search
= =-1
Stop
Sub-program:
linearsearch()
for(i=0;i<n;i++)
if
a[i]=
=k
return 1
OUTPUT:
How many elements 5
Enter the elements 23 36 56 48 79
Enter the element you wish to search 48
The element is present
QUICKSORT
AIM:
ALGORITHM:
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define MAX 10
void main()
{
int n,i,x[MAX];
void quick(int x[],int,int);
clrscr();
printf(“How many elements do you wish to sort?\n”);
scanf(“%d”,&n);
for(i=0;i<n;i++)
{
printf(“\n Enter the elements:”);
scanf(“%d”,&x[i]);
}
quick(x,0,n-1);
printf(“\n\t\t The sorted elements are:” );
for(i=0;i<n;i++)
printf(“\n\t\t%d”,x[i]);
getch();
}
void quick(int x[MAX],int lb,int ub)
{
int q,pivot,i,lower;
void interchange(int x[MAX],int a,int b;
interchange(x,lb,(lb+ub)/2);
pivote=x[lb];
lower=lb;
for(i=lb+1;i<=ub;i++)
{
if(x[i]<pivot)
{
lower=lower+1;
interchange(x,lower,i);
}
}
interchange(x,lb,lower);
q=lower;
return q;
void interchange(int x[MAX],int a,int b)
{
int temp;
temp=x[a];
x[a]=x[b];
x[b]=temp;
}
RESULT:
Print “How
many
elements”
Read n
for(i=0;i<n;i++)
Print “Enter
the
elements”
Read x[i]
Next i
Quick(x,0,n-1)
Print “The
sorted elements
are “
for(i=0;i<n;i++)
Print x[i]
STOP
OUTPUT:
AIM:
ALGORITHM:
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<process.h>
void bubblesort(int a[20],int n);
void bubblesort(int a[20],int n);
{
int i,j,temp;
for(i=0;i<n-1;i++)
{
for(j=0;j<n;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
for(i=0;i<n;i++)
printf(“\n\t%d”,a[i]);
}
void main()
{
int a[20],i,n;
int ch;
clrscr();
printf(“Enter the number of elements in the sorting array”);
scanf(“%d”,&n);
printf(“Enter the number of elements in the array”);
for(i=0;i<n;i++)
scanf(“%d”,&a[i]);
bubblesort(a,n);
}
RESULT:
Start
Bubble(a,n)
For(i=o;i<n;i+ i=0
+)
Read
Elements whil
e
j<n
stop
j=0
Whil
e
J<n-1
Ajj=a[j+1]
;
A[]
[j]<a[j+1]
Swapped=TRUE;
Temp=a[j];
A[j]=a[j+1];
J+1
OUTPUT:
AIM:
To write and execute a program to implement the operation in double linked list.
ALGORITHM:
Step:1 Start.
Step:2 Read.
Step:3 If c==1,create new node.
Step:4 If c==2,insert new node at specific location.
Step:5 If c==3,delete a particular node.
Step:6 If c==4,display the list.
Step:7 If c==5,exit.
Step:8 Stop.
PROGRAM:
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
struct node
{
int data;
struct node*next,*prev;
}
*New,*new1,*temp,*start,*dummy;
void add(void)
struct node*get_node();
void display(void);
void delete(void);
int find(int);
int first=1;
void main()
{
char ans;
int choice,num,found=0;
struct=NULL;
do{
clrscr();
printf("Program for double link list");
printf("\n1.insert\n2.delete\n3.display\n4.searching\n5.exit");
printf("\n enter your choice:");
scanf("%d",&choice);
switch(choice);
{
case 1:add()
break;
case 2:delete()
break;
case 3:display()
break;
case 4:printf("enter no to be search:");
scanf("%d",&num);
temp=start;
while((temp!=NULL)&&(found==0))
found=find(num);
if(found)
printf("\n no is present");
break;
case 5:exit(0)
}
printf("\n do u want to continue?");
ans=getch();
}
while(ans=='y'||ans=='Y')
getch();
}
void add(void)
{
clrscr();
New=get_node();
printf("\n\n\tEnter the element:");
scanf("%d",&New->data);
if(first==1)
{
start=New;
first=0;
}
else
{
dummy=start;
while(dummy->next!=NULL)
dummy=dummy->next;
dummy->next=New;
New->pre=dummy;
}
}
struct node*get_node()
{
new1=(node*)malloc(sizeof(struct node));
new1->next=NULL;
new1->prev=NULL;
return(new1);
}
void display(void)
{
clrscr();
temp=stat;
if(temp==NULL);
printf("\n double linked list is empty");
else
{
while(temp!=NULL)
{
printf("%d",temp->data);
temp=temp->next;
}
printf("NULL");
}
getch();
}
int find(int num)
{
if(temp->data==num)
return(1)
else
temp=temp>next;
return 0;
}
void delete(void)
{
int num,flag=0;
int found;
int least=0;
clrscr();
temp=start;
if(temp==NULL)
printf("\n sorry dll not created");
else
{
printf("\n Enter the no of deleated");
scanf("%d",&num);
while((flag==0)&&temp!=NULL))
{
found=find(num);
flag=found;
}
if(found==0)
printf("\n no not found");
else
{
if(temp=start)
{
start=start->next;
temp->next=NULL;
start->next=NULL;
free(temp);
getch();
printf("\n the starting node is deleted");
}
else
{
if(temp->next==NULL)
last=1;
else
last=0;
(temp->next)->prev=temp->prev;
(temp->prev)->next=temp->next;
temp->prev=NULL;
free(temp);
if(last)
printf("\n the last node is deleted");
else
printf("\n the intermediate node is deleted");
}
}
}
}
RESULT:
Thus,a program to implement double linked list was written,executed and output was
verified.
START
Read choice
Switch
choice
while
(choice!=6)
STOP
f data=num
i next=null
rprev=r
qnext=r
Return
OUTPUT:
ALGORITHM:
Step:1 Start the program.
Step:2 Read the given string.
Step:3If the input symbol read is ‘c’ push it into the stack.
Step:4 If the input symbol read is an operand then place it in the expression
Step:5 If the input symbol read is an operator,then
a)check the precedence of the operator read. If it has a higher precedence then remove
it from the stack and place it in the postfix expression.
Repeat 5a) till operator in the stack has a higher precedence than that being read.
Step:6 If the input symbol read is closing parenthesis then pop all the operators from the stack
and place them in the postfix expression till the opening parenthesis is encountered.
Step:7 Stop the program.
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
#include<stdlib.h>
#include<process.h>
#include<string.h>
char pre[40],post[40];
int top=0,st[20];
void postfix();
void push(int);
char pop();
void main()
{
clrscr();
printf(“Enter the expression”);
scanf(“%s”,&pre);
postfix();
getch();
}
void postfix()
{
int i,j=0;
fro(i=0;pre[i]!=’\0’;i++)
{
switch(inf[i])
{
case ‘+’:
while(st[top]>=1)
post[j++]=pop();
push(1);
break;
case ‘-’:
while(st[top]>=1)
post[j++]=pop();
push(2);
break;
case ‘*’:
while(st[top]>=3)
post[j++]=pop();
push(3);
break;
case ‘/’:
while(st[top]>=3)
post[j++]=pop();
push(4);
break;
case ‘^’:
while(st[top]>=1)
post[j++]=pop();
push(5);
break;
case ‘(’:
push(0);
break;
case ‘)’:
while(st[top]!=0)
post[j++]=pop();
top--;
break;
default:
post[j++]=pre[i];
}
}
while(top>0)
post[j++]=pop();
printf(“\nThe postfix expression is”,post);
}
void push(int ele)
{
top++;
st[top]=ele;
}
char pop()
{
int el;
char e;
el=st[top] ;
top--;
switch(el)
{
case 1:
e= ‘+’;
break
case 2:
e= ‘-’;
break;
case 3:
e= ‘*’;
break;
case 4:
e= ‘/’;
break;
case 5:
e= ‘^’;
break;
}
return(e);
}
RESULT:
Thus a C program to convert the prefix expression to postfix expression was written, executed and
the output was verfied.
FLOWCHART:
START
Main
program
Read input
Pre 2 post
STOP
Sub-Program
Pre 2 post
while yes
input()!
=10
no
if
operat
ed
Print the expression
op2=pop()
op1=pop()
print
postfix
exp()
i++
OUTPUT:
AIM:
To write a program to implement circularly linked list and perform the operations such as creation,
insertion, deletion and view
ALGORITHM:
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
struct node
{
int data;
struct node *link;
};
void addcirq(struct node**,struct node**,int);
int delcirq(struct node**,struct node**);
void cirq_display(struct node*);
void main()
struct node *front,*rear;
front=rear=NULL;
addcirq(&front,&rear,10);
addcirq(&front,&rear,17);
addcirq(&front,&rear,18);
addcirq(&front,&rear,23);
addcirq(&front,&rear,12);
clrscr();
printf(“\n Before deletion:\n”);
cirq_display(front);
delcirq(&front,&rear);
delcirq(&front,&rear);
printf(“\n After deletion:\n”);
cirq_display(front);
getch();
}
Void addcirq (struct node **f,struct node **r,int item)
{
struct node *q;
q=malloc (sizeof(struct node));
qdata+=item;
if(*f==null)
*f=q;
else
(*r)link=q;
(*r)q;
(*r)link=(*f);
}
int delcirq (struct node **f,struct node **r)
{
struct node *q;
int item;
if(*f==null)
printf(“queue is empty”);
else
{
if(*f==*r)
{
item=(*f)data;
free(*f);
*f=null;
*r=null;
}
else
{
q=*f;
item=qdata;
*f=(*f)link;
(*r)link=*f;
free(q);
}
return(item);
}
return Null;
}
void cirq_display(struct node *f)
{
struct node *q=f,*p=Null;
while(q1=p)
{
printf(“%d”,qdata);
qqlink;
p=f;
}
}
RESULT:
Thus a C program to implement circularly linked list was written, executed and the output was
verified.
FLOW CHART:
start
Start=null
Read
choice
Switch
(choic
e)
yes While
Ch<=
0 no
stop
SINGLY LINKED LIST
AIM:
To write a ‘C’ program to perform operations like create, insert,delete, seach,and display in a
singly linked list.
ALGORITHM:
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define TRUE 1
#define FALSE 0
typedef struct SLL
{
int data;
struct SLL*next;
}node;
Node*create();
void main()
{
int choice, val;
char ans;
node*head;
void display(node*);
node*search(node*,int);
void insert(node*)
void delete(node**);
head=NULL;
do
{
clrscr();
printf(“ program to perform various operations on linked list”);
printf(“\n 1. create \n 2. display\n 3. search\n 4. insert\n 5. delete \n 6.quit”);
printf(“ \n Enter your choice”);
scanf(“%d”,& choice);
switch(choice)
{
case1:
head=create();
break;
case 2:
display (head);
break;
case 3:
printf(“ enter the element you want to search”);
scanf(“%d”,val);
scanf(“head,val);
break;
case 4:
insert(head);
break;
case 5:
dele (head);
break;
case 6:
exit(0);
default;
clrscr();
printf(“ Invalid choice,try again”);
getch();
}
}
while(choice!=t);
}
node*create()
{
node*temp,*new,*head;
int val, flag;
char ans=’Y’;
node*get_node();
temp=NULL;
flag=TRUE;
do
{
{
printf(“\n Enter the element:”);
scanf(“%d”,&val);
new=get_node();
If(new ==NULL)
printf(“ \n memory is not allocated”);
new->data=val;
if(flag)
{
head= new;
temp=head;
flag=FALSE;
}
else
{
temp->next=new;
temp=new;
}
printf(“\n do you want to enter more element?(y/n)”);
ans=getche();
}
while (ans==’Y’);
printf(“The singly linked list is created”);
getch();
clrscr();
return head;
}
node*get_node()
{
node*temp;
temp=(node*)malloc(size of node));
temp->next=NULL;
return temp;
}
void display(node*head)
{
node*temp;
temp=head;
if(temp==NULL)
{
printf(“\n The list is empty”);
getch();
clrscr();
return;
}
while(temp!=NULL)
}
printf(“%d->”,temp->data);
temp=temp->next;
}
printf(“NULL”);
}
node*searchnode(node*head,int key)
{
node*temp;
int found;
temp=head;
if(temp==NULL)
{
printf(“The linked list is empty”);
getch();
clrscr();
return NULL;
}
found=FALSE;
while(temp!=NULL&&!found){
if(found)
{
printf(“The element is present in the list\n”);
getch();
return temp;
}
else
{
printf(“The element is not present in the list\n”);
getch();
return NULL;
}
}
RESULT:
Thus the c-program for singly linked list had been written and the operations such as creation
,deletion, insertion, display was performed.
FLOWCHART:
Main Program
start
Start=null
Read
choice
Switch
(choic
e)
While
Ch<=
yes 0 no
stop
Sub-Program:
view()
list=head
if
list=NUL
L
print roll no
name
for(list!=NULL;list=list->link)
print roll no
name
next list
return
OUTPUT: