EX - NO: Sivalingam.A 10MCA38 Program For Stack Without Using Functon
EX - NO: Sivalingam.A 10MCA38 Program For Stack Without Using Functon
EX - NO: Sivalingam.A 10MCA38 Program For Stack Without Using Functon
A
10MCA38
Program for Stack without using functon
#include<stdio.h>
#include<conio.h>
#include<process.h>
void main()
{
int s[5],i,n,j,top;
clrscr();
top=-1;
for(;;)
{
printf("\nenter your choice");
printf("\n1. for insertion");
printf("\n2. for deletion");
printf("\n3. for view top element");
printf("\n4. for view all element\n");
scanf("%d",&n);
switch(n)
{
case 1:
{
if(top==4)
{
printf("stack is full");
}
else
{
printf("Enter the value for insertion");
scanf("%d",&i);
top=top+1;
s[top]=i;
}
}
break;
case 2:
{
if(top==-1)
{
printf("stack is empty");
}
else
{
i=s[top];
top=top-1;
printf("the removed element is %d",i);
}
}
break;
case 3:
{
if(top==-1)
{
printf("stack is empty");
}
else
{
i=s[top];
printf("top element is %d",i);
}
}
break;
case 4:
if(top==-1)
{
printf("stack is empty");
}
else
{
for(j=0;j<=top;j++)
{
printf(" %d \n",s[j]);
}
}
break;
default:
exit(0);
}
}
getch();
}
Output:
1
Enter the value for insertion 43
#include<stdio.h>
#include<conio.h>
void push(int *,int *);
void pop(int *,int *);
void disp(int *,int *);
void main()
{
int top,st[10];
int s;
top=-1;
clrscr();
for(;;)
{
printf("\n1.insert\n");
printf("2.delete\n");
printf("3.display\n");
printf("4,exit\n");
printf("\nEnter your choice:\t");
scanf("%d",&s);
switch(s)
{
case 1:
push(&top,&st[0]);
break;
case 2:
pop(&top,&st[0]);
break;
case 3:
disp(&top,&st[0]);
break;
default:
exit(0);
break;
}
}
}
void push(int *top,int *st)
{
int x=0;
if(*top==9)
{
printf("\nstack is full");
}
else
{
printf("\nenter the element for insertion:\t");
scanf("%d",&x);
*top=*top+1;
st[*top]=x;
printf("\nThe add element is:%d",x);
}
}
void pop(int *top,int *st)
{
int x=0;
if(*top==-1)
{
printf("\nstack empty");
}
else
{
x=st[*top];
*top=*top-1;
printf("\nThe deleted element is:%d",x);
}
}
void disp(int *top,int *st)
{
if(*top==-1)
{
printf("stack is under flow\n");
}
else
{
printf("\nThe top element is:%d",st[*top]);
}
}
Output:
1.insert
2.delete
3.display
4,exit
#include<stdio.h>
#include<conio.h>
#include<process.h>
void main()
{
int a[10],f,r,s,i,x;
clrscr();
f=-1;
r=-1;
for(;;)
{
printf("\nenter your choice");
printf("\npress 1 for insertion");
printf("\npress 2 for deletion");
printf("\npress 3 over flow or not");
printf("\npress 4 under flow or not");
printf("\npress 5 for exit");
scanf("%d",&s);
switch(s)
{
case 1:
if(r==10)
{
printf("\nthe queue is full");
}
else
{
r=r+1;
printf("\nEnter the value for insertion");
scanf("%d",&x);
a[r]=x;
}
break;
case 2:
if(r<=f)
{
printf("\nthe queue is empty");
}
else
{
f=f+1;
x=a[f];
printf("\nthe removed element is %d ",x);
}
break;
case 3:
if(r==10)
{
printf("the queue is over flow");
}
else
{
printf("the queue is not over flow");
}
break;
case 4:
if(f==r)
{
printf("the queue is under flow");
}
else
{
printf("the queue is not under flow");
}
break;
default:
exit(0);
break;
}
}
getch();
}
Output:
Output:
1
Enter the value for insertion52
front element is 78
Rear element is 52
Enter your choice
2
Removed element is 78
Enter your choice
5
front element is 52
Rear element is 52
EX.NO: SIVALINGAM.A
10MCA38
Program for Circular queue without using function
#include<stdio.h>
#include<conio.h>
void main()
{
int cq[10],n,temp,i,front=0,rear=0,ch;
clrscr();
printf("Enter the size of the Queue:");
scanf("%d",&n);
do
{
printf("\nCircular Queue Implementation");
printf("\n1.Push\n2.Pop\n3.Display\n4.Exit");
printf("\nEnter Ur Option : ");
scanf("%d",&ch);
switch(ch)
{
case 1:
if(((rear+1)%n)!=front)
{
printf("\nEnter Element: " );
rear=(rear+1)%n;
scanf("%d",&cq[rear]);
printf("\nYour Element to be Added");
}
else
{
printf("Queue Is Full");
}
break;
case 2:
if(front==rear)
{
printf("Queue is empty");
}
else
{
front=(front+1)%n;
printf("The Deleted Element Is : %d",cq[front]);
}
break;
case 3:
if(front==rear)
{
printf("Queue Is Empty");
}
else
{
printf("Element In Queue : ");
temp=front;
while(temp!=rear)
{
temp=(temp+1)%n;
printf("\n\t%d",cq[temp]);
}
}
break;
case 4:
exit(0);
break;
default:
printf("\nPlease Enter Your Choice 1 to 4 ");
}
}while(ch!=4);
getch();
}
Output:
Enter the size of the Queue:5
Enter Element: 78
Enter Element: 46
Enter Ur Option : 3
Elements in queue:
78
46
85
Enter Ur Option : 2
The deleted element is: 78
Enter Ur Option : 3
Elements in queue:
45
85
EX.NO: SIVALINGAM.A
10MCA38
Program for Circular queue with using function:
#include<stdio.h>
#include<conio.h>
int rear(int *,int, int);
int front(int *,int, int);
void disp(int *,int, int);
void main()
{
int c,a[5],r,f;
r=0;
f=0;
clrscr();
for(;;)
{
Printf(“\n1 for insertion”);
Printf(“\n2 foe deletion”);
Printf(“\n3 for display”);
printf("\nenter your choice\t");
scanf("%d",&c);
switch(c)
{
case 1:
r=rear(&a[0],r,f);
break;
case 2:
f=front(&a[0],r,f);
break;
case 3:
disp(&a[0],r,f);
break;
case 4:
exit(0);
break;
}
}
}
int rear(int *a,int r,int f)
{
int x;
if((r+1)==5)
{
r=0;
}
else
{
r=r+1;
}
if(r==f)
{
printf("\nqueue is full\n");
return r;
}
else
{
printf("insert your element\n");
scanf("%d",&x);
a[r]=x;
printf("inserted element is:%d\n",a[r]);
return r;
}
}
int front(int *a,int r,int f)
{
int x;
if(r==f)
{
printf("queue is empty\n");
return f;
}
else
{
if((f+1)==5)
{
f=0;
}
else
{
f=f+1;
}
x=a[f];
printf("the deleted element is:%d\n",x);
return f;
}
}
void disp(int *a,int r,int f)
{
while(f!=r)
{
f=f+1;
if(f==5)
{
f=0;
}
else
{
printf("the element:%d\n",,a[f]);
}
}
}
Output:
1 for insertion
2 for deletion
3 for display
enter your choice 1
insert your element
78
inserted element is:78
EX.NO: SIVALINGAM.A
10MCA38
program for Link list
#include<stdio.h>
#include<conio.h>
#include<process.h>
#include<malloc.h>
typedef struct link
{
int data;
struct link *next;
};
struct link *newlink,*t,*head=NULL,*x;
void create();
void insert();
void del();
void erase();
void display();
void main()
{
int ch;
clrscr();
for(;;)
{
printf("\n\nenter your choice\n");
printf("1 create list\n");
printf("2 insert list\n");
printf("3 delete element\n");
printf("4 delete list\n");
printf("5 display element\n");
scanf("%d",&ch);
switch(ch)
{
case 1:
create();
break;
case 2:
insert();
break;
case 3:
del();
break;
case 4:
erase();
break;
case 5:
display();
break;
default:
printf("\ninput only numbers 1-5");
exit(0);
break;
}
}
}
void create()
{
{
newlink=malloc(sizeof(struct link));
printf("\nenter the data\n");
scanf("%d",&newlink->data);
newlink->next=NULL;
if(head==NULL)
{
head=newlink;
}
else
{
for(t=head;t->next!=NULL;t=t->next)
t->next=newlink;
}
}
}
void insert()
{
int i,pos;
newlink=malloc(sizeof(struct link));
printf("\nenter the data for insertion\n");
scanf("%d",&newlink->data);
printf("enter the position to be inserted\n");
scanf("%d",&pos);
if(pos==1)
{
newlink->next=head;
head=newlink;
}
for(t=head,i=1;t!=NULL;t=t->next,i++)
{
if(i==pos-1)
{
newlink->next=t->next;
t->next=newlink;
}
}
}
void del()
{
int pos,i;
printf("\nenter the position to be deleted\n");
scanf("%d",&pos);
for(t=head,i=1;t!=NULL;t=t->next,i++)
{
if(pos==1)
{
printf("\nDeleted element is %d",t->data);
t=t->next;
head=t;
break;
}
if(i==pos-1)
{
x=t->next;
printf("\nDeleted elememt is %d",t->next->data);
t->next=t->next->next;
free(x);
break;
}
}
}
void erase()
{
head=NULL;
printf("\nAll elements are deleted from the list");
}
void display()
{
if(head==NULL)
{
printf("\nthe list is empty\n");
}
else
{
for(t=head;t!=NULL;t=t->next)
{
printf("\n\t%d",t->data);
}
}
}
Output:
2
enter your choice
2
1
enter your choice
5
85
78
45
enter your choice
3
Enter the position to de deleted
2
Deleted element is 78
enter your choice
5
85
45
EX.NO: SIVALINGAM.A
10MCA38
Program for Singly circular linked list
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include "malloc.h"
void main()
{
node1 *n1,*head,*t,*t1;
int x=0,f,c,count;
head= NULL;
clrscr();
for(;;)
{
printf("1.insertion\n");
printf("2.deletion\n");
printf("3.display\n");
printf("4.exit\n");
printf("enter your choice:");
scanf("%d",&f);
switch(f)
{
case 1:
for(; ;)
{
printf("Give the value for x (*** x=1 for exit ***)\n");
scanf("%d",&x);
if(x==1)
{
break;
}
n1=(node1*)malloc(sizeof(struct node));
n1->data =x;
printf("Give the place for insertion \n");
scanf("%d",&count);
if(count==1)
{
printf("First element insertion\n");
if(head==NULL)
{
n1->link=n1;
head=n1;
}
else
{
t=head;
t1=t;
for(;t->link!=head;)
{
t=t->link;
}
n1->data=x;
n1->link=t1;
t->link=n1;
head=n1;
}
}
else
{
printf("%d th node insertion\n",count);
t=head;
for(c=1;c<count-1;c++)
{
t=t->link;
}
n1->link = t->link ;
t->link = n1;
}
break;
case 2:
for(;;)
{
printf("get the position for deletion:");
scanf("%d",&x);
if(x==0)
{
break;
}
else if(x==1)
{
printf("first node deletion\n");
t=head;
t1=t->link;
for(;t->link!=head;)
{
t=t->link;
}
head=t1;
t->link=head;
}
else
{
printf("%d th node deletion\n",x);
t=head;
for(c=1;c<x;c++)
{
t1=t;
t=t->link;
}
t1->link=t->link;
}
}
break;
case 3:
t=head;
for(;t->link!=head;t=t->link)
{
printf(" %d \n",t->data);
}
printf("%d\n",t->data);
break;
case 4:
exit(0);
break;
}
}
getch(); }
Output:
1.insertion
2.deletion
3.display
4.exit
enter your choice:1
Give the value for insertion
45
Give the place for insertion
1
First element insertion
enter your choice:1
Give the value for insertion
78
Give the place for insertion
1
First element insertion
enter your choice:3
78
45
Enter your choice: 2
Enter the position for deletion
2
2 th node deletion
Enter your choice:3
78
EX.NO: SIVALINGAM.A
10MCA38
Program for Doubly linked list
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<malloc.h>
typedef struct node
{
int data;
struct node *plink;
struct node *nlink;
}node1;
void main()
{
node1 *n1,*head,*t,*t1;
int x=0,f,c,count;
head= NULL;
clrscr();
for(;;)
{
printf("\n1.insertion\n");
printf("2.deletion\n");
printf("3.display\n");
printf("enter your choice:");
scanf("%d",&f);
switch(f)
{
case 1:
{
printf("Give the value for insertion\n");
scanf("%d",&x);
n1=(node1*)malloc(sizeof(struct node));
n1->data =x;
printf("Give the place for insertion \n");
scanf("%d",&count);
if(count==1)
{
printf("First element insertion\n");
if(head==NULL)
{
n1->plink= head;
n1->nlink= head;
head=n1;
}
else
{
n1->plink=NULL;
n1->nlink=head;
head->plink=n1;
head=n1;
}
}
else
{
printf("%d th node insertion\n",count);
t=head;
for(c=1;c<count-1;c++)
{
t=t->nlink;
}
n1->plink = t;
n1->nlink =t->nlink;
if(t->nlink!=NULL)
{
t->nlink->plink = n1;
}
t->nlink=n1;
}
}
break;
case 2:
printf("get the position for deletion:");
scanf("%d",&x);
if(x==0)
{
break;
}
else if(x==1)
{
printf("first node deletion\n");
t=head;
head=t->nlink;
head->plink=NULL;
printf("deleted element is %d",t->data);
}
else
{
printf("%d th node deletion",x);
t=head;
for(c=1;c<x;c++)
{
t=t->nlink;
}
t->plink->nlink=t->nlink;
if(t->nlink!=NULL)
{
t->nlink->plink=t->plink;
}
}
break;
case 3:
t=head;
printf("prevlink\data=address\tnextnlink\n");
for(;t!=NULL;)
{
printf("%u\t\t",t->plink);
printf(" %d=%u \t\t",t->data,t);
printf("%u\n\n",t->nlink);
t=t->nlink;
}
break;
default:
exit(0);
break;
}
}
}
Output :
1.insertion
2.deletion
3.display
enter your choice:1
Give the value for insertion
48
Give the place for insertion
1
First element insertion
EX.NO: SIVALINGAM.A
10MCA38
Program for Circular doubly linked list
#include<stdio.h>
#include<conio.h>
typedef struct node
{
int data;
struct node *plink,*nlink;
}node1;
node1 *n1,*head,*t;
void firstins()
{
int x;
printf("Enter a Value:\n");
scanf("%d",&x);
n1=(node1*)malloc(sizeof(struct node));
n1->data=x;
if(head==NULL)
{
n1->plink=n1;
n1->nlink=n1;
head=n1;
}
else
{
n1->nlink=head;
for(t=head;t->nlink!=head;t=t->nlink)
{
}
n1->plink=t->nlink;
head->plink=n1;
t->nlink=n1;
head=n1;
}
}
void nthins()
{
int x,pos,i;
printf("Enter a Value:\n");
scanf("%d",&x);
printf("Enter The Position:\n");
scanf("%d",&pos);
n1=(node1*)malloc(sizeof(struct node));
n1->data=x;
if(pos==1)
{
n1->nlink=head;
for(t=head;t->nlink!=head;t=t->nlink)
{
}
n1->plink=t;
t->nlink=n1;
head=n1;
}
else
{
t=head;
for(i=1;i<pos;i++)
{
t=t->nlink;
}
n1->plink=t->plink;
n1->nlink=t;
(t->plink)->nlink=n1;
t->plink=n1;
}
}
void lastins()
{
int x;
printf("Enter a Value:\n");
scanf("%d",&x);
n1=(node1*)malloc(sizeof(struct node));
n1->data=x;
for(t=head;t->nlink!=head;t=t->nlink)
{
}
t->nlink=n1;
n1->plink=t;
n1->nlink=head;
head->plink=n1;
}
void firstdel()
{
for(t=head;t->nlink!=head;t=t->nlink)
{
}
t->nlink=head->nlink;
head=head->nlink;
head->plink=t;
}
void nthdel()
{
int pos,i;
printf("Enter The Position For Deletion:\n");
scanf("%d",&pos);
t=head;
for(i=1;i<pos;i++)
{
t=t->nlink;
}
(t->plink)->nlink=t->nlink;
(t->nlink)->plink=t->plink;
}
void lastdel()
{
for(t=head;t->nlink!=head;t=t->nlink)
{
}
(t->plink)->nlink=head;
head->plink=t->plink;
}
void disp()
{
for(t=head;t->nlink!=head;t=t->nlink)
{
printf("%p\t%d=%p\t%p\n",t->plink,t->data,t,t->nlink);
}
printf("%p\t%d=%p\t%p\n",t->plink,t->data,t,t->nlink);
void main()
{
int ch;
head=NULL;
clrscr();
for(;;)
{
printf("\t\n1.create :");
printf("\t\n2.Insertion:");
printf("\t\n3.1st Node Deletion:");
printf("\t\n4.Nth Node Deletion:");
printf("\t\nEnter Ur Choice:");
scanf("%d",&ch);
switch(ch)
{
case 1: firstins();
disp();
break;
case 2: nthins();
disp();
break;
case 3: firstdel();
disp();
break;
case 4: nthdel();
disp();
break;
default: exit(0);
}
}
}
Output :
1.create :
2.Insertion:
3.1st Node Deletion:
4.Nth Node Deletion:
Enter Ur Choice:1
Enter a Value:
48
0814 48=0814 0814
Enter Ur Choice:2
Enter a Value:
49
Enter The Position:
2
081E 48=0814 081E
0814 49=081E 0814
Enter Ur Choice:2
Enter a Value:
96
Enter The Position:
1
081E 96=0828 0814
0828 48=0814 081E
0814 49=081E 0828
Enter Ur Choice: 4
Enter the position for delete:
3
EX.NO: SIVALINGAM.A
10MCA38
Program for Insertion sort
#include<stdio.h>
#include<conio.h>
void main()
{
int a[20],i,n,t,x;
clrscr();
printf("enter the limit");
scanf("%d",&n);
printf(“\nenter the value one by one\n”);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
for(i=1;i<n;i++)
{
t=a[i];
x=i-1;
while(x>=0&&t<a[x])
{
a[x+1]=a[x];
x=x-1;
}
a[x+1]=t;
}
printf("\n ascending order");
for(i=0;i<n;i++)
{
printf("\n%d",a[i]);
}
getch();
}
Output:
enter the limit 5
enter the value
65
23
85
12
75
ascending order
12
23
65
75
85
EX.NO: SIVALINGAM.A
10MCA38
Program for Selection sort:
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10],i,j,t,n,temp;
clrscr();
printf("enter the limit\n");
scanf("%d",&n);
printf("enter the value one by one\n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<n;i++)
{
temp=i;
for(j=i+1;j<n;j++)
{
if(a[temp]>a[j])
{
temp=j;
}
}
t=a[i];
a[i]=a[temp];
a[temp]=t;
}
printf("\n ascending order");
for(i=0;i<n;i++)
{
printf("\n%d",a[i]);
}
getch();
}
Output:
enter the limit
5
enter the value one by one
10
96
26
100
03
ascending order
3
10
26
96
100
EX.NO: SIVALINGAM.A
10MCA38
Program for Bubble sort:
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10],i,j,t,n;
clrscr();
printf("enter the limit\n");
scanf("%d",&n);
printf("enter the value one by one\n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
for(i=0;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("\nAscending order\n");
for(i=0;i<n;i++)
{
printf("%d\n",a[i]);
}
getch();
}
Output:
enter the limit
8
enter the value one by one
45
63
21
96
87
02
10
25
Ascending order
2
10
21
25
45
63
87
96
EX.NO: SIVALINGAM.A
10MCA38
Program for Quick sort:
#include<stdio.h>
#include<conio.h>
void quick(int[],int ,int);
int split(int[],int ,int);
void main()
{
int j,i,n,a[10];
int low,upp;
clrscr();
printf("enter the limit:");
scanf("%d",&n);
low=n-1;
printf("enter elements:");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
quick(a,0,low);
printf("\nafter sorting:");
for(i=0;i<n;i++)
printf("%d\n",a[i]);
getch();
}
void quick(int a[],int low, int upp)
{
int j;
if(upp>low)
{
j=split(a,low,upp);
quick(a,low,j-1);
quick(a,j+1,upp);
}
}
int split(int a[],int low, int upp)
{
int i,p,t,q;
p=low+1;
q=upp;
i=a[low];
while(q>=p)
{
while(a[p]<i)
p++;
while(a[q]>i)
q--;
if(q>p)
{
t=a[p];
a[p]=a[q];
a[q]=t;
}
}
t=a[low];
a[low]=a[q];
a[q]=t;
return q;
}
Output:
after sorting:
3
16
25
48
96