You are on page 1of 27

Program26:

/* Write a C program that uses functions to perform the following operations on


singly linked list.:
i) Creation ii) Insertion iii) Deletion iv) Traversal */

#include<stdio.h>
#include<conio.h>
#include<alloc.h>
typedef struct sampnode
{
int id;int nodevalue;
char name[10];
struct sampnode *next;
}sampnode_t;
sampnode_t *phead,*ptail;
void createnode(sampnode_t*);
void fillnode(sampnode_t*);
void dispnodes();
sampnode_t *dispnode(int);
void insertnode(sampnode_t*);
int deletenode(int);
sampnode_t *searchnode(int val);
int flag=0;
main()
{
int choice,nodevalue;
char ch;
sampnode_t *temp,*search,*disp;
clrscr();
do
{
printf("\n1.create node");
printf("\n2.display all nodes");
printf("\n3.disp node:");
printf("n4.search node");
printf("\n5.delete node");
printf("\n6.insert node");
printf("\n7.exit:");
printf("\nenter ur choice\n");
scanf("%d",&choice);
switch(choice)
{
case 1:
temp=(sampnode_t*)malloc(sizeof(sampnode_t));
fillnode(temp);
createnode(temp);
break;
case 2:
dispnodes();
break;
case 3:
printf("ente node value to display:");
scanf("%d",&nodevalue);
disp=dispnode(nodevalue);
if(disp)
printf("\n id____%d\n.....%s\n......",disp->id,disp->name);
else
printf("ur record not found.:");
break;
case 4:
printf("enter nodelvalue to search.....");
scanf("%d",&nodevalue);
search=searchnode(nodevalue);
if(search)
printf("ur record found....:\nid....:%d\nname....%s\n",search->id,search->name);
else
printf("ur record not found....:");
break;
case 5:
printf("enter node value to delete:");
scanf("%d",&nodevalue);
flag=deletenode(nodevalue);
if(flag)
printf("record successfully deleated:");
else
printf("not found record...");
break;
case 6:
temp=(sampnode_t*)malloc(sizeof(sampnode_t));
fillnode(temp);
insertnode(temp);
break;
default:
exit(0);
}
printf("\ndo you want one more transaction....:y\n\n");
fflush(stdin);
scanf("%c",&ch);
}
while(ch=='y');
getch();
}
void fillnode(sampnode_t *pn)
{
printf("\nenter id....:");
scanf("%d",&pn->id);
printf("\nenter name ....:");
scanf("%s",pn->name);
}
void createnode(sampnode_t *pn)
{
sampnode_t *temp;
temp=phead;
if(temp==NULL)
{
phead=pn;
pn->next=NULL;
}
else
{
while(temp->next)
temp=temp->next;
temp->next=pn;
pn->next=NULL;
}
}
void dispnodes()
{
sampnode_t *temp;
temp=phead;
while(temp)
{
printf("\nid...%d\nname....%s\n",temp->id,temp->name);
temp=temp->next;
}
}
sampnode_t *dispnode(int nodevalue)
{
sampnode_t *temp;
temp=phead;
while(temp)
{
if(temp->id==nodevalue)
{
return temp;
}
temp=temp->next;
}
}
sampnode_t *searchnode(int nodevalue)
{
sampnode_t *temp;
temp=phead;
while(temp)
{
if(temp->id==nodevalue)
{
return temp;
}
temp=temp->next;
}
}
int deletenode(int nodevalue)
{
sampnode_t *temp;
temp=phead;
if(phead->id==nodevalue)
{
phead=phead->next;
flag=1;
}
while(temp->next)
{
if(temp->next->id==nodevalue)
{
temp->next=temp->next->next;
flag=1;
}
temp=temp->next;
}
return(flag);
}
void insertnode(sampnode_t *pn)
{
sampnode_t *temp,*temp1;
int nodevalue;
temp=phead;
printf("enter node value to insert:");
scanf("%d",&nodevalue);
if(phead==NULL)
{
phead=pn;
pn->next=NULL;
}
if(phead->id>nodevalue)
{
temp1=phead;
phead=pn;
pn->next=temp1;
}
while(temp->next)
{
if(temp->next->id>nodevalue)
{
temp1=temp->next;
temp->next=pn;
pn->next=temp1;
break;
}
temp=temp->next;
}
}

