Implementation of Queue Using Array
Implementation of Queue Using Array
Program:
#include<stdio.h>
#include<conio.h>
#define null 0
void main()
{
int a;
void insert();
void deletion();
void disp();
clrscr();
printf("\n\t Implementation of Queue using Array\n");
do
{
printf("\n 1.Insertion");
printf("\n 2.Deletion");
printf("\n 3.Display");
printf("\n 4.Exit");
printf("\n Enter your choice: ");
scanf("%d",&a);
switch(a)
{
case 1:
insert();
break;
case 2:
deletion();
break;
case 3:
disp();
break;
case 4:
exit(0);
break;
default:
printf("\n Invalid choice........Try again");
break;
}
}
while(a<4);
getch();
}
int q[10],n=5,f=0,r=0;
void insert()
{
int b;
if(r>=n)
{
printf("\n Queue Overflow.........");
return;
}
else
{
printf("\n Enter the element: ");
scanf("%d",&b);
q[r]=b;
r++;
printf("\n The inserted element is %d",b);
f=1;
return;
}
}
void deletion()
{
int b;
if(f==0)
{
printf("\n Queue Underflow....");
return;
}
else
{
b=q[f];
printf("\n The deleted element is %d",b);
f++;
r--;
return;
}
}
void disp()
{
int i;
if(r==0)
{
printf("\n Queue is empty.....");
return;
}
else
{
for(i=0;i<r;i++)
printf("\n The elements present in the queue are........%d",q[i]);}}
Output:
1.Insertion
2.Deletion
3.Display
4.Exit
Enter your choice: 1
1.Insertion
2.Deletion
3.Display
4.Exit
Enter your choice: 4
Implementation of Queue using Linkedlist
Program:
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
#define null 0
void main()
{
int ch;
void insertion();
void deletion();
void display();
clrscr();
printf("\n\t Implementation of Queue using Linkedlist");
do
{
printf("\n1.Insertion\n2.Deletion\n3.Display\n4.Exit");
printf("\nEnter your choice: ");
scanf("%d",&ch);
switch(ch)
{
case 1:
insertion();
break;
case 2:
deletion();
break;
case 3:
display();
break;
case 4:
exit(0);
break;
default:
printf("\n Invalid choice........Try again");
break;
}
}
while(ch<4);
getch();
}
struct node
{
int elt;
struct node*next;
}*front=null,*rear=null;
void insertion()
{
int x;
struct node*newnode;
newnode=malloc(sizeof(struct node));
printf("\n Enter the element to insert: ");
scanf("%d",&x);
newnode->elt=x;
newnode->next=null;
if(rear==null)
{
front=newnode;
rear=newnode;
}
else
{
rear->next=newnode;
rear=newnode;
}
printf("\n The inserted element is %d",newnode->elt);
}
void deletion()
{
struct node*temp;
if(front==null)
{
printf("\n Queue is empty");
return;
}
temp=front;
if(front==rear)
front=rear=null;
else
front=front->next;
printf("\n Deleted element is %d",temp->elt);
free(temp);
}
void display()
{
struct node*temp;
if(front==null)
{
printf("\n Queue is empty");
return;
}
temp=front;
printf("\n The elements present in the queue are........");
while(temp!=null)
{
printf("%d\t",temp->elt);
temp=temp->next;
}}
Output :
Deleted element is 55
1.Insertion
2.Deletion
3.Display
4.Exit
Enter your choice: 3
Program:
#include<stdio.h>
#include<conio.h>
#include<string.h>
int top=-1;
char stack[20];
void main()
{
char expr[20],ch;
int len,i;
void push(char);
void pop();
clrscr();
printf("\n\t Balancing Paranthesis using Array implementation");
printf("\nEnter the Expression: ");
scanf("%s",expr);
len=strlen(expr);
for(i=0;i<len;i++)
{
if(expr[i]=='(')
push(expr[i]);
if(expr[i]==')')
pop();
}
if(top==-1)
printf("\n Balanced Expression");
else
printf("\nImbalanced right paranthesis");
getch();
}
void push(char item)
{
stack[top++]=item;
}
void pop()
{
if(top==-1)
{
printf("\nImbalanced left paranthesis");
getch();
}
else
top--;
}
Output:
Balanced Expression
Program:
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
#define null 0
struct node
{
int data,top;
struct node*next;
}
*top=null;
void main()
{
char expr[20],ch;
int len,i;
void push(char);
void pop();
clrscr();
printf("\n\t Balancing Paranthesis using Linkedlist implementation");
printf("\nEnter the Expression: ");
scanf("%s",expr);
len=strlen(expr);
for(i=0;i<len;i++)
{
if(expr[i]=='(')
push(expr[i]);
if(expr[i]==')')
pop();
}
if(top==null)
printf("\n Balanced Expression");
else
printf("\nImbalanced right paranthesis");
getch();
}
Output:
Balanced Expression
Program:
#include<stdio.h>
#include<conio.h>
int top=0,stack[20];
void main()
{
char expr[20];
int x,len,a,b,c,i;
void push(int);
int pop();
clrscr();
printf(“\n Postfix Evaluation Using Array Implementation”);
printf("\n Enter the expression: ");
scanf("%s",expr);
len=strlen(expr);
for(i=0;i<len;i++)
{
if(expr[i]=='+'||expr[i]=='-'||expr[i]=='*'||expr[i]=='/')
{
b=pop();
a=pop();
switch(expr[i])
{
case '+':
c=a+b;
push(c);
break;
case '-':
c=a-b;
push(c);
break;
case '*':
c=a*b;
push(c);
break;
case '/':
c=a/b;
break;
}
}
else
{
printf("\n Enter the value of %c ",expr[i]);
scanf("%d",&x);
push(x);
}
}
printf("\n The Result is: %d",stack[top]);
getch();
}
Output:
Program:
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
#include<stdlib.h>
#define null 0
void main()
{
char expr[20];
int x,top,len,a,b,c,i;
void push(int);
int pop();
clrscr();
printf(“\n Postfix Evaluation Using Linkedlist Implementation”);
printf("\n Enter the expression: ");
scanf("%s",expr);
len=strlen(expr);
for(i=0;i<len;i++)
{
if(expr[i]=='+'||expr[i]=='-'||expr[i]=='*'||expr[i]=='/')
{
b=pop();
a=pop();
switch(expr[i])
{
case '+':
c=a+b;
push(c);
break;
case '-':
c=a-b;
push(c);
break;
case '*':
c=a*b;
push(c);
break;
case '/':
c=a/b;
break;
}
}
else
{
printf("\n Enter the value of %c ",expr[i]);
scanf("%d",&x);
push(x);
}}
printf("the result is: ");
printf("%d",top->data);
}
struct node
{
int data;
struct node*next;
}*top=null;
void push(int item)
{
struct node*newnode;
newnode=malloc(sizeof(struct node));
newnode->data=item;
if(top==null)
{
newnode->next=null;
top=newnode;
}
else
{
newnode->next=top;
top=newnode;
}}
int pop()
{
int staval;
struct node*temp;
temp=top;
staval=temp->data;
top=top->next;
free(temp);
return staval;
}
Output:
Program:
#include<stdio.h>
#include<conio.h>
int a[20],n;
void main()
{
int i;
void qsort(int,int);
clrscr();
printf("\n QUICK SORT");
printf("\n Enter the array size: ");
scanf("%d",&n);
printf("\n Enter the elements to be sorted: ");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
qsort(0,n-1);
printf("\n The sorted elements are: ");
for(i=0;i<n;i++)
printf("%d\t",a[i]);
getch();
}
void qsort(int left,int right)
{
int i,j,pivot,temp,k;
if(left<right)
{
i=left+1;
j=right;
pivot=left;
while(i<j)
{
while(a[pivot]>=a[i])
i=i+1;
while(a[pivot]<a[j])
j=j-1;
if(i<j)
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
temp=a[pivot];
a[pivot]=a[j];
a[j]=temp;
qsort(left,j-1);
qsort(j+1,right);}}
Output:
QUICK SORT
QUICK SORT
Program:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void heapsort(int a[],int n);
void delmax(int a[],int,int);
int a[20];
void main()
{
int n,i;
clrscr();
printf("\n HEAP SORT\n");
printf("\n Enter the array size: ");
scanf("%d",&n);
printf("\n Enter the elements of array: ");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
heapsort(a,n);
printf("\n The sorted elements are.........");
for(i=0;i<n;i++)
printf("%d",a[i]);
getch();
}
void heapsort(int a[],int n)
{
int i,temp;
for(i=(n/2)-1;i>=0;i--)
delmax(a,i,n);
for(i=n-1;i>=1;i--)
{
temp=a[0];
a[0]=a[i];
a[i]=temp;
delmax(a,0,i-1);
}
}
void delmax(int a[],int root,int bottom)
{
int maxch,temp;
if(root*2<=bottom)
{
if(root*2==bottom)
maxch=root*2;
else if(a[root*2]>a[root*2+1])
maxch=root*2;
else
maxch=(root*2)+1;
if(a[root]<a[maxch])
{
temp=a[root];
a[root]=a[maxch];
a[maxch]=temp;
root=maxch;
}
}
}
Output:
HEAP SORT
HEAP SORT
Program:
#include<stdio.h>
#include<conio.h>
#define size 5
int stack[size],top=-1;
void main()
{
int choice;
void push():
void pop();
void disp();
int isempty();
int length();
clrscr();
do
{
printf(“\n1.Push\n2. Pop\n3. Display\n4. Length\n5. Exit\n”);
printf(“\nEnter your choice: “);
scanf(“%d”,&choice);
switch(choice)
{
case 1:
push();
break;
case 2:
pop();
break;
case 3:
disp();
break;
case 4:
printf(“\n No of elements in stack is %d”,length(1));
break;
case 5:
exit(0);
break;
default:
printf(“\nInvalid choice!!!!!!!!”);
}
}
while(choice!=5);
getch();
}
void push()
{
int no;
if(top==size-1)
{
printf(“\nStack if full”);
}
else
{
printf(“\n Enter the number: ”);
scanf(“%d”,&no);
top++;
stack[top]=no;
}
}
void pop()
{
int no;
if(isempty())
{
printf(“\nStack is empty”);
top=-1;
}
else
{
no=stack[top];
printf(“\n %d is popped from the stack”,no);
--top;
}
}
void disp()
{
int I,temp=top;
if(isempty())
{
printf(“\nStack is empty”);
return;
}
printf(“\nElements in the stack…”);
for(i=temp;i>=0;i--)
{
printf(“%d”,stack[i]);
}}
int isempty()
{
return(top==-1);
}
int length()
{
return(top+1);
}
Output:
1. Push
2. Pop
3. Display
4. Length
5. Exit
1. Push
2. Pop
3. Display
4. Length
5. Exit
1. Push
2. Pop
3. Display
4. Length
5. Exit
1. Push
2. Pop
3. Display
4. Length
5. Exit
1. Push
2. Pop
3. Display
4. Length
5. Exit
1. Push
2. Pop
3. Display
4. Length
5. Exit
1. Push
2. Pop
3. Display
4. Length
5. Exit
1. Push
2. Pop
3. Display
4. Length
5. Exit
1. Push
2. Pop
3. Display
4. Length
5. Exit
1. Push
2. Pop
3. Display
4. Length
5. Exit
1. Push
2. Pop
3. Display
4. Length
5. Exit
1. Push
2. Pop
3. Display
4. Length
5. Exit
1. Push
2. Pop
3. Display
4. Length
5. Exit
1. Push
2. Pop
3. Display
4. Length
5. Exit
Program:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct stack
{
int no;
struct stack *next;
};
struct stack *p,*q,*temp,*head=NULL;
void main()
{
void push();
void pop();
void disp();
int choice;
clrscr();
do
{
printf(“\n1.Push\n2. Pop\n3. Display\n4. Exit\n”);
printf(“\nEnter your choice: “);
scanf(“%d”,&choice);
switch(choice)
{
case 1:
push();
break;
case 2:
pop();
break;
case 3:
disp();
break;
case 4:
exit(0);
break;
default:
printf(“\nInvalid choice!!!!!!!!”);
}
}
while(choice!=4);
getch();
}
void push()
{
temp=(struct stack*)malloc(sizeof(struct stack));
printf(“\nEnter the no:”);
scanf(“%d”,&temp->no);
if(head==NULL)
{
head=temp;
temp->next=NULL;
}
else
(
temp->next=head;
head=temp;
]
printf(“\nElement %d is pushed into the stack”,temp->no);
}
void pop()
{
p=head;
if(head==NULL)
{
printf(“Stack is empty”);
}
else
{
p=head;
head=head->next;
printf(“\n Element %d is popped from the stack”,p->no);
free(p);
}
}
void disp()
{
if(head==NULL)
printf(“Stack is empty”);
else
{
p=head;
printf(“\nElement is present in the stack”);
while(p!=NULL)
{
printf(“%d”,p->no);
}
}
}
Output:
1. Push
2. Pop
3. Display
4. Exit
1. Push
2. Pop
3. Display
4. Exit
1. Push
2. Pop
3. Display
4. Exit
1. Push
2. Pop
3. Display
4. Exit
1. Push
2. Pop
3. Display
4. Exit
1. Push
2. Pop
3. Display
4. Exit
1. Push
2. Pop
3. Display
4. Exit
Program:
#include<stdio.h>
#include<conio.h>
struct emp
{
char name[20];
int empno;
int salary;
};
struct emp empinfo[10];
void main()
{
int choice;
void getdetails();
void search();
void disp();
clrscr();
do
{
printf(“\n1.Getdetails\n2. Search\n3. Display\n4. Exit\n”);
printf(“\nEnter your choice: “);
scanf(“%d”,&choice);
switch(choice)
{
case 1:
getdetails();
break;
case 2:
search();
break;
case 3:
disp();
break;
case 4:
exit(0);
break;
default:
printf(“\nInvalid choice!!!!!!!!”);
}
}
while(choice!=4);
getch();}
void getdetails()
{
int i;
printf(“\n Enter the details:”);
for(i=0;i<3;i++)
{
printf(“\n Enter the Employee number:”);
scanf(“%d”,&empinfo[i].empno);
printf(“\n Enter the Employee name:”);
scanf(“%d”,&empinfo[i].name);
printf(“\n Enter the Employee salary:”);
scanf(“%d”,&empinfo[i].salary);
}
}
void disp()
{
int i;
printf(“\n Employee details……”);
for(i=0;i<3;i++)
{
printf(“\nEmployee Name: %s”,empinfo[i].name);
printf(“\nEmployee Number:%d”,empinfo[i].empno);
printf(“\nEmployee Salary: %d”,empinfo[i].salary);
}
}
void search()
{
int i,id,position;
printf(“\n Enter the employee no Who’s detail was to be found?....”);
scanf(“%d”,&id);
for(i=0;i<3;i++)
{
if(id==empinfo[i].empno)
{
printf(“\nEmployee Name: %s”,empinfo[i].name);
printf(“\nEmployee Number:%d”,empinfo[i].empno);
printf(“\nEmployee Salary: %d”,empinfo[i].salary);
}
}
}
Output:
1. Getdetails
2. Search
3. Display
4. Exit
1. Getdetails
2. Search
3. Display
4. Exit
1. Getdetails
2. Search
3. Display
4. Exit
Program:
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
#include<stdlib.h>
void main()
{
int data,ch;
void insert(int x);
void deletion(int x);
void disp();
clrscr();
do
{
printf(“\n1.Insertion\n2. Deletion\n3. Display\n4. Exit\n”);
printf(“\nEnter your choice: “);
scanf(“%d”,&choice);
switch(choice)
{
case 1:
printf(“\n Enter the element to insert: ”);
scanf(“%d”,&data);
insert(data);
break;
case 2:
printf(“\n Enter the element to delete: ”);
scanf(“%d”,&data);
deletion(data);
break;
case 3:
disp();
break;
case 4:
exit(0);
break;
default:
printf(“\nInvalid choice!!!!!!!!”);
}
}
while(choice<4);
getch();
}
struct node()
{
int ele;
struct node *next;
}
*list=NULL,*p;
void insert(int x)
{
struct node *newnode;
int pos;
newnode=malloc(sizeof(struct node));
newnode->ele=x;
if(list->next==NULL)
{
list->next=newnode;
newnode->next=NULL;
}
else
{
printf(“\n Enter the value after which the element to be inserted:”);
scanf(“%d”,&pos);
p=find(pos);
newnode->next=p->next;
p->next-newnode;
}
}
if(p->next!=NULL)
{
temp=p->next;
p->next=temp->next;
printf(“\n The deleted element is %d”,temp->ele);
free(temp);
}
else
{
pritnf(“\n Element is not found in the list….”);
}
}
void disp()
{
if(list->next==NULL)
printf(“\n List is empty”);
else
{
p=list->next;
printf(“\n The contents of the list are….”);
while(p!=NULL)
{
printf(“%d->”,p->ele);
p=p->next;
}
}
}
Output:
1. Insertion
2. Deletion
3. Display
4. Exit
1. Insertion
2. Deletion
3. Display
4. Exit
1. Insertion
2. Deletion
3. Display
4. Exit
1. Insertion
2. Deletion
3. Display
4. Exit
1. Insertion
2. Deletion
3. Display
4. Exit
1. Insertion
2. Deletion
3. Display
4. Exit
1. Insertion
2. Deletion
3. Display
4. Exit
1. Insertion
2. Deletion
3. Display
4. Exit
1. Insertion
2. Deletion
3. Display
4. Exit
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int flag=0,a[20],n,pos,i,item,low,mid,high;
void sort(int[],int);
clrscr();
while(low<=high)
{
mid=(low+high)/2;
if(item==a[mid])
{
flag=1;
break;
}
else
if(item>a[mid])
low=mid+1;
else
high=mid-1;
}
if(flag==0)
printf(“\n The element %d is not present in the tree:”,item);
else
printf(“\n The element %d is present in the tree”,item);
getch();
}
void sort(int a[],int n)
{
int i,j,temp;
for(i=0;i<n-1;i++)
for(j=i+1;j<=n;j++)
{
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
Output: