|
1.Program for Sorting ‘n’ elements Using bubble sort technique.
|
#include "stdio.h"
#include "conio.h"
void main()
{
int a[10],i,j,n,temp;
clrscr();
printf("Enter no. of Elements:");
scanf("%d",&n);
printf("Enter Values:\n");
for(i=0;i=0;i--)
for(j=0;ja[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
printf("After sorting elements are:\n");
for(i=0;i<n;i++)
printf("%d\t",a[i]);
getch();
}
|
Output
Enter no. of Elements:5
Enter Values:
10
55
16
89
24
After sorting elements are:
10 16 24 55 89
|
|
2.Sort given elements using Selection Sort.
|
#include "stdio.h"
#include "conio.h"
void main( )
{ int a[10],n,i,j,min,loc,temp;
clrscr();
printf("Enter no. of elements :");
scanf("%d",&n);
printf("Enter elements :\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("\nBefore sorting elements are:\n");
for(i=0;i<n;i++)
printf("%d\t",a[i]);
for(i=0;i<n;i++)
{ loc=i;
min=a[i];
for(j=i+1;ja[j])
{ min=a[j];
loc=j;
}
}
temp=a[i];
a[i]=a[loc];
a[loc]=temp;
}
printf("\nAfter sorting elements are:\n");
for(i=0;i<n;i++)
printf("%d\t",a[i]);
getch();
}
|
Output
Enter no. of elements :5
Enter elements :
55
11
77
22
99
Before sorting elements are:
55 11 77 22 99
After sorting elements are:
11 22 55 77 99
|
|
3.Sort given elements using Insertion Sort.
|
#include "stdio.h"
#include "conio.h"
void main()
{ int a[10],n,i,j,temp;
clrscr();
printf("Enter no. of Elements:");
scanf("%d",&n);
printf("Enter Elements:");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=1;i<n;i++)
{ temp=a[i];
for(j=i-1;temp=0;j--)
{
if(a[j]>temp)
{
a[j+1]=a[j];
a[j]=temp;
}
}
}
printf("After Sorting Elements are:\n");
for(i=0;i<n;i++)
printf("%d\t",a[i]);
getch();
}
|
Output
Enter no. of Elements: 5
Enter Elements:
45
21
95
42
13
After Sorting Elements are:
13 21 42 45 95
|
|
4.Sort given elements using Quick Sort.
|
#include "stdio.h"
#include "conio.h"
void main()
{ int a[10],n,i,j;
void qsort(int *,int , int);
clrscr();
printf("Enter no. of elements:");
scanf("%d",&n);
printf("Enter the values:");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
qsort(a,0,n-1);
printf("After Sorting Elements are:\n");
for(i=0;i<n;i++)
printf("%d\t",a[i]);
getch();
}
void qsort(int *a,int lb,int ub)
{ void swap(int *, int *);
if(lb<ub)
{ int i=lb;
int j=ub;
int pivot=lb;
while(i<j)
{
while((a[i]<=a[pivot]) && (ia[pivot])
j--;
if(i<j)
swap(&a[i],&a[j]);
}
swap(&a[j],&a[pivot]);
qsort(a,lb,j-1);
qsort(a,i,ub);
}
}
void swap(int *a,int *b)
{
int temp;
temp=*a;
*a=*b;
*b=temp;
}
|
Output
Enter no. of elements:5
Enter the values:
11
98
15
77
45
After Sorting Elements are:
11 15 45 77 98
|
|
5.Sort given elements using Merge Sort.
|
#include "stdio.h"
#include "conio.h"
void main()
{ int i,n,a[10];
void msort(int a[10],int low, int high);
clrscr();
printf("Enter no. of Elements :");
scanf("%d",&n);
printf("Enter Elements :\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("Before Sorting Elements are:\n");
for(i=0;i<n;i++)
printf("%d\t",a[i]);
msort(a,0,n-1);
printf("\nAfter Sorting Elements are:\n");
for(i=0;i<n;i++)
printf("%d\t",a[i]);
getch();
}
void msort(int a[10],int low, int high)
{ void merge(int a[10],int low, int mid, int high);
int mid;
if(low<high)
{ mid=(low+high)/2;
msort(a,low,mid);
msort(a,mid+1,high);
merge(a,low,mid,high);
}
}
void merge(int a[], int low, int mid, int high)
{ int b[10],i,j,k;
j=mid+1;
i=low;
k=low;
while(i<=mid && j<=high)
if(a[i]=a[j])
{ b[k++]=a[j++];
}
else
{ b[k++]=a[i++];
j++;
}
while(i<=mid)
b[k++]=a[i++];
while(j<=high)
b[k++]=a[j++];
for(k=low;k<=high;k++)
a[k]=b[k];
}
|
Output
Enter no. of Elements :5
Enter Elements :
75
14
98
45
69
Before Sorting Elements are:
75 14 98 45 69
After Sorting Elements are:
14 45 69 75 98
|
|
6.Write a program to perform linear search.
|
#include "stdio.h"
#include "conio.h"
void main()
{
int a[10],n,i,key=0,found=0,loc=0;
clrscr();
printf("Enter the no.of elements:");
scanf("%d",&n);
printf("\nEnter elements:");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("\nElements are:");
for(i=0;i<n;i++)
printf("%d \t",a[i]);
printf("\nEnter key element to search:");
scanf("%d",&key);
for(i=0;i<n && found==0;i++)
{
if(a[i]==key)
{
found=1;
loc=i;
}
}
if(found==0)
printf("\nElement is not found");
else
printf("\nElement is found at:%d",loc);
getch();
}
|
Output
Enter the no.of elements:5
Enter elements:
45
78
14
65
28
Elements are: 45 78 14 65 28
Enter key element to search: 14
Element is found at Position: 2
|
|
7.Write a program to implement binary search.
|
#include "stdio.h"
#include "conio.h"
void main()
{
int a[10],n,i,key;
clrscr();
printf("Enter the no. of Elements:");
scanf("%d",&n);
printf("\nEnter Elements:");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("\nElements are:");
for(i=0;i<n;i++)
printf("%d\t",a[i]);
printf("\nEnter key element to search:");
scanf("%d",&key);
i=bin_search(a,n,key);
if(i!=-1)
printf("\n %d is found",a[i]);
else
printf("Element not found");
getch();
}
int bin_search(int x[],int size, int k)
{
int low,high,mid;
low=-1;
high=size;
while(low+1!=high)
{
mid=(low+high)/2;
if(x[mid]=0 && x[low]==k)
return(low);
else
return(-1);
}
|
Output
Enter the no. of Elements:5
Enter Elements:
11
22
33
44
55
Elements are:11 22 33 44 55
Enter key element to search:33
33 is found
|
|
8.Implement Stack Operations Using Arrays.
|
#include "stdio.h"
#include "conio.h"
int top=0,i;
int stack[10],item;
void push();
void pop();
void display();
void push()
{
if(top==10)
{
printf("\n Stack is full");
}
else
{
printf("\nEnter the item:");
scanf("%d",&item);
top++;
stack[top]=item;
display();
}
}
void pop()
{
if(top==0)
{
printf("\nStack is empty");
}
else
{
item=stack[top];
printf("The deleted item is:%d",item);
top--;
display();
}
}
void display()
{
if(top==0)
{
printf("\nStack is empty");
}
else
{
printf("\nElements in the stack are:");
for(i=1;i<=top;i++)
printf("%d\t",stack[i]);
}
}
void main()
{
int ch;
clrscr();
do
{ printf("\n");
printf("\Menu");
printf("\n 1.Push");
printf("\n 2.Pop");
printf("\n 3.Display");
printf("\n 4.Exit");
printf("\n");
printf("\nEnter the choice: ");
scanf("%d",&ch);
switch(ch)
{
case 1: push();
break;
case 2: pop();
break;
case 3: display();
break;
case 4: break;
}
}while(ch!=4);
}
|
Output
Stack Operations Using Arrays
1.Push
2.PoP
3.Display
4.Exit
Enter your choice: 1
Enter the item:10
Stack Operations Using Arrays
1.Push
2.PoP
3.Display
4.Exit
Enter your choice: 1
Enter the item:20
Stack Operations Using Arrays
1.Push
2.PoP
3.Display
4.Exit
Enter your choice: 1
Enter the item:30
Stack Operations Using Arrays
1.Push
2.PoP
3.Display
4.Exit
Enter your choice: 3
Elements in the stack are:10 20 30
Stack Operations Using Arrays
1.Push
2.PoP
3.Display
4.Exit
Enter your choice: 2
The deleted item is:30
Elements in the stack are:10 20
Stack Operations Using Arrays
1.Push
2.PoP
3.Display
4.Exit
Enter your choice: 2
The deleted item is:20
Elements in the stack are:10
Stack Operations Using Arrays
1.Push
2.PoP
3.Display
4.Exit
Enter your choice: 2
The deleted item is:10
Stack is empty
Stack Operations Using Arrays
1.Push
2.PoP
3.Display
4.Exit
Enter your choice: 4
|
|
9.Converting infix expression to postfix expression by using stack.
|
#include "stdio.h"
#include "conio.h"
#define max 30
char data[max];
int top=-1;
void push(char ch)
{
if(top==(max-1))
printf("\n Stack overflow");
else
{
top++;
data[top]=ch;
}
}
int empty()
{
return(top==-1);
}
char pop()
{
char temp;
if(top==-1)
printf("Stack is empty");
else
{
temp=data[top];
top--;
}
return temp;
}
char stacktop()
{
return data[top];
}
int prod(char expr, char stp)
{
if(expr=='^' && stp=='^')
return 1;
else if((expr=='*' || expr=='/' || expr=='+' || expr=='-')
&& (stp=='^' || stp=='*' || stp=='/'))
return 1;
else
return 0;
}
void main()
{
int i,j;
char in[max],post[max],ch,st;
clrscr();
printf("Enter any infix expression :");
scanf("%s",&in);
j=0;
for(i=0;(ch=in[i])!='';i++)
{
if((ch>='a' && ch='A' && ch <= 'Z'))
post[j++]=ch;
else if(ch=='(')
push(ch);
else if(ch==')')
{
while(!empty() && (st=pop())!='(')
post[j++]=st;
}
else if(ch=='+' || ch=='*' || ch=='-'
|| ch=='/' || ch=='^' || ch=='%')
{
while(prod(ch,stacktop()))
post[j++]=pop();
push(ch);
}
}
while(!empty())
{
post[j++]=pop();
}
post[j]='';
printf("Infix expression is :%s",in);
printf("\nPostfix expression is:%s",post);
getch();
}
|
Output
Enter any infix expression : (A+B)^C-(D*E)/F
Infix expression is : (A+B)^C-(D*E)/F
Postfix expression is: AB+C^DE*F/-
|
|
10.Write program to evaluate post fix expression.
|
#include "stdio.h"
#include "conio.h"
#define max 30
int data[max],top=-1;
void push(char ch);
int pop();
void push(char ch)
{
if(top==(max-1))
printf("\n Stack overflow");
else
{
top++;
data[top]=ch;
}
}
int pop()
{
int item;
if(top==-1)
printf("\nStack is Empty");
else
{
item=data[top];
top=top-1;
}
return item;
}
void main()
{
char post[30];
int i,x,y,t,n,item;
clrscr();
printf("\nEnter any postfix expression:");
scanf("%s",&post);
for(i=0;(item=post[i])!='';i++)
{
if((item!='+') && (item!='-') && (item!='*') && (item!='/'))
{
n=item-'0';
push(n);
}
else
{
switch(item)
{
case '+': x=pop();
y=pop();
t=y+x;
push(t);
break;
case '-': x=pop();
y=pop();
t=y-x;
push(t);
break;
case '*': x=pop();
y=pop();
t=y*x;
push(t);
break;
case '/': x=pop();
y=pop();
t=y/x;
push(t);
break;
}
}
}
printf("\n postfix expression is:%d",pop());
getch();
}
|
Output
Enter any postfix expression:562+*84/-
postfix expression is:38
|
|
11.Implement Queue Operations Using Arrays.
|
#include "stdio.h"
#include "conio.h"
int queue[20],front=0,rear=0,item;
void enqueue();
void dequeue();
void display();
void enqueue()
{
if(rear==20)
{
printf("Queue is Full");
}
else if(rear==0 && front==0)
{
front=1;
}
printf("Enter element into the Queue:");
scanf("%d",&item);
rear++;
queue[rear]=item;
printf("Element Inserted:\n\n");
}
void dequeue()
{
if(front==0)
{
printf("Queue is empty:\n\n");
}
else
item=queue[front];
if(front==rear)
{
rear=0;
front=0;
}
else
{
front++;
printf("Element deleted:\n\n");
display();
}
}
void display()
{
int i;
if(front==0)
printf("Queue is empty:\n\n");
else
{
printf("Elements in the queue are:");
for(i=front;i<=rear;i++)
{
printf("%d\t",queue[i]);
}
}
}
void main()
{
int choice;
clrscr();
do
{
printf("\nQUEUE OPERATIONS");
printf("\n\n1.ENQUEUE");
printf("\n\n2.DEQUEUE");
printf("\n\n3.DISPLAY");
printf("\n\n4.EXIT");
printf("\nEnter ur Option:");
scanf("%d",&choice);
switch(choice)
{
case 1:enqueue();
break;
case 2:dequeue();
break;
case 3:display();
break;
case 4:exit();
default : printf("Invalid Option");
break;
}
}while(choice!=4);
getch();
}
|
Output
QUEUE OPERATIONS
1.ENQUEUE
2.DEQUEUE
3.DISPLAY
4.EXIT
Enter ur Option:1
Enter element into the Queue:10
Element Inserted
QUEUE OPERATIONS
1.ENQUEUE
2.DEQUEUE
3.DISPLAY
4.EXIT
Enter ur Option:1
Enter element into the Queue:20
Element Inserted
QUEUE OPERATIONS
1.ENQUEUE
2.DEQUEUE
3.DISPLAY
4.EXIT
Enter ur Option:1
Enter element into the Queue:30
Element Inserted
QUEUE OPERATIONS
1.ENQUEUE
2.DEQUEUE
3.DISPLAY
4.EXIT
Enter ur Option:3
Elements in the queue are:10 20 30
QUEUE OPERATIONS
1.ENQUEUE
2.DEQUEUE
3.DISPLAY
4.EXIT
Enter ur Option:2
Element deleted
QUEUE OPERATIONS
1.ENQUEUE
2.DEQUEUE
3.DISPLAY
4.EXIT
Enter ur Option:3
Elements in the queue are:20 30
QUEUE OPERATIONS
1.ENQUEUE
2.DEQUEUE
3.DISPLAY
4.EXIT
Enter ur Option:2
Element deleted
QUEUE OPERATIONS
1.ENQUEUE
2.DEQUEUE
3.DISPLAY
4.EXIT
Enter ur Option:3
Elements in the queue are:30
QUEUE OPERATIONS
1.ENQUEUE
2.DEQUEUE
3.DISPLAY
4.EXIT
Enter ur Option:2
QUEUE OPERATIONS
1.ENQUEUE
2.DEQUEUE
3.DISPLAY
4.EXIT
Enter ur Option:3
Queue is empty
QUEUE OPERATIONS
1.ENQUEUE
2.DEQUEUE
3.DISPLAY
4.EXIT
Enter ur Option:4
|
|
12.Implement the following operations on single linked list.
(i) Creation (ii) Insertion (iii) Deletion (iv) Display
|
#include "stdio.h"
#include "conio.h"
struct node
{
int data;
struct node *Link;
};
void insert(struct node *);
void del(struct node *);
void display(struct node *);
void main()
{
int ch;
struct node *header;
clrscr();
printf("\n Operations on Signle Linked List");
header=(struct node *)malloc(sizeof(struct node));
header->data=NULL;
header->Link=NULL;
do
{
printf("\n 1.Insert");
printf("\n 2.Delete");
printf("\n 3.Display");
printf("\n 4.Exit");
printf("\n Enter your Choice:");
scanf("%d",&ch);
switch(ch)
{
case 1: insert(header);
break;
case 2: del(header);
break;
case 3: display(header);
break;
case 4: break;
default: printf("\n Invalid Choice");
}
}while(ch!=4);
getch();
}
void display(struct node *header)
{
struct node *ptr;
ptr=header->Link;
if(ptr==NULL)
printf("\nList is empty");
else
printf("\nElements are:\n");
printf("\n");
while(ptr)
{
printf("\n%d",ptr->data);
ptr=ptr->Link;
}
}
void insert(struct node *header)
{
struct node *ptr,*ptr1,*node1,*last;
int ch,data,pos;
node1=(struct node*)malloc(sizeof(struct node));
printf("\nEnter the data:");
scanf("%d",&data);
node1->data=data;
node1->Link=NULL;
if(header->Link==NULL)
header->Link=node1;
else
{
printf("\n 1.Front \n 2.Middle \n 3.End \n 4.Exit");
printf("\n Enter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1: node1->Link=header->Link;
header->Link=node1;
break;
case 2: printf("\n Enter the key element to insert:");
scanf("%d",&pos);
ptr=header->Link;
while(ptr->data!=pos && ptr)
{
ptr=ptr->Link;
}
if(ptr)
{
node1->Link=ptr->Link;
ptr->Link=node1;
}
else
printf("Key is not in list");
break;
case 3: ptr=header->Link;
while(ptr->Link!=NULL)
ptr=ptr->Link;
ptr->Link=node1;
node1->Link=NULL;
break;
case 4: break;
}
}
}
void del(struct node *header)
{
struct node *ptr,*ptr1;
int ch,key;
printf("\n1.Front \n2.Middle\n3.End \n 4.Exit");
printf("\nEnter the position to delete:");
scanf("%d",&ch);
switch(ch)
{
case 1: ptr=header->Link;
if(ptr==NULL)
{
printf("\nList is Empty");
}
else
{
ptr1=ptr->Link;
header->Link=ptr1;
}
break;
case 2: printf("\nEnter the key value to be deleted");
scanf("%d",&key);
ptr1=header;
ptr=ptr1->Link;
while(ptr && ptr->data!=key)
{
ptr1=ptr;
ptr=ptr->Link;
}
if(ptr)
ptr1->Link=ptr->Link;
else
printf("\nNode with key does not exist");
break;
case 3: ptr1=header;
ptr=header->Link;
if(ptr==NULL)
printf("\nList is empty");
else
{
while(ptr->Link)
{
ptr1=ptr;
ptr=ptr->Link;
}
ptr1->Link=NULL;
}
break;
case 4: break;
}
}
|
Output
Operations on Signle Linked List
1.Insert
2.Delete
3.Display
4.Exit
Enter your Choice:1
Enter any Element:10
1.Insert
2.Delete
3.Display
4.Exit
Enter your Choice:1
Enter any Element:20
1.Front
2.Middle
3.End
4.Exit
Enter Position to Insert :3
1.Insert
2.Delete
3.Display
4.Exit
Enter your Choice:1
Enter any Element:30
1.Front
2.Middle
3.End
4.Exit
Enter Position to Insert :3
1.Insert
2.Delete
3.Display
4.Exit
Enter your Choice:1
Enter any Element:15
1.Front
2.Middle
3.End
4.Exit
Enter Position to Insert :2
Enter the key element to insert:10
1.Insert
2.Delete
3.Display
4.Exit
Enter your Choice:3
Elements are:
10
15
20
30
1.Insert
2.Delete
3.Display
4.Exit
Enter your Choice:2
1.Front
2.Middle
3.End
4.Exit
Enter the Position to Delete2
Enter the key value to be deleted15
1.Insert
2.Delete
3.Display
4.Exit
Enter your Choice:3
Elements are:
10
20
30
1.Insert
2.Delete
3.Display
4.Exit
Enter your Choice:2
1.Front
2.Middle
3.End
4.Exit
Enter the Position to Delete1
1.Insert
2.Delete
3.Display
4.Exit
Enter your Choice:3
Elements are:
20
30
1.Insert
2.Delete
3.Display
4.Exit
Enter your Choice:2
1.Front
2.Middle
3.End
4.Exit
Enter the Position to Delete3
1.Insert
2.Delete
3.Display
4.Exit
Enter your Choice:3
Elements are:
20
1.Insert
2.Delete
3.Display
4.Exit
Enter your Choice:2
1.Front
2.Middle
3.End
4.Exit
Enter the Position to Delete3
1.Insert
2.Delete
3.Display
4.Exit
Enter your Choice:3
List is empty
1.Insert
2.Delete
3.Display
4.Exit
Enter your Choice:4
|
|
13. Implement the following operations on double linked list.
(i) Creation (ii) Insertion (iii) Deletion (iv) Display
|
#include "stdio.h"
#include "conio.h"
struct node
{
int data;
struct node *Rlink;
struct node *Llink;
};
void insert(struct node*);
void delete(struct node*);
void display(struct node*);
void main()
{
int ch;
struct node *header;
clrscr();
header=(struct node*)malloc(sizeof(struct node));
header->data=NULL;
header->Rlink=NULL;
header->Llink=NULL;
do
{
printf("\nOperations on Double Linked List");
printf("\n1.Insert \n 2.Delete\n 3.Display\n 4.Exit\n");
printf("\nEnter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:insert(header);
break;
case 2:delete(header);
break;
case 3:display(header);
break;
case 4:break;
default:printf("Invalid option:");
break;
}
} while(ch!=4);
}
void insert(struct node *header)
{
struct node *ptr,*ptr1,*node1;
int ch,data,pos;
node1=(struct node*)malloc(sizeof(struct node));
if(node1==NULL)
{
printf("Out of memory!\n");
return;
}
printf("Enter the element:");
scanf("%d",&data);
node1->data=data;
node1->Llink=NULL;
node1->Rlink=NULL;
if(header->Rlink==NULL)
{
header->Rlink=node1;
node1->Llink=header;
}
else
{
printf("\n1.Front\n 2.Middle\n 3.End\n 4.Exit\n");
printf("Enter choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:ptr=header->Rlink;
node1->Llink=header;
header->Rlink=node1;
node1->Rlink=ptr;
ptr->Llink=node1;
break;
case 2: printf("\nEnter the Key element:");
scanf("%d",&pos);
ptr=header;
while(ptr->data!=pos && ptr->Rlink!=NULL)
ptr=ptr->Rlink;
if(ptr->data!=pos)
{
printf("Key not in list\n");
return;
}
node1->Llink=ptr;
node1->Rlink=ptr->Rlink;
ptr->Rlink=node1;
if(node1->Rlink!=NULL)
node1->Rlink->Llink=node1;
break;
case 3: ptr=header->Rlink;
while(ptr->Rlink!=NULL)
ptr=ptr->Rlink;
ptr->Rlink=node1;
node1->Llink=ptr;
break;
case 4 : break;
}
}
}
void display(struct node *header)
{
struct node *ptr;
ptr=header->Rlink;
if(ptr==NULL)
printf("'\nList is empty:");
else
{
printf("(L-R)Elements are: header");
while(ptr)
{
printf("%d",ptr->data);
ptr=ptr->Rlink;
}
printf("->NULL\n");
ptr=header->Rlink;
while(ptr->Rlink) /* ptr->Rlink != NULL */
ptr=ptr->Rlink;
printf("(R-L)Elements are: NULL->");
while(ptr!=header)
{
printf("%d",ptr->data);
ptr=ptr->Llink;
}
printf("header\n");
}
}
void delete(struct node *header)
{
struct node *ptr,*ptr1,*ptr2;
int ch,key;
if(header->Rlink==NULL)
{
printf("No elements to delete\n");
return;
}
printf("\n1.Front \n2.Middle\n3.End \n 4.Exit\n");
printf("\n Enter the position to Delete :");
scanf("%d",&ch);
switch(ch)
{
case 1: ptr=header->Rlink;
ptr1=ptr->Rlink;
header->Rlink=ptr1;
if(ptr1!=NULL)
ptr1->Llink=header;
break;
case 2: printf("\nEnter the key value to delete");
scanf("%d",&key);
ptr=header->Rlink;
while(ptr->data!=key && ptr->Rlink)
ptr=ptr->Rlink;
if(ptr->data==key)
{
ptr1=ptr->Llink;
ptr2=ptr->Rlink;
if(ptr2!=NULL)
ptr2->Llink=ptr1;
ptr1->Rlink=ptr2;
free(ptr);
}
else
printf("\nNode with key does not exist");
break;
case 3: ptr=header->Rlink;
while(ptr->Rlink)
ptr=ptr->Rlink;
ptr->Llink->Rlink=NULL;
free(ptr);
break;
case 4: break;
}
}
|
Output
Operations on Double Linked List
1.Insert
2.Delete
3.Display
4.Exit
Enter your choice:1
Enter the element:10
Operations on Double Linked List
1.Insert
2.Delete
3.Display
4.Exit
Enter your choice:1
Enter the element:20
1.Front
2.Middle
3.End
4.Exit
Enter choice:3
Operations on Double Linked List
1.Insert
2.Delete
3.Display
4.Exit
Enter your choice:1
Enter the element:30
1.Front
2.Middle
3.End
4.Exit
Enter choice:3
Operations on Double Linked List
1.Insert
2.Delete
3.Display
4.Exit
Enter your choice:1
Enter the element:15
1.Front
2.Middle
3.End
4.Exit
Enter choice:2
Enter the Key element:10
Operations on Double Linked List
1.Insert
2.Delete
3.Display
4.Exit
Enter your choice:3
(L-R)Elements are: header10152030->NULL
(R-L)Elements are: NULL->30201510header
Operations on Double Linked List
1.Insert
2.Delete
3.Display
4.Exit
Enter your choice:2
1.Front
2.Middle
3.End
4.Exit
Enter the position to Delete :1
Operations on Double Linked List
1.Insert
2.Delete
3.Display
4.Exit
Enter your choice:3
(L-R)Elements are: header152030->NULL
(R-L)Elements are: NULL->302015header
Operations on Double Linked List
1.Insert
2.Delete
3.Display
4.Exit
Enter your choice:2
1.Front
2.Middle
3.End
4.Exit
Enter the position to Delete :2
Enter the key value to delete20
Operations on Double Linked List
1.Insert
2.Delete
3.Display
4.Exit
Enter your choice:3
(L-R)Elements are: header1530->NULL
(R-L)Elements are: NULL->3015header
Operations on Double Linked List
1.Insert
2.Delete
3.Display
4.Exit
Enter your choice:2
1.Front
2.Middle
3.End
4.Exit
Enter the position to Delete :3
Operations on Double Linked List
1.Insert
2.Delete
3.Display
4.Exit
Enter your choice:3
(L-R)Elements are: header15->NULL
(R-L)Elements are: NULL->15header
Operations on Double Linked List
1.Insert
2.Delete
3.Display
4.Exit
Enter your choice:2
1.Front
2.Middle
3.End
4.Exit
Enter the position to Delete :1
Operations on Double Linked List
1.Insert
2.Delete
3.Display
4.Exit
Enter your choice:3
List is empty:
Operations on Double Linked List
1.Insert
2.Delete
3.Display
4.Exit
Enter your choice:4
|
|
14. Program to Implement stacks using linked list.
|
#include "stdio.h"
#include "conio.h"
struct node
{ int data;
struct node *link;
}*Top;
void PUSH(struct node *);
void POP(struct node *);
void Display(struct node *);
void PUSH(struct node *header)
{ struct node *node1;
int data;
printf("\nEnter the Element:");
scanf("%d",&data);
node1=(struct node *)malloc(sizeof(struct node));
node1->data=data;
node1->link=Top;
Top=node1;
header->link=Top;
}
void POP(struct node *header)
{ struct node *ptr;
int item;
if(Top==NULL)
{ printf("\nStack is Empty");
}
else
{ ptr=Top->link;
item=Top->data;
header->link=ptr;
Top=ptr;
}
printf("\nItem deleted from Stack :%d",item);
}
void display(struct node *header)
{ struct node *ptr;
ptr=header->link;
if(ptr==NULL)
printf("\nStack is empty");
else
{ printf("\nElements are:");
while(ptr)
{ printf("%d->",ptr->data);
ptr=ptr->link;
}
}
}
void main()
{ int data,ch,c;
struct node *header;
clrscr();
header=(struct node *)malloc(sizeof(struct node));
header->data=NULL;
header->link=NULL;
printf("\nStack Operations using SingleLinked List");
do{ printf("\n 1.PUSH");
printf("\n 2.POP");
printf("\n 3.Display");
printf("\n 4.Exit");
printf("\nEnter your choice:");
scanf("%d",&ch);
switch(ch)
{ case 1: PUSH(header);
break;
case 2: POP(header);
break;
case 3: display(header);
break;
case 4: break;
default: printf("\n Invalid choice");
}
}while(ch!=4);
}
|
Output
Stack Operations using SingleLinked List
1.PUSH
2.POP
3.Display
4.Exit
Enter your choice:1
Enter the Element:10
1.PUSH
2.POP
3.Display
4.Exit
Enter your choice:1
Enter the Element:20
1.PUSH
2.POP
3.Display
4.Exit
Enter your choice:1
Enter the Element:30
1.PUSH
2.POP
3.Display
4.Exit
Enter your choice:3
Elements are:30->20->10->
1.PUSH
2.POP
3.Display
4.Exit
Enter your choice:2
Item deleted from Stack :30
1.PUSH
2.POP
3.Display
4.Exit
Enter your choice:2
Item deleted from Stack :20
1.PUSH
2.POP
3.Display
4.Exit
Enter your choice:2
Item deleted from Stack :10
1.PUSH
2.POP
3.Display
4.Exit
Enter your choice:3
Stack is empty
1.PUSH
2.POP
3.Display
4.Exit
Enter your choice:4
|
|
15. Program to Implement Queues using linked list.
|
#include "stdio.h"
#include "conio.h"
struct node
{ int item;
struct node *next;
}*front,*rear,*p;
int h;
void insert();
void delete();
void display();
void main()
{ int ch;
rear=NULL;
front=NULL;
clrscr();
do {
printf("\nQueue Operations Using Linked List");
printf("\n 1.Insert");
printf("\n 2.Delete");
printf("\n 3.Display");
printf("\n 4.Exit");
printf("\nEnter your choice:");
scanf("%d",&ch);
switch(ch)
{ case 1: insert();
break;
case 2: delete();
break;
case 3: display();
break;
case 4: break;
}
}while(ch!=4);
}
void insert()
{ struct node *p;
int n;
printf("\nEnter the Element to Insert:");
scanf("%d",&n);
p=(struct node *)malloc(sizeof(struct node));
p->item=n;
p->next=NULL;
if(front==NULL)
{ front=p;
rear=p;
}
else
{ rear->next=p;
rear=p;
}
}
void delete()
{ struct node *p;
int n;
if(front==NULL)
printf("\nQueue is Empty");
else{ p=front;
front=front->next;
printf("\nItem deleted is %d",*p);
}
}
void display()
{ if(front==NULL)
printf("\n Queue is Empty");
else{p=front;
printf("Elements in Queue are :\n ");
while(p!=NULL)
{ printf("%d\t",p->item);
p=p->next;
}
}
}
|
Output
Queue Operations Using Linked List
1.Insert
2.Delete
3.Display
4.Exit
Enter your choice:1
Enter the Element to Insert:10
Queue Operations Using Linked List
1.Insert
2.Delete
3.Display
4.Exit
Enter your choice:1
Enter the Element to Insert:20
Queue Operations Using Linked List
1.Insert
2.Delete
3.Display
4.Exit
Enter your choice:1
Enter the Element to Insert:30
Queue Operations Using Linked List
1.Insert
2.Delete
3.Display
4.Exit
Enter your choice:3
Elements in Queue are :
10 20 30
Queue Operations Using Linked List
1.Insert
2.Delete
3.Display
4.Exit
Enter your choice:2
Item deleted is 20
Queue Operations Using Linked List
1.Insert
2.Delete
3.Display
4.Exit
Enter your choice:2
Item deleted is 30
Queue Operations Using Linked List
1.Insert
2.Delete
3.Display
4.Exit
Enter your choice:3
Queue is Empty
Queue Operations Using Linked List
1.Insert
2.Delete
3.Display
4.Exit
Enter your choice:4
|
|
16.Program to sort numbers using HeapSort
|
#include "stdio.h"
#include "conio.h"
void createheap();
void swap();
void main()
{ int i,j,n,a[20],temp,p,Lchild,Rchild;
clrscr();
printf("\nEnter size of Array:");
scanf("%d",&n);
printf("Enter the Elements into the Array:\n");
for(i=1;i<=n;i++)
scanf("%d",&a[i]);
printf("\nElements before sorting are :\n");
for(i=1;i<=n;i++)
printf("%d\t",a[i]);
printf("\nElements after sorting are :\n");
for(i=1;i1)
{ swap(&a[1],&a[i]);
i=i-1; j=1;
while(2*j<=i)
{ Lchild=2*j;
Rchild=2*j+1;
if(Lchild==i)
temp=Lchild;
else if(a[Lchild]a[temp])
break;
else
swap(&a[j],&a[temp]);
j*=2;
}
}
for(i=1;i0 && a[p]<a[i])
{ temp=a[i];
a[i]=a[p];
a[p]=temp;
i=p;
p=p/2;
}
}
void swap(int *a,int *b)
{ int temp=*a;
*a=*b;
*b=temp;
}
|
Output
Enter size of Array :5
Enter the elements into the Array:
78
12
95
42
65
Elements before sorting are:
78 12 95 42 65
Elements after sorting are:
12 42 65 78 95
|
|
17.Construct BST and implement traversing techniques recursively.
|
#include "stdio.h"
#include "conio.h"
struct node
{ int data;
struct node *Lchild,*Rchild;
};struct node *node,*root;
int item;
struct node *insert(int, struct node *);
void inorder(struct node *);
void preorder(struct node *);
void postorder(struct node *);
void main()
{ int ch;
clrscr();
do{
printf("\n 1.Insert\n 2.InOrder\n 3.PreOrder\n 4.PostOrder\n 5.EXIT");
printf("\n\nEnter your choice :");
scanf("%d",&ch);
switch(ch)
{ case 1: printf("\nEnter element to insert:");
scanf("%d",&item);
root=insert(item,root);
break;
case 2: printf("\n InOrder : ");
inorder(root);
break;
case 3: printf("\n PreOrder : ");
preorder(root);
break;
case 4: printf("\n PostOrder : ");
postorder(root);
break;
case 5: exit(0);
default:printf("\n Invalid Option");
break;
}
}while(ch!=5);
getch();
}
struct node *insert(int item, struct node *root)
{ if(item==root->data)
printf("\n Element already Exist");
else{ if(root==NULL)
{ root=(struct node *)malloc(sizeof(struct node));
if(root==NULL)
printf("\n NO Enough Memory");
else
{ root->data=item;
root->Lchild=root->Rchild=NULL;
return root;
}
}
else{
if(itemdata)
root->Lchild=insert(item,root->Lchild);
else if(item>root->data)
root->Rchild=insert(item,root->Rchild);
return root;
}
}
}
void inorder(struct node *root)
{ if(root!=NULL)
{ inorder(root->Lchild);
printf("%d\t", root->data);
inorder(root->Rchild);
}
}
void preorder(struct node *root)
{ if(root!=NULL)
{ printf("%d\t", root->data);
preorder(root->Lchild);
preorder(root->Rchild);
}
}
void postorder(struct node *root)
{ if(root!=NULL)
{ postorder(root->Lchild);
postorder(root->Rchild);
printf("%d\t", root->data);
}
}
|
Output
1.Insert
2.InOrder
3.PreOrder
4.PostOrder
5.EXIT
i) Enter your choice :1
Enter element to insert:10
ii) Enter your choice :1
Enter element to insert:8
iii) Enter your choice :1
Enter element to insert:15
iv) Enter your choice :2
InOrder : 8 10 15
v) Enter your choice :3
PreOrder : 10 8 15
vi) Enter your choice :4
PostOrder : 8 15 10
vii) Enter your choice :5
|
|
18.Program to implement Inorder traversal on BST Non recursively.
|
#include "stdio.h"
#include "conio.h"
struct node
{ int data;
struct node *Lchild,*Rchild;
};
struct node *node,*root,*st[20],*ptr;
int top=0;
struct node * insert(struct node *root,int item)
{ if(item==root->data)
printf("\nElement Alrady Existed");
else { if(root == NULL) {
root = (struct node *)malloc(sizeof(struct node));
root->data =item;
root->Lchild = root->Rchild = NULL;
return root;
}
else {
if(root->data Rchild = insert(root->Rchild,item);
else
root->Lchild = insert(root->Lchild,item);
return root;
}
}
}
void inorder(struct node *root)
{ ptr=root;
Printf(“\nInorder is :”);
while(ptr != NULL) {
st[++top] = ptr;
ptr = ptr->Lchild;
}
ptr= st[top--];
while(ptr!= NULL)
{ printf("%d\t",ptr->data);
if(ptr->Rchild!= NULL)
{ ptr=ptr->Rchild;
while(ptr!= NULL)
{ st[++top] =ptr;
ptr=ptr->Lchild;
}
}
ptr=st[top--];
}
}
void main()
{ int item,ch;
root=NULL;
clrscr();
do
{ printf("\n1.Insert\n2.Inorder\n3.Exit\n");
printf("Enter your choice :");
scanf("%d",&ch);
switch(ch)
{ case 1: printf("Enter item to insert :");
scanf("%d",&item);
root=insert(root,item);
break;
case 2: inorder(root);
break;
case 3: exit(0);
default: printf("\nInvalid option");
break;
}
}while(ch!=3);
getch();
}
|
Output
1.Insert
2.Inorder
3.Exit
Enter your choice :1
Enter item to insert :10
1.Insert
2.Inorder
3.Exit
Enter your choice :1
Enter item to insert :8
1.Insert
2.Inorder
3.Exit
Enter your choice :1
Enter item to insert :15
1.Insert
2.Inorder
3.Exit
Enter your choice :2
Inorder is :8 10 15
1.Insert
2.Inorder
3.Exit
Enter your choice :3
|
|
19. Program to implement Pre order traversal on BST Non recursively.
|
#include "stdio.h"
#include "conio.h"
struct node
{ int data;
struct node *Lchild,*Rchild;
};
struct node *node,*header,*st[20],*ptr;
int top=0;
struct node * insert(struct node *root,int item)
{ if(item==root->data)
printf("\nElement Alrady Existed");
else { if(root == NULL) {
root = (struct node *)malloc(sizeof(struct node));
root->data =item;
root->Lchild = root->Rchild = NULL;
return root;
}
else {
if(root->data Rchild = insert(root->Rchild,item);
else
root->Lchild = insert(root->Lchild,item);
return root;
}
}
}
void preorder(struct node *root)
{ st[0]=NULL;
ptr=root;
printf(“\nPreorder is : “);
while(ptr!= NULL)
{ printf("%d\t",ptr->data);
if(ptr->Rchild!= NULL)
st[++top] = ptr->Rchild;
if(ptr->Lchild!= NULL)
ptr= ptr->Lchild;
else
ptr=st[top--];
}
}
void main()
{ int item,ch;
header = NULL;
clrscr();
do
{ printf("\n1.Insert\n2.Preorder\n3.Exit\n");
printf("Enter your choice :");
scanf("%d",&ch);
switch(ch)
{ case 1: printf("Enter item to insert :");
scanf("%d",&item);
header=insert(header,item);
break;
case 2: preorder(header);
break;
case 3: exit(0);
default: printf("\nInvalid option");
break;
}
}while(ch!=3);
getch();
}
|
Output
1.Insert
2.Preorder
3.Exit
Enter your choice :1
Enter item to insert :10
1.Insert
2.Preorder
3.Exit
Enter your choice :1
Enter item to insert :8
1.Insert
2.Preorder
3.Exit
Enter your choice :1
Enter item to insert :15
1.Insert
2.Preorder
3.Exit
Enter your choice :2
Preorder is : 10 8 15
1.Insert
2.Preorder
3.Exit
Enter your choice :3
|
|
20. Program to implement Post order traversal on BST Non recursively.
|
#include "stdio.h"
#include "conio.h"
struct node
{ int data;
struct node *Lchild,*Rchild;
};
struct node *node,*root,*st[20],*ptr;
struct node * insert(struct node *root,int item)
{ if(item==root->data)
printf("\nElement Alrady Existed");
else { if(root == NULL) {
root = (struct node *)malloc(sizeof(struct node));
root->data =item;
root->Lchild = root->Rchild = NULL;
return root;
}
else {
if(root->data Rchild = insert(root->Rchild,item);
else
root->Lchild = insert(root->Lchild,item);
return root;
}
}
}
void postorder(struct node *root)
{ int top=-1,children[20],i;
printf("\nPostorder is: ");
for(i=0;iLchild;
}
if(st[top]->Rchild==NULL) {
ptr=st[top--];
printf("%d ",ptr->data);
children[top]--;
}
else
children[top]--;
while(children[top]==0 && top>-1) {
ptr=st[top--];
printf("%d ",ptr->data);
children[top]--;
}
ptr=st[top]->Rchild;
}while(top>-1);
}
Void main()
{ int item,ch;
root = NULL;
clrscr();
do
{ printf("\n1.Insert\n2.Postorder\n3.Exit\n");
printf("Enter your choice :");
scanf("%d",&ch);
switch(ch)
{ case 1: printf("Enter item to insert :");
scanf("%d",&item);
root=insert(root,item);
break;
case 2: postorder(root);
break;
case 3: exit(0);
default: printf("\nInvalid option");
break;
}
}while(ch!=3);
getch();
}
|
Output
1.Insert
2.Postorder
3.Exit
Enter your choice :1
Enter item to insert :10
1.Insert
2.Postorder
3.Exit
Enter your choice :1
Enter item to insert :8
1.Insert
2.Postorder
3.Exit
Enter your choice :1
Enter item to insert :15
1.Insert
2.Postorder
3.Exit
Enter your choice :2
Postorder is : 8 15 10
1.Insert
2.Postorder
3.Exit
Enter your choice :3
|
NOTE: if u find mistakes in above programs please bring it to my notice(make a comment) so that i can modify them as early as possible...
Comments
Post a Comment