Program27:
/* Write C programs that use both recursive and non recursive functions
to perform the following searching operation for a Key value in a given list of
integers : Linear search */

#include<stdio.h>
#include<conio.h>
main()
{
int a[10],n;
int i,k,pos;
clrscr() ;
printf("enter the number of elements in the list \n");
scanf("%d",&n);
printf("enter the elements of list\n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("enter the element to search\n");
scanf("%d",&k);
for(i=0;i<n;i++)
{
if(a[i]==k)
{
pos=i;
break;
}
}
printf("%d is present in list with position%d\n",k,pos);
getch();
}

#include<stdio.h>
#include<conio.h>
void lsearch(int [],int,int);
main()
{
int a[10],i,n,ele;
clrscr();
printf("enter n value");
scanf("%d",&n);
printf("enter values");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("enter the elements");
scanf("%d",&ele);
lsearch(a,n,ele);
}
void lsearch(int a[10],int num,int i)
{
int f=0;
if(a[num]==i)
{
printf("pos=%d",num);
f=1;
}
else if((num==0)&&(f==0))
{
printf("notfound\n");
}
else
lsearch(a,num-1,i);
getch();}
Program28:
/* Write C programs that use both recursive and non recursive functions to
perform the following searching operations for a Key value in a given list of
integers :Binary search*/

#include<stdio.h>
#include<conio.h>
main()
{
int a[10],n;
int start,end,mid;
int i,k,pos;
clrscr();
printf("enter the no of elements in list \n");
scanf("%d",&n);
printf("enter the elements of list\n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("enter the element to search\n");
scanf("%d",&k);
start=0;
end=n-1;
while(start<=end)
{
mid=((start+end)/2);
if(a[mid]==k)
{
pos=mid;
break;
}
else if(a[mid]>k)
end=mid-1;
else
start=mid+1;
}
printf("%d is present in list with posion%d\n",k,pos);
getch();
}

#include<stdio.h>
#include<conio.h>
void main()
{
int a[10],num,i,ele,pos;
clrscr();
printf("\n enter the number of elements:");
scanf("%d",&num);
printf("\n enter the elements :\n");
for(i=0;i<num;i++)
scanf("%d",&a[i]);
printf("\n\nenter the element you want to search:\n\n");
scanf("%d",&ele);
pos=b_search_recursive(a,0,num,ele);
if(pos==-1)
printf("element is not found");
else
printf("element is found at %d position",pos);
getch();
}
int b_search_recursive(int a[],int start,int end,int i)
{
int mid;
if(start<=end)
{
mid=(start+end)/2;
if(a[mid]==i)
return mid;
else if(a[mid]>i)
return b_search_recursive(a,start,mid-1,i);
else
return b_search_recursive(a,mid+1,end,i);
}
return-1;
}

Program29:
/*Write a c program to implement insertion sort */

#include<stdio.h>
#include<conio.h>
main()
{
int num[10];
int n,i,j,t;
clrscr();
printf("enter the no of elements in list");
scanf("%d",&n);
printf("enter the elements in list \n");
for(i=0;i<n;i++)
scanf("%d",&num[i]);
for(i=1;i<n;i++)
{
t=num[i];
for(j=i-1;j>=0;j--)
{
if(num[j]>t)
num[j+1]=num[j];
else
break;
}
num[j+1]=t;
}
printf("sorted list is\n");
for(i=0;i<n;i++)
printf("%d\t",num[i]);
getch();
}

Program30:
/*Write a c program to implement bubble sort */

#include<stdio.h>
#include<conio.h>
main()
{
int num[10];
int i,j,t,n;
clrscr();
printf("enter the no of elements in list \n");
scanf("%d",&n);
printf("enter the elements in list \n");
for(i=0;i<n;i++)
{
scanf("%d",&num[i]);
}
for(j=0;j<=n-1;j++)
{
for(i=0;i<n-1;i++)
{
if(num[i]>num[i+1])
{
t=num[i];
num[i]=num[i+1];
num[i+1]=t;
}
}
}
printf("sorted list is \n");
for(i=0;i<n;i++)
{
printf("%d\t",num[i]);
}
getch();
}

Program31:
/*Write a c program to implement quicksort */

#include<stdio.h>
#include<conio.h>
int num[10],n,i;
main()
{
clrscr();
printf("enter the no of elements in list\n");
scanf("%d",&n);
printf("enter the elements in list \n");
for(i=0;i<n;i++)
{
scanf("%d",&num[i]);
}
quicksort(0,n-1);
printf("sorted list is \n");
for(i=0;i<n;i++)
{
printf("%d\t",num[i]);
}
getch();
}

quicksort(int low,int high)


{
int pivot,t;
int a=low;
int b=high;
pivot=(low+high)/2;
while(a<=b)
{
while(num[a]<num[pivot])
a++;
while(num[b]>num[pivot])
b--;
if(a<=b)
{
t=num[a];
num[a]=num[b];
num[b]=t;
a++;
b--;
}
}
if(low<b)
quicksort(low,b);
if(a<high)
quicksort(a,high);
}

Program32:
/*Write a c program to implement mergesort*/

#include<stdio.h>
#include<conio.h>
int num[10],n,i;
main()
{
clrscr();
printf("enter the no of elements in list \n");
scanf("%d",&n);
printf("enter the elements in list \n");
for(i=0;i<n;i++)
{
scanf("%d",&num[i]);
}
mergesort(0,n-1);
printf("sorted listis\n");
for(i=0;i<n;i++)
printf("%d\t",num[i]);
getch();
}
mergesort(int low,int high)
{
int mid;
if(low<high)
{
mid=(high+low)/2;
mergesort(low,mid);
mergesort(mid+1,high);
merge(low,mid,high);
}
}
merge(int l,int m,int h)
{
int b[10];
int i=l;
int j=m+1;
int k=l;
while(i<=m&&j<=h)
{
if(num[i]<=num[j])
{
b[k]=num[i];
k++;
i++;
}
else
{
b[k]=num[j];
k++;
j++;
}
}
while(i<=m)
b[k++]=num[i++];
while(j<=h)
b[k++]=num[j++];
for(k=l;k<=h;k++)
num[k]=b[k];
}

Program33:
/*Write a c program to implement seletionsort*/

#include<stdio.h>
#include<conio.h>
void main()
{
int num[10],i,j,t,n,min;
clrscr();
printf("Enter the no. of elements in list\n");
scanf("%d",&n);
printf("Enter the elements in list\n");
for(i=0;i<n;i++)
{
scanf("%d",&num[i]);
}
for(i=0;i<=n-1;i++)
{
min=i;
for(j=i+1;j<=n-1;j++)
{
if(num[j]<num[min])
{
min=j;
}
}
t=num[i];
num[i]=num[min];
num[min]=t;
}
printf("sorted list is\n");
for(i=0;i<n;i++)
{
printf("%d\t",num[i]);
}
getch();
}

Program34:
Write C programs that implement stack (its operations) using
i) Arrays ii) Pointers

#include<stdio.h>
#include<conio.h>
struct node
{
int data;
struct node*next;
};
struct node *phd,*pn;
void push();
void pop();
void display();
main()
{
int choice;
char ch='y';
clrscr();
while(ch=='y')
{
printf("push \n2:pop\n3:display\n");
printf("enter the option\n");
scanf("%d",&choice);
switch(choice)
{
case 1:
push();
break;
case 2:
pop();
break;
case 3:
display();
break;
default:
exit(0);
}
printf("enter the choice \n");
fflush(stdin);
scanf("%c",&choice);
}
}
void push()
{
struct node*temp,*temp1;
pn=malloc(sizeof(struct node));
printf("enter the data \n");
scanf("%d",&pn->data);
if(phd==NULL)
{
phd=pn;
pn->next=NULL;
}
else
{
temp=phd;
while(temp->next)
temp=temp->next;
temp->next=pn;
pn->next=NULL;
}
}
void pop()
{
struct node *temp,*temp1;
temp=phd;
while(temp->next)
{
temp1=temp;
temp=temp->next;
}
printf("poped data is \n");
printf("%d",temp->data);
temp1->next=NULL;
}
void display()
{
struct node*temp;
temp=phd;
while(temp)
{
printf("%d\n",temp->data);
temp=temp->next;
}
}

#include<stdio.h>
#include<conio.h>
int data;
int stack[100]
int stkele=-1;
int x;
void pop();
void push();
void display();
main()
{
int choice;
char ch='y';
while(ch=='y')
{
printf("1:push\n2:pop\n3:display\n");
printf("enter the option");
scanf("%d",&choice);
switch(chioce)
{
case 1:
push();
break;
case 2:
pop();
break;
case 3:
display();
break;
default:
exist(0);
}
printf("enter the choice \n");
fflush(stdin);
scanf("%c",&ch);
}
}
void push()
{
pn=malloc(sizeof(struct node))
printf("enter the data \n");
scanf("%d",&x);
stkele++;
stack[stkele]=x;
if(stkale==100)
{
printf("stack is over flow \n");
}
}
void pop()
{
printf("poped list is \n");
printf("%d\n",stack[stakele]);
stkele--;
if(stkele==-1)
{
printf("stack is under flow \n");
}
}
viod display()
{
int i;
for(i=0;i<=stkele;i++)
{
printf("%d\n",stack[i]);
}
}

Program35:
Write C programs that implement queue (its operations) using
i) Arrays ii) Pointers

#include<stdio.h>
#include<conio.h>
struct node
{
int data;
struct node *next;
}
struct node *phd,*pn;
main()
{
int choice;
char ch='y';
while(ch=='y')
{
printf("1:put\n2:get\n3:display\n");
printf("enter the option");
scanf("%d",&chioce);
switch(choice)
{
case 1:
put();
break;
case 2:
get();
break;
case 3:
display();
break;
default;
exit(0);
}
printf("enter the chioce\n");
fflush(stdin);
scanf("%c",&ch);
}
}
void put()
{
struct node *temp;
pn=malloc(sizeof (struct node));
printf("enter the data\n");
scanf("%d",&pn->data);
if(phd==NULL)
{
phd=pn;
pn->next=NULL;
}
temp=phd;
while(temp->next)
temp=temp->next;
temp->next=pn;
pn->next=NULL;
}
void get()
{
printf("removed elements is \n");
printf("%d",phd->data);
phd=phd->next;
}
void display()
{
struct node *temp;
temp=phd;
while(temp)
{
printf("%d",temp->data);
temp=temp->next;
}
}

#include<stdio.h>
#include<conio.h>
int queue[100]
int rear=-1,front=-1;
int x;
void put();
void get();
void display();
main()
{
int choice;
char ch='y';
while(ch=='y')
{
printf("1:push\n2:pop\n3:display\n");
printf("enter the option");
scanf("%d",&choice);
switch(chioce)
{
case 1:
put();
break;
case 2:
get();
break;
case 3:
display();
break;
default:
exist(0);
}
printf("enter the choice \n");
fflush(stdin);
scanf("%c",&ch);
}
}
void put()
{
pn=malloc(sizeof(struct node))
printf("enter the data \n");
scanf("%d",&x);
rear++;
queue[rear]=x;
if(rear==100)
{
printf("queue is full \n");
}
}
void get()
{
printf("removed ele is \n");
front++;
printf("%d\n",.queue[front]);
if(front==rear)
{
printf("queue is empty \n");
}
}
viod display()
{
int i;
for(i=front;i<rear;i++)
{
printf("%d\n",queue[i]);
}
}

Program36:
/* Write a C program that uses functions to perform the following operations on
double linked list.:
i) Creation ii) Insertion iii) Deletion iv) Traversal */

#include<stdio.h>
#include<conio.h>
void create();
void display();
void search();
void delet();
void insert();
struct node
{
int data;
struct node *prev,*next;
};
struct node *phd,*pn;
void main()
{
char ch='y';
int choice;
clrscr();
while(ch=='y')
{
printf("1.create\n 2.display\n 3.search\n 4.delete\n 5.insert\n");
printf("Enter your choice\n");
scanf("%d",&choice);
switch(choice)
{
case 1:
create();
break;
case 2:
display();
break;
case 3:
search();
break;
case 4:
delet();
break;
case 5:
insert();
break;
default:
exit(0);
}
printf("do you want to continue\n");
fflush(stdin);
scanf("%c",&ch);
}
getch();
}
void create()
{
struct node *temp;
pn=(struct node *)malloc(sizeof(struct node));
printf("Enter the data\n");
scanf("%d",&pn->data);
if(phd==NULL)
{
phd=pn;
pn->next=NULL;
pn->prev=phd;
}
temp=phd;
while(temp->next)
temp=temp->next;
temp->next=pn;
pn->next=NULL;
pn->prev=temp;
}
void display()
{
struct node *temp,*temp1;
temp=phd;
while(temp)
{
printf("%d\n",temp->data);
temp1=temp;
temp=temp->next;
}
while(temp1!=NULL)
{
printf("%d\n",temp1->data);
temp1=temp1->prev;
}
}
void search()
{
int value,f=0;
struct node *temp,*temp1;
printf("Enter the value to search\n");
scanf("%d",&value);
temp=phd;
while(temp)
{
if(temp->data==value)
{
printf("%d",temp->data);
f=1;
}
temp1=temp;
temp=temp->next;
}
if(f==0)
printf("Element is not found\n");
f=0;
while(temp1)
{
if(temp1->data==value)
{
printf("%d",temp1->data);
f=1;
}
temp1=temp1->prev;
}
if(f==0)
printf("Element is not found\n");
}
void delet()
{
int value,f=0;
struct node *temp,*temp1;
printf("Enter the value to delete\n");
scanf("%d",&value);
temp=phd;
if(phd->data==value)
{
phd=phd->next;
f=1;
}
while(temp->next)
{
if(temp->next->data==value)
{
temp->next=temp->next->next;
f=1;
}
temp1=temp;
temp=temp->next;
}
if(f==0)
printf("Element is not deleted\n");
f=0;
while(temp1->prev)
{
if(temp1->prev->data==value)
{
temp1->prev=temp1->prev->prev;
f=1;
}
if(f==0)
printf("Element is not deleted\n");
}
}
void insert()
{
struct node *temp,*temp1;
int value;
printf("Enter the value to insert\n");
scanf("%d",&value);
temp=phd;
while(temp)
{
if(temp->data>value)
{
temp1=temp;
temp=pn;
pn->next=temp1;
}
temp=temp->next;
}
}
Program37:
Write a C program that uses functions to perform the following:
i) Creating a Binary Tree of integers
ii) Traversing the above binary tree in preorder, inorder and postorder.

#include<stdio.h>
#include<process.h>
#include<conio.h>
struct node
{
int data;
node *left,*right;
}*root;
void insert(int x)
{
node *p,*t;
if(root==NULL)
{
root=(struct node *)malloc(sizeof(struct node));
root->data=x;
root->left=NULL;
root->right=NULL;
}
else
{
p=root;
while(p)
{
if(p->data==x)
{
printf("node is already exists");
return;
}
else if(p->data<x)
{
if(p->right==NULL)
{
t=(struct node *)malloc(sizeof(struct node));
; t->data=x;
p->right=t;
t->right=NULL;
t->left=NULL;
return;
}
else
{
p=p->right;
continue;
}
}
else
{
if(p->left==NULL)
{
t =(struct node *)malloc(sizeof(struct node));
t->data=x;
p->left=t;
t->right=NULL;
t->left=NULL;
return;
}
else
{
p=p->left;
continue;
}
}
}
}
}
void inorder(node *p)
{

if(p)
{
inorder(p->left);
printf(“%d\n”,p->data);
inorder(p->right);
}
}
void preorder(node *p)
{

if(p)
{
printf(“%d\n”,p->data);
preorder(p->left);
preorder(p->right);
}
}
void postorder(node *p)
{
if(p)
{
postorder(p->left);
postorder(p->right);
printf(“%d\n”,p->data);
}
}

main()
{
int ch,a;
clrscr();
while(1)
{
scanf(“%d”,&ch);
switch(ch)
{
case 1:
scanf(“%d”,&a);
insert(a);
break;
case 2:
inorder(root);
break;
case 3:
preorder(root);
break;
case 4:
postorder(root);
break
default:
exit(0);
}
}
getch();
}

You might also like