PRACTICLE-1 |
[1] Introduction to pointers. Call by Value and Call by Reference.
->
//CALL BY VAULE
#include<stdio.h>
void swap(int,int);
void main()
{
int a,b;
printf("\nEnter a Value of a=");
scanf("%d",&a);
printf("\nEnter a Value of b=");
scanf("%d",&b);
swap(a,b);
printf("Old Values==>");
printf("a=%d b=%d \n",a,b);
}
void swap(intp,intq)
{
int tmp;
tmp=p;
p=q;
q=tmp;
printf("New Values After swap==>");
printf("p=%d q=%d",p,q);
}
========================================================================================
OUTPUT
Enter a Value of a=10
Enter a Value of b=20
New Values After swap==> p=20 q=10
Old Values==> a=10 b=20
========================================================================================
//CALL BY REFERENCE
#include<stdio.h>
void swap(int*,int*);
void main()
{
int a,b;
printf("\nEnter a Value of a=");
scanf("%d",&a);
printf("\nEnter a Value of b=");
scanf("%d",&b);
swap(&a,&b);
printf("Old Values==>");
printf("a=%d b=%d \n",a,b);
}
void swap(intp*,intq*)
{
int tmp;
tmp=*p;
*p=*q;
*q=tmp;
printf("New Values After swap==>");
printf("p=%d q=%d",*p,*q);
}
========================================================================================
OUTPUT
Enter a Value of a=10
Enter a Value of b=20
New Values After swap==> p=20 q=10
Old Values==> a=10 b=20
========================================================================================
->
//CALL BY VAULE
#include<stdio.h>
void swap(int,int);
void main()
{
int a,b;
printf("\nEnter a Value of a=");
scanf("%d",&a);
printf("\nEnter a Value of b=");
scanf("%d",&b);
swap(a,b);
printf("Old Values==>");
printf("a=%d b=%d \n",a,b);
}
void swap(intp,intq)
{
int tmp;
tmp=p;
p=q;
q=tmp;
printf("New Values After swap==>");
printf("p=%d q=%d",p,q);
}
========================================================================================
OUTPUT
Enter a Value of a=10
Enter a Value of b=20
New Values After swap==> p=20 q=10
Old Values==> a=10 b=20
========================================================================================
//CALL BY REFERENCE
#include<stdio.h>
void swap(int*,int*);
void main()
{
int a,b;
printf("\nEnter a Value of a=");
scanf("%d",&a);
printf("\nEnter a Value of b=");
scanf("%d",&b);
swap(&a,&b);
printf("Old Values==>");
printf("a=%d b=%d \n",a,b);
}
void swap(intp*,intq*)
{
int tmp;
tmp=*p;
*p=*q;
*q=tmp;
printf("New Values After swap==>");
printf("p=%d q=%d",*p,*q);
}
========================================================================================
OUTPUT
Enter a Value of a=10
Enter a Value of b=20
New Values After swap==> p=20 q=10
Old Values==> a=10 b=20
========================================================================================
PRACTICLE-2
[2] Introduction to Dynamic Memory Allocation. DMA functions malloc(), calloc(),free() etc.
//malloc ()
#include<stdio.h>
void main()
{
int *ptr;
int n,i,sum;
printf("Enter a Total Number=");
scanf("%d",&n);
ptr=(int*)malloc(n*sizeof(int));
for(i=0;i<n;i++)
{
printf("Value=");
scanf("%d",*ptr);
sum=sum+(*ptr);
ptr=(ptr+i);
printf("Ans=%d",*ptr);
}
}
========================================================================================
OUTPUT
Enter a Total Number=4
Value=10
Value=20
Value=30
Value=40
Ans=100
========================================================================================
//calloc ()
#include<stdio.h>
void main()
{
int *ptr;
int n,i,sum;
printf("Enter a Total Number=");
scanf("%d",&n);
ptr=(int*)calloc(n,sizeof(int));
for(i=0;i<n;i++)
{
printf("Value=");
scanf("%d",*ptr);
sum=sum+(*ptr);
ptr=(ptr+i);
printf("Ans=%d",*ptr);
}
}
========================================================================================
OUTPUT
Enter a Total Number=4
Value=10
Value=20
Value=30
Value=40
Ans=100
========================================================================================
//malloc ()
#include<stdio.h>
void main()
{
int *ptr;
int n,i,sum;
printf("Enter a Total Number=");
scanf("%d",&n);
ptr=(int*)malloc(n*sizeof(int));
for(i=0;i<n;i++)
{
printf("Value=");
scanf("%d",*ptr);
sum=sum+(*ptr);
ptr=(ptr+i);
printf("Ans=%d",*ptr);
}
}
========================================================================================
OUTPUT
Enter a Total Number=4
Value=10
Value=20
Value=30
Value=40
Ans=100
========================================================================================
//calloc ()
#include<stdio.h>
void main()
{
int *ptr;
int n,i,sum;
printf("Enter a Total Number=");
scanf("%d",&n);
ptr=(int*)calloc(n,sizeof(int));
for(i=0;i<n;i++)
{
printf("Value=");
scanf("%d",*ptr);
sum=sum+(*ptr);
ptr=(ptr+i);
printf("Ans=%d",*ptr);
}
}
========================================================================================
OUTPUT
Enter a Total Number=4
Value=10
Value=20
Value=30
Value=40
Ans=100
========================================================================================
PRACTICLE-3
[3] Implement a program for stack that performs following operations using array.
(a) PUSH , (b) POP , (c) PEEP , (d) CHANGE , (e) DISPLAY.
->
#include<stdio.h>
#include<stdlib.h>
#define n 5
void push(int [],int *,int );
int pop(int [], int *);
int peep(int [], int * , int );
int change(int [], int * ,int , int );
void display(int [], int *);
void main()
{
int s[n],p,*TOP,x,ch,y,I,a;
TOP=(int*)malloc(sizeof(int));
*TOP=-1;
while(ch<=6)
{
printf("^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^");
printf("\n [1] Push \n [2] Pop \n [3] Peep \n [4] Change \n [5] Display \n [6] Exit \n");
printf("--------------------------------");
printf("Enter Your Choice= ");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("Enter a element=");
scanf("%d",&x);
push(s,TOP,x);
display(s,TOP);
break;
case 2:
y=pop(s,TOP);
printf("deleted element is : %d \n ",a);
display(s,TOP);
break;
case 3:
printf("Enter a the value of I");
scanf("%d",&I);
y=peep(s,TOP,I);
printf("Your Value=%d", y);
display(s,TOP);
break;
case 4:
printf("Enter a Value of x");
scanf("%d",&x);
printf("Enter a Value of I");
scanf("%d",&I);
p=change(s,TOP,x,I);
printf("Your New Value=%d",p);
display(s,TOP);
break;
case 5:
printf("stack is : \n");
display(s,TOP);
break;
case 6:
// printf("invalid choise");
exit(0);
break;
default:
printf("Invalid Choice");
break;
}
}
}
void push(int s[],int *TOP,int x)
{
if(*TOP>=n-1)
{
printf("Overflow");
return ;
}
*TOP=*TOP+1;
s[*TOP]=x;
}
int pop(int s[], int *TOP)
{
if(*TOP==-1)
{
printf("\nUnderflow\n");
exit(0);
}
*TOP=*TOP-1;
return(s[*TOP+1]);
}
int peep(int s[], int *TOP , int I)
{
if((*TOP-I+1)<0)
{
printf("Stack Underflow On PEEP");
exit(0);
}
return(s[*TOP-I+1]);
}
int change(int s[], int *TOP ,int x, int I)
{
if((*TOP-I+1)<0)
{
printf("Stack Underflow On CHANGE");
exit (0);
}
s[*TOP-I+1]=x;
return(s[*TOP-I+1]);
}
void display(int s[], int *TOP)
{
int i;
for(i=*TOP; i>=0; i--)
{
printf("s[%d]:%d \n",i, s[i]);
}
}
========================================================================================
(a) PUSH , (b) POP , (c) PEEP , (d) CHANGE , (e) DISPLAY.
->
#include<stdio.h>
#include<stdlib.h>
#define n 5
void push(int [],int *,int );
int pop(int [], int *);
int peep(int [], int * , int );
int change(int [], int * ,int , int );
void display(int [], int *);
void main()
{
int s[n],p,*TOP,x,ch,y,I,a;
TOP=(int*)malloc(sizeof(int));
*TOP=-1;
while(ch<=6)
{
printf("^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^");
printf("\n [1] Push \n [2] Pop \n [3] Peep \n [4] Change \n [5] Display \n [6] Exit \n");
printf("--------------------------------");
printf("Enter Your Choice= ");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("Enter a element=");
scanf("%d",&x);
push(s,TOP,x);
display(s,TOP);
break;
case 2:
y=pop(s,TOP);
printf("deleted element is : %d \n ",a);
display(s,TOP);
break;
case 3:
printf("Enter a the value of I");
scanf("%d",&I);
y=peep(s,TOP,I);
printf("Your Value=%d", y);
display(s,TOP);
break;
case 4:
printf("Enter a Value of x");
scanf("%d",&x);
printf("Enter a Value of I");
scanf("%d",&I);
p=change(s,TOP,x,I);
printf("Your New Value=%d",p);
display(s,TOP);
break;
case 5:
printf("stack is : \n");
display(s,TOP);
break;
case 6:
// printf("invalid choise");
exit(0);
break;
default:
printf("Invalid Choice");
break;
}
}
}
void push(int s[],int *TOP,int x)
{
if(*TOP>=n-1)
{
printf("Overflow");
return ;
}
*TOP=*TOP+1;
s[*TOP]=x;
}
int pop(int s[], int *TOP)
{
if(*TOP==-1)
{
printf("\nUnderflow\n");
exit(0);
}
*TOP=*TOP-1;
return(s[*TOP+1]);
}
int peep(int s[], int *TOP , int I)
{
if((*TOP-I+1)<0)
{
printf("Stack Underflow On PEEP");
exit(0);
}
return(s[*TOP-I+1]);
}
int change(int s[], int *TOP ,int x, int I)
{
if((*TOP-I+1)<0)
{
printf("Stack Underflow On CHANGE");
exit (0);
}
s[*TOP-I+1]=x;
return(s[*TOP-I+1]);
}
void display(int s[], int *TOP)
{
int i;
for(i=*TOP; i>=0; i--)
{
printf("s[%d]:%d \n",i, s[i]);
}
}
========================================================================================
PRACTICAL - 4
[4] Implement a program to convert infix notation to postfix notation using stack.
//PROGRAM :A infix to postfix conversion(without parenthesis)
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define N 50
int l;
void PUSH(char [],int *,char);
char POP(char [],int *);
int r(char c);
int f(char c);
int main()
{
int i,RANK=0,*TOP;
char INFIX[N],S[N]=" ",POLISH[N]=" ",NEXT=' ',*TEMP;
TEMP=(char *)malloc(sizeof(char *));
TOP=(int *)malloc(sizeof(int *));
*TOP=-1;
printf("\n Enter UNPARENTHESIZED Infix Expression Padded With '#':");
gets(INFIX);
l=strlen(INFIX);
PUSH(S,TOP,'#');
i=0;
NEXT=INFIX[i];
printf("\n character stack content polish rank\n ");
printf("\n---------------------------------------------------\n");
printf("%s",S);
while(NEXT!='#')
{
while(f(NEXT)<=f(S[*TOP]))
{
*TEMP=POP(S,TOP);
strcat(POLISH,TEMP);
RANK=RANK+r(*TEMP);
if(RANK<1)
{
printf("\n INVALID Expression");
exit(0);
}
}
PUSH(S,TOP,NEXT);
printf("\n %c %s %10s %3d ",NEXT,S,POLISH,RANK);
i++;
NEXT=INFIX[i];
}
while(S[*TOP]!='#')
{
*TEMP=POP(S,TOP);
strcat(POLISH,TEMP);
RANK+=r(*TEMP);
if(RANK<1)
{
printf("\n INVALID Expression");
exit(0);
}
printf("\n %c %s %10s %3d ",NEXT,S,POLISH,RANK);
NEXT=TEMP;
}
POLISH[i+1]='\0';
printf("\n\n\n POSTFIX Expression = %s",POLISH);
if(RANK==1)
{
printf("\n\n\t\t VALID Expression \n");
}
else
{
printf("\n\n\t\t INVALID Expression\n");
}
return 0;
}
int r(char c)
{
if(c=='+' || c=='-' || c=='*' || c=='/')
return(-1);
else
return(1);
}
int f(char c)
{
if(c=='+'||c=='-')
return(1);
else if(c=='*'||c=='/')
return(2);
else if(c=='#')
return(0);
else
return(3);
}
void PUSH(char S[],int *TOP,char X)
{
if(*TOP>=N-1)
{
printf("\n STACK Overflow On PUSH");
exit(0);
}
(*TOP)++;
S[*TOP]=X;
}
char POP(char S[],int *TOP)
{
if(*TOP<0)
{
printf("STACK Underflow On POP");
exit(0);
}
(*TOP)--;
return(S[*TOP+1]);
}
==============================================================================
//PROGRAM :A infix to postfix conversion(without parenthesis)
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define N 50
int l;
void PUSH(char [],int *,char);
char POP(char [],int *);
int r(char c);
int f(char c);
int main()
{
int i,RANK=0,*TOP;
char INFIX[N],S[N]=" ",POLISH[N]=" ",NEXT=' ',*TEMP;
TEMP=(char *)malloc(sizeof(char *));
TOP=(int *)malloc(sizeof(int *));
*TOP=-1;
printf("\n Enter UNPARENTHESIZED Infix Expression Padded With '#':");
gets(INFIX);
l=strlen(INFIX);
PUSH(S,TOP,'#');
i=0;
NEXT=INFIX[i];
printf("\n character stack content polish rank\n ");
printf("\n---------------------------------------------------\n");
printf("%s",S);
while(NEXT!='#')
{
while(f(NEXT)<=f(S[*TOP]))
{
*TEMP=POP(S,TOP);
strcat(POLISH,TEMP);
RANK=RANK+r(*TEMP);
if(RANK<1)
{
printf("\n INVALID Expression");
exit(0);
}
}
PUSH(S,TOP,NEXT);
printf("\n %c %s %10s %3d ",NEXT,S,POLISH,RANK);
i++;
NEXT=INFIX[i];
}
while(S[*TOP]!='#')
{
*TEMP=POP(S,TOP);
strcat(POLISH,TEMP);
RANK+=r(*TEMP);
if(RANK<1)
{
printf("\n INVALID Expression");
exit(0);
}
printf("\n %c %s %10s %3d ",NEXT,S,POLISH,RANK);
NEXT=TEMP;
}
POLISH[i+1]='\0';
printf("\n\n\n POSTFIX Expression = %s",POLISH);
if(RANK==1)
{
printf("\n\n\t\t VALID Expression \n");
}
else
{
printf("\n\n\t\t INVALID Expression\n");
}
return 0;
}
int r(char c)
{
if(c=='+' || c=='-' || c=='*' || c=='/')
return(-1);
else
return(1);
}
int f(char c)
{
if(c=='+'||c=='-')
return(1);
else if(c=='*'||c=='/')
return(2);
else if(c=='#')
return(0);
else
return(3);
}
void PUSH(char S[],int *TOP,char X)
{
if(*TOP>=N-1)
{
printf("\n STACK Overflow On PUSH");
exit(0);
}
(*TOP)++;
S[*TOP]=X;
}
char POP(char S[],int *TOP)
{
if(*TOP<0)
{
printf("STACK Underflow On POP");
exit(0);
}
(*TOP)--;
return(S[*TOP+1]);
}
==============================================================================
PRACTICLE-5
[5] WAP to implement QUEUE using arrays that performs following operations.
(a) INSERT
(b) DELETE
(c) DISPLAY
->
Implementation of simple queue using array
#include<stdio.h>
//#include<conio.h>
#include<stdlib.h>
void DISPLAY(int [],int *,int *,int);
int QDELETE(int [],int *,int *);
void QINSERT(int [],int *,int *,int,int);
int main()
{
int Q[100],*f,*r,N,Y,n;
f=(int *) malloc(sizeof(int *));
r=(int *) malloc(sizeof(int *));
*f=*r=-1;
//clrscr();
fflush(stdout);
printf("\n ENTER TOTAL NUMBER OF ELEMENTS IN QUEUE\n");
scanf("%d",&N);
do
{
printf("\n 1.INSERT ELEMENT INTO A QUEUE.\n");
printf("\n 2.DELETE ELEMENT FROM QUEUE.\n");
printf("\n 3.STOP.\n");
printf("\n SELECT YOUR CHOICE->\n");
scanf("%d",&n);
switch(n)
{
case 1: printf("\nenter element:->\n");
scanf("%d",&Y);
QINSERT(Q,f,r,N,Y);
DISPLAY(Q,f,r,N);
break;
case 2: Y=QDELETE(Q,f,r);
printf("\n deleted element - > %d",Y);
DISPLAY(Q,f,r,N);
break;
//case 3: exit(0);
default:printf("\n ILLEGAL CHOICE\n");
break;
}
}while(n!=3);
//getch();
return 0;
}
void DISPLAY(int Q[],int *f,int *r,int N)
{
int i;
printf("\n------------------------------\n");
printf("\n CONTENT OF QUEUE ->\n");
printf("\n------------------------------\n");
for(i=*f;i<=*r;i++)
printf(" %3d ",Q[i]);
printf("\n------------------------------\n");
}
int QDELETE(int Q[],int *f,int *r)
{
int x;
if(*f==-1)
{
printf("\n UNDERFLOW\n");
exit(0);
return(0);
}
x=Q[*f];
if(*f==*r)
{
*f=-1;
*r=-1;
}
else
*f=*f+1;
return(x);
}
void QINSERT(int Q[],int *f,int *r,int N,int Y)
{
if(*r>=N-1)
{
printf("\n QUEUE OVERFLOW");
exit(0);
}
*r=*r+1;
Q[*r]=Y;
if(*f==-1)
{
*f=0;
return;
}
}
(a) INSERT
(b) DELETE
(c) DISPLAY
->
Implementation of simple queue using array
#include<stdio.h>
//#include<conio.h>
#include<stdlib.h>
void DISPLAY(int [],int *,int *,int);
int QDELETE(int [],int *,int *);
void QINSERT(int [],int *,int *,int,int);
int main()
{
int Q[100],*f,*r,N,Y,n;
f=(int *) malloc(sizeof(int *));
r=(int *) malloc(sizeof(int *));
*f=*r=-1;
//clrscr();
fflush(stdout);
printf("\n ENTER TOTAL NUMBER OF ELEMENTS IN QUEUE\n");
scanf("%d",&N);
do
{
printf("\n 1.INSERT ELEMENT INTO A QUEUE.\n");
printf("\n 2.DELETE ELEMENT FROM QUEUE.\n");
printf("\n 3.STOP.\n");
printf("\n SELECT YOUR CHOICE->\n");
scanf("%d",&n);
switch(n)
{
case 1: printf("\nenter element:->\n");
scanf("%d",&Y);
QINSERT(Q,f,r,N,Y);
DISPLAY(Q,f,r,N);
break;
case 2: Y=QDELETE(Q,f,r);
printf("\n deleted element - > %d",Y);
DISPLAY(Q,f,r,N);
break;
//case 3: exit(0);
default:printf("\n ILLEGAL CHOICE\n");
break;
}
}while(n!=3);
//getch();
return 0;
}
void DISPLAY(int Q[],int *f,int *r,int N)
{
int i;
printf("\n------------------------------\n");
printf("\n CONTENT OF QUEUE ->\n");
printf("\n------------------------------\n");
for(i=*f;i<=*r;i++)
printf(" %3d ",Q[i]);
printf("\n------------------------------\n");
}
int QDELETE(int Q[],int *f,int *r)
{
int x;
if(*f==-1)
{
printf("\n UNDERFLOW\n");
exit(0);
return(0);
}
x=Q[*f];
if(*f==*r)
{
*f=-1;
*r=-1;
}
else
*f=*f+1;
return(x);
}
void QINSERT(int Q[],int *f,int *r,int N,int Y)
{
if(*r>=N-1)
{
printf("\n QUEUE OVERFLOW");
exit(0);
}
*r=*r+1;
Q[*r]=Y;
if(*f==-1)
{
*f=0;
return;
}
}
PRACTICLE-6
[6] Write a program to implement Circular Queue using arrays that performs following
operations.
(a) INSERT
(b) DELETE
(c) DISPLAY
->
#include<stdio.h>
#include<stdlib.h>
void DISPLAY(int [],int *,int *,int);
int CQDELETE(int [],int *,int *);
void CQINSERT(int [],int *,int *,int,int);
int main()
{
int Q[100],*f,*r,N,Y,n;
f=(int *) malloc(sizeof(int *));
r=(int *) malloc(sizeof(int *));
*f=*r=-1;
//clrscr();
fflush(stdout);
printf("\n ENTER TOTAL NUMBER OF ELEMENTS IN QUEUE\n");
scanf("%d",&N);
do
{
printf("\n 1.INSERT ELEMENT INTO A QUEUE.\n");
printf("\n 2.DELETE ELEMENT FROM QUEUE.\n");
printf("\n 3.STOP.\n");
printf("\n SELECT YOUR CHOICE->\n");
scanf("%d",&n);
switch(n)
{
case 1: printf("\nenter element:->\n");
scanf("%d",&Y);
CQINSERT(Q,f,r,N,Y);
DISPLAY(Q,f,r,N);
break;
case 2: Y=CQDELETE(Q,f,r);
printf("\n deleted element - > %d",Y);
DISPLAY(Q,f,r,N);
break;
//case 3: exit(0);
default:printf("\n ILLEGAL CHOICE\n");
break;
}
}while(n!=3);
//getch();
return 0;
}
void DISPLAY(int Q[],int *f,int *r,int N)
{
int i;
printf("\n------------------------------\n");
printf("\n CONTENT OF QUEUE ->\n");
printf("\n------------------------------\n");
for(i=*f;i<=*r;i++)
printf(" %3d ",Q[i]);
printf("\n------------------------------\n");
}
int CQDELETE(int Q[],int *f,int *r)
{
int x;
if(*f==-1)
{
printf("\n UNDERFLOW\n");
exit(0);
return(0);
}
x=Q[*f];
if(*f==*r)
{
*f=0;
*r=0;
}
return(x);
if(*f=n-1)
{
*f=0;
}
else
{
*f=*f+1;
return(0);
}
}
void CQINSERT(int Q[],int *f,int *r,int N,int Y)
{
if(*f=*r)
{
printf("\n QUEUE OVERFLOW");
exit(0);
}
Q[*r]=Y;
if(*r=n-1)
{
*r=0;
return;
}
else
{
*r=*r+1;
}
if(*f=-1)
{
*f=0;
return;
}
}
operations.
(a) INSERT
(b) DELETE
(c) DISPLAY
->
#include<stdio.h>
#include<stdlib.h>
void DISPLAY(int [],int *,int *,int);
int CQDELETE(int [],int *,int *);
void CQINSERT(int [],int *,int *,int,int);
int main()
{
int Q[100],*f,*r,N,Y,n;
f=(int *) malloc(sizeof(int *));
r=(int *) malloc(sizeof(int *));
*f=*r=-1;
//clrscr();
fflush(stdout);
printf("\n ENTER TOTAL NUMBER OF ELEMENTS IN QUEUE\n");
scanf("%d",&N);
do
{
printf("\n 1.INSERT ELEMENT INTO A QUEUE.\n");
printf("\n 2.DELETE ELEMENT FROM QUEUE.\n");
printf("\n 3.STOP.\n");
printf("\n SELECT YOUR CHOICE->\n");
scanf("%d",&n);
switch(n)
{
case 1: printf("\nenter element:->\n");
scanf("%d",&Y);
CQINSERT(Q,f,r,N,Y);
DISPLAY(Q,f,r,N);
break;
case 2: Y=CQDELETE(Q,f,r);
printf("\n deleted element - > %d",Y);
DISPLAY(Q,f,r,N);
break;
//case 3: exit(0);
default:printf("\n ILLEGAL CHOICE\n");
break;
}
}while(n!=3);
//getch();
return 0;
}
void DISPLAY(int Q[],int *f,int *r,int N)
{
int i;
printf("\n------------------------------\n");
printf("\n CONTENT OF QUEUE ->\n");
printf("\n------------------------------\n");
for(i=*f;i<=*r;i++)
printf(" %3d ",Q[i]);
printf("\n------------------------------\n");
}
int CQDELETE(int Q[],int *f,int *r)
{
int x;
if(*f==-1)
{
printf("\n UNDERFLOW\n");
exit(0);
return(0);
}
x=Q[*f];
if(*f==*r)
{
*f=0;
*r=0;
}
return(x);
if(*f=n-1)
{
*f=0;
}
else
{
*f=*f+1;
return(0);
}
}
void CQINSERT(int Q[],int *f,int *r,int N,int Y)
{
if(*f=*r)
{
printf("\n QUEUE OVERFLOW");
exit(0);
}
Q[*r]=Y;
if(*r=n-1)
{
*r=0;
return;
}
else
{
*r=*r+1;
}
if(*f=-1)
{
*f=0;
return;
}
}
PRACTICAL - 7
7) Write an program for insert routine in input restricted dequeues.
->
//Implementation of simple queue using array
#include<stdio.h>
//#include<conio.h>
#include<stdlib.h>
void DISPLAY(int [],int *,int *,int);
int FDELETE(int [],int *,int *);
void FINSERT(int [],int *,int *,int,int);
int RDELETE(int [],int *,int *);
int main()
{
int Q[100],*f,*r,N,Y,n;
f=(int *) malloc(sizeof(int *));
r=(int *) malloc(sizeof(int *));
*f=*r=-1;
printf("\n Enter Total Numbers Of Elements In DQUEUE\n");
scanf("%d",&N);
do
{
printf("\n [1] Insert Element From FRONTSIDE.\n");
printf("\n [2] Delete Element From FRONTSIDE.\n");
printf("\n [3] Delete Element From REARSIDE.\n");
printf("\n [4] Stop.\n");
printf("\n Select Your Choice=\n");
scanf("%d",&n);
switch(n)
{
case 1: printf("\nEnter Element=\n");
scanf("%d",&Y);
FINSERT(Q,f,r,N,Y);
DISPLAY(Q,f,r,N);
break;
case 2: Y=FDELETE(Q,f,r);
printf("\n Deleted Element = %d",Y);
DISPLAY(Q,f,r,N);
break;
case 3: Y=RDELETE(Q,f,r);
printf("\n Deleted Element = %d",Y);
DISPLAY(Q,f,r,N);
break;
case 4: exit(0);
default:printf("\n Wrong Choice\n");
break;
}
}while(n!=4);
//getch();
return 0;
}
void DISPLAY(int Q[],int *f,int *r,int N)
{
int i;
printf("\n------------------------------\n");
printf("\n CONTENT OF QUEUE =\n");
printf("\n------------------------------\n");
for(i=*f;i<=*r;i++)
printf(" %3d ",Q[i]);
printf("\n------------------------------\n");
}
int FDELETE(int Q[],int *f,int *r)
{
int x;
if(*f==-1)
{
printf("\n UNDERFLOW\n");
exit(0);
return(0);
}
x=Q[*f];
if(*f==*r)
{
*f=-1;
*r=-1;
}
else
*f=*f+1;
return(x);
}
int RDELETE(int Q[],int *f,int *r)
{
int y;
if(*r==-1)
{
printf("\n DQUEUE UNDERFLOW\n");
exit(0);
return(0);
}
y=Q[*r];
if(*f==*r)
{
*f=-1;
*r=-1;
}
else
*r=*r-1;
return(y);
}
void FINSERT(int Q[],int *f,int *r,int N,int Y)
{ printf("%d",r);
if(*r>=N-1)
{
printf("\n DQUEUE OVERFLOW");
exit(0);
}
*r=*r+1;
Q[*r]=Y;
if(*f==-1)
{
*f=0;
return;
}
}
->
//Implementation of simple queue using array
#include<stdio.h>
//#include<conio.h>
#include<stdlib.h>
void DISPLAY(int [],int *,int *,int);
int FDELETE(int [],int *,int *);
void FINSERT(int [],int *,int *,int,int);
int RDELETE(int [],int *,int *);
int main()
{
int Q[100],*f,*r,N,Y,n;
f=(int *) malloc(sizeof(int *));
r=(int *) malloc(sizeof(int *));
*f=*r=-1;
printf("\n Enter Total Numbers Of Elements In DQUEUE\n");
scanf("%d",&N);
do
{
printf("\n [1] Insert Element From FRONTSIDE.\n");
printf("\n [2] Delete Element From FRONTSIDE.\n");
printf("\n [3] Delete Element From REARSIDE.\n");
printf("\n [4] Stop.\n");
printf("\n Select Your Choice=\n");
scanf("%d",&n);
switch(n)
{
case 1: printf("\nEnter Element=\n");
scanf("%d",&Y);
FINSERT(Q,f,r,N,Y);
DISPLAY(Q,f,r,N);
break;
case 2: Y=FDELETE(Q,f,r);
printf("\n Deleted Element = %d",Y);
DISPLAY(Q,f,r,N);
break;
case 3: Y=RDELETE(Q,f,r);
printf("\n Deleted Element = %d",Y);
DISPLAY(Q,f,r,N);
break;
case 4: exit(0);
default:printf("\n Wrong Choice\n");
break;
}
}while(n!=4);
//getch();
return 0;
}
void DISPLAY(int Q[],int *f,int *r,int N)
{
int i;
printf("\n------------------------------\n");
printf("\n CONTENT OF QUEUE =\n");
printf("\n------------------------------\n");
for(i=*f;i<=*r;i++)
printf(" %3d ",Q[i]);
printf("\n------------------------------\n");
}
int FDELETE(int Q[],int *f,int *r)
{
int x;
if(*f==-1)
{
printf("\n UNDERFLOW\n");
exit(0);
return(0);
}
x=Q[*f];
if(*f==*r)
{
*f=-1;
*r=-1;
}
else
*f=*f+1;
return(x);
}
int RDELETE(int Q[],int *f,int *r)
{
int y;
if(*r==-1)
{
printf("\n DQUEUE UNDERFLOW\n");
exit(0);
return(0);
}
y=Q[*r];
if(*f==*r)
{
*f=-1;
*r=-1;
}
else
*r=*r-1;
return(y);
}
void FINSERT(int Q[],int *f,int *r,int N,int Y)
{ printf("%d",r);
if(*r>=N-1)
{
printf("\n DQUEUE OVERFLOW");
exit(0);
}
*r=*r+1;
Q[*r]=Y;
if(*f==-1)
{
*f=0;
return;
}
}
PRACTICAL - 8
[8] Write a menu driven program to implement following operations on the singly linked list.
(a) Insert a node at the front of the linked list.
(b) Insert a node at the end of the linked list.
(c) Insert a node such that linked list is in asending order.
(d) Delete a First node of the linked list.
(e) Delete a node before specified position.
(f) Delete a node after specified position.
->
//PROGRAM : program to insert element in linked list at beginning,intermedaite and ending position
//delete an item from linked list
#include<stdio.h>
//#include<conio.h>
#include<stdlib.h>
struct linked_list
{
int no;
struct linked_list *next;
};
typedef struct linked_list NODE;
void main()
{
NODE *head,*p;
int a;
void create_list(NODE *);
void print_list(NODE *);
int count(NODE *);
NODE *insert_beg(NODE *);
NODE *insert_bet(NODE *);
NODE *insert_end(NODE *);
NODE *delete_beg(NODE *);
NODE *delete_bet(NODE *);
head=(NODE *)malloc(sizeof(NODE));
//clrscr();
printf("\n Enter -1 to Stop= \n");
create_list(head);
printf(" \n \n \n ");
print_list(head);
printf("\n\n\n\t Total no of Items in linked list-> %d",count(head));
do
{
printf("\n Enter your choice:=\n");
printf("\n 1. Insert at beginning.");
printf("\n 2. Insert at intermediate position.");
printf("\n 3. Insert at last.");
printf("\n 4. Delete element from beginning.");
printf("\n 5. Delete element from intermediate position.");
printf("\n 6. Exit.\n");
scanf("%d",&a);
switch(a)
{
case 1:
head=insert_beg(head);
print_list(head);
break;
case 2:
head=insert_bet(head);
print_list(head);
break;
case 3:
head=insert_end(head);
print_list(head);
break;
case 4:
head=delete_beg(head);
print_list(head);
break;
case 5:
head=delete_bet(head);
print_list(head);
break;
case 6:
exit(0);
default:
printf("<-Invalid choice->");
break;
}
// printf("\n\n\n\t total no of items in linked list-> %d",count(head));
}while(a!=6);
//getch();
}
void create_list(NODE *p)
{
printf("\n\t Enter no to Input: ->\n");
scanf("%d",&p->no);
if(p->no==-1)
{
p->next=NULL;
}
else
{
p->next=(NODE *)malloc(sizeof(NODE));
create_list(p->next);
}
}
void print_list(NODE *p)
{
if(p->next!=NULL)
{
printf(" \t %d-->",p->no);
if(p->next->next==NULL)
printf(" \t %d",p->next->no);
print_list(p->next);
}
}
int count(NODE *p)
{
if(p->next==NULL)
return 0;
else
return(1+count(p->next));
}
NODE *insert_beg(NODE *p)
{
int a,key,key1,x;
NODE *new,*n,*q,*s;
NODE *find(NODE *,int);
printf("\n Insert value of new item:->");
scanf("%d",&x);
fflush(stdin);
new=(NODE *)malloc(sizeof(NODE *));
new->no=x;
new->next=p;
p=new;
return(p);
}
NODE *insert_bet(NODE *p)
{
int a,key,key1,x;
NODE *new,*n,*q,*s;
NODE *find(NODE *,int);
printf("\n Insert value of new item:->");
scanf("%d",&x);
fflush(stdin);
printf("\n Enter value of key item of two consecutive nodes:->");
scanf("%d %d",&key,&key1);
n=find(p,key);
q=find(p,key1);
if(n==NULL ||q==NULL)
{
printf("\n Key node is not found.\n");
}
else
{
new=(NODE *)malloc(sizeof(NODE *));
new->no=x;
new->next=q->next;
q->next=new;
}
return(p);
}
NODE *insert_end(NODE *p)
{
int a,key,key1,x;
NODE *new,*n,*q,*s;
NODE *find(NODE *,int);
printf("\n Insert value of new item:->");
scanf("%d",&x);
// fflush(stdin);
s=p;
while(p->next!=NULL)
{
if(p->next->next==NULL)
break;
p=p->next;
}
new=(NODE *)malloc(sizeof(NODE *));
p->next=new;
new->no=x;
new->next=NULL;
//new->next->no=-1;
return(s);
}
NODE *delete_beg(NODE *p)
{
NODE *find(NODE *,int);
int key;
NODE *h;
NODE *n; //pointer to node preceding key node
// printf("\n Enter which no to be deleted: ->");
// scanf("%d",&key);
// if(p->no==key)
// {
h=p->next;
free(p);
p=h;
//}
return(p);
}
NODE *delete_bet(NODE *p)
{
NODE *find(NODE *,int);
int key;
NODE *h;
NODE *n;//pointer to node preceding key node
printf("\n Enter which no to be deleted: ->");
scanf("%d",&key);
n=find(p,key);
if(n==NULL)
{
printf("\n No is not found.");
}
else
{
h=n->next->next;
free(n->next);
n->next=h;
}
return(p);
}
NODE *find(NODE *p,int key)
{
if(p->next->no==key)
return(p);
else
{
if(p->next->next==NULL)
return(0);
else
find(p->next,key);
}
}
(a) Insert a node at the front of the linked list.
(b) Insert a node at the end of the linked list.
(c) Insert a node such that linked list is in asending order.
(d) Delete a First node of the linked list.
(e) Delete a node before specified position.
(f) Delete a node after specified position.
->
//PROGRAM : program to insert element in linked list at beginning,intermedaite and ending position
//delete an item from linked list
#include<stdio.h>
//#include<conio.h>
#include<stdlib.h>
struct linked_list
{
int no;
struct linked_list *next;
};
typedef struct linked_list NODE;
void main()
{
NODE *head,*p;
int a;
void create_list(NODE *);
void print_list(NODE *);
int count(NODE *);
NODE *insert_beg(NODE *);
NODE *insert_bet(NODE *);
NODE *insert_end(NODE *);
NODE *delete_beg(NODE *);
NODE *delete_bet(NODE *);
head=(NODE *)malloc(sizeof(NODE));
//clrscr();
printf("\n Enter -1 to Stop= \n");
create_list(head);
printf(" \n \n \n ");
print_list(head);
printf("\n\n\n\t Total no of Items in linked list-> %d",count(head));
do
{
printf("\n Enter your choice:=\n");
printf("\n 1. Insert at beginning.");
printf("\n 2. Insert at intermediate position.");
printf("\n 3. Insert at last.");
printf("\n 4. Delete element from beginning.");
printf("\n 5. Delete element from intermediate position.");
printf("\n 6. Exit.\n");
scanf("%d",&a);
switch(a)
{
case 1:
head=insert_beg(head);
print_list(head);
break;
case 2:
head=insert_bet(head);
print_list(head);
break;
case 3:
head=insert_end(head);
print_list(head);
break;
case 4:
head=delete_beg(head);
print_list(head);
break;
case 5:
head=delete_bet(head);
print_list(head);
break;
case 6:
exit(0);
default:
printf("<-Invalid choice->");
break;
}
// printf("\n\n\n\t total no of items in linked list-> %d",count(head));
}while(a!=6);
//getch();
}
void create_list(NODE *p)
{
printf("\n\t Enter no to Input: ->\n");
scanf("%d",&p->no);
if(p->no==-1)
{
p->next=NULL;
}
else
{
p->next=(NODE *)malloc(sizeof(NODE));
create_list(p->next);
}
}
void print_list(NODE *p)
{
if(p->next!=NULL)
{
printf(" \t %d-->",p->no);
if(p->next->next==NULL)
printf(" \t %d",p->next->no);
print_list(p->next);
}
}
int count(NODE *p)
{
if(p->next==NULL)
return 0;
else
return(1+count(p->next));
}
NODE *insert_beg(NODE *p)
{
int a,key,key1,x;
NODE *new,*n,*q,*s;
NODE *find(NODE *,int);
printf("\n Insert value of new item:->");
scanf("%d",&x);
fflush(stdin);
new=(NODE *)malloc(sizeof(NODE *));
new->no=x;
new->next=p;
p=new;
return(p);
}
NODE *insert_bet(NODE *p)
{
int a,key,key1,x;
NODE *new,*n,*q,*s;
NODE *find(NODE *,int);
printf("\n Insert value of new item:->");
scanf("%d",&x);
fflush(stdin);
printf("\n Enter value of key item of two consecutive nodes:->");
scanf("%d %d",&key,&key1);
n=find(p,key);
q=find(p,key1);
if(n==NULL ||q==NULL)
{
printf("\n Key node is not found.\n");
}
else
{
new=(NODE *)malloc(sizeof(NODE *));
new->no=x;
new->next=q->next;
q->next=new;
}
return(p);
}
NODE *insert_end(NODE *p)
{
int a,key,key1,x;
NODE *new,*n,*q,*s;
NODE *find(NODE *,int);
printf("\n Insert value of new item:->");
scanf("%d",&x);
// fflush(stdin);
s=p;
while(p->next!=NULL)
{
if(p->next->next==NULL)
break;
p=p->next;
}
new=(NODE *)malloc(sizeof(NODE *));
p->next=new;
new->no=x;
new->next=NULL;
//new->next->no=-1;
return(s);
}
NODE *delete_beg(NODE *p)
{
NODE *find(NODE *,int);
int key;
NODE *h;
NODE *n; //pointer to node preceding key node
// printf("\n Enter which no to be deleted: ->");
// scanf("%d",&key);
// if(p->no==key)
// {
h=p->next;
free(p);
p=h;
//}
return(p);
}
NODE *delete_bet(NODE *p)
{
NODE *find(NODE *,int);
int key;
NODE *h;
NODE *n;//pointer to node preceding key node
printf("\n Enter which no to be deleted: ->");
scanf("%d",&key);
n=find(p,key);
if(n==NULL)
{
printf("\n No is not found.");
}
else
{
h=n->next->next;
free(n->next);
n->next=h;
}
return(p);
}
NODE *find(NODE *p,int key)
{
if(p->next->no==key)
return(p);
else
{
if(p->next->next==NULL)
return(0);
else
find(p->next,key);
}
}
PRACTICLE - 9
[9] Wriite a program to implement following operations on the doubly linked list.
(a) Insert a node at the front of the linked list.
(b) Insert a node at the end of the linked list.
(c) Delete a last node of the linked list.
(d) Delete a node before specified position.
->
//Doubly Linked Linear List
#include<stdio.h>
#include<stdlib.h>
struct node
{
int info;
struct node *lptr,*rptr;
}*l,*r;
void doubins(int);
void doubdel(int);
void display();
int chk;
void main()
{
int ch,x;
l=NULL;
r=NULL;
do
{
printf("\n Press:=>\n");
printf("\n 1.Insert Node");
printf("\n 2.Delete Node");
printf("\n 3.Display Doubly Linked List");
printf("\n 4.Exit");
printf("\n Enter Choice: ");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("\n\t Enter Element: ");
scanf("%d",&x);
doubins(x);
display();
break;
case 2:
printf("\n\t Enter Element which you want to Delete: ");
scanf("%d",&x);
chk=0;
if(l!=NULL) //if(r!=NULL)
{
printf("\n\n Before Deletion:=>");
printf("\n----------------");
display();
}
doubdel(x);
if(chk==0) //if(r!=NULL)
{
printf("\n\n\n\n\n After Deletion:=>");
printf("\n---------------");
display();
}
break;
case 3:
display();
break;
case 4:
exit(0);
default:
printf("\n\t Invalid Choice.\n\tTry Again.");
}
}while(ch!=4);
}
void doubins(int x)
{
struct node *New,*temp;
New=(struct node *)malloc(sizeof(struct node));
New->info=x;
if(l==NULL) //if(r==NULL)
{
New->lptr=NULL;
New->rptr=NULL;
l=New;
r=New;
return;
}
if(x<=l->info) //If the Node is less than all the Nodes
{
New->lptr=NULL;
New->rptr=l;
l->lptr=New;
l=New;
return;
}
temp=l; //If the Node is inserted in between two nodes
while(temp->info<x && temp!=NULL)
temp=temp->rptr;
if(temp!=NULL)
{
New->lptr=temp->lptr;
New->rptr=temp;
temp->lptr=New;
New->lptr->rptr=New;
return;
}
New->rptr=NULL; //Node is inserted at last
New->lptr=r;
r->rptr=New;
r=New;
}
void doubdel(int x)
{
struct node *temp;
if(l==NULL) //if(r==NULL)
{
printf("\n\n\tDoubly Linked List Underflow on Delete.");
chk=1;
return;
}
if(x==l->info) //If First Node is tobe Deleted
{
temp=l;
l=l->rptr;
l->lptr=NULL;
free(temp);
return;
}
if(x==r->info) //If Last Node is tobe Deleted
{
temp=r;
r=r->lptr;
r->rptr=NULL;
free(temp);
return;
}
temp=l;
while(temp->info!=x && temp!=NULL)
temp=temp->rptr;
if(temp==NULL) //If Node not found in the List
{
/* gotoxy(1,13);
delline();
gotoxy(1,13);
delline();
gotoxy(1,14);
delline();
gotoxy(1,16);
delline();
gotoxy(1,17);
delline();
gotoxy(1,18);
delline();
gotoxy(10,13);
printf("\n\tNode not found.");
chk=1;
return; */
}
temp->lptr->rptr=temp->rptr;
temp->rptr->lptr=temp->lptr;
free(temp);
return;
}
void display()
{
struct node *temp;
if(l==NULL) //if(r==NULL)
printf("\n\n\tDoubly Linked List is Empty.");
else
{
printf("\n\nDoubly Linked List:\n\n\n");
printf("l = %u\n\n",l);
temp=l;
while(temp!=NULL)
{
printf("[%u|%u|%d|%u]->",temp,temp->lptr,temp->info,temp->rptr);
temp=temp->rptr;
}
printf("NULL");
printf("\n\nr = %u",r);
}
}
(a) Insert a node at the front of the linked list.
(b) Insert a node at the end of the linked list.
(c) Delete a last node of the linked list.
(d) Delete a node before specified position.
->
//Doubly Linked Linear List
#include<stdio.h>
#include<stdlib.h>
struct node
{
int info;
struct node *lptr,*rptr;
}*l,*r;
void doubins(int);
void doubdel(int);
void display();
int chk;
void main()
{
int ch,x;
l=NULL;
r=NULL;
do
{
printf("\n Press:=>\n");
printf("\n 1.Insert Node");
printf("\n 2.Delete Node");
printf("\n 3.Display Doubly Linked List");
printf("\n 4.Exit");
printf("\n Enter Choice: ");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("\n\t Enter Element: ");
scanf("%d",&x);
doubins(x);
display();
break;
case 2:
printf("\n\t Enter Element which you want to Delete: ");
scanf("%d",&x);
chk=0;
if(l!=NULL) //if(r!=NULL)
{
printf("\n\n Before Deletion:=>");
printf("\n----------------");
display();
}
doubdel(x);
if(chk==0) //if(r!=NULL)
{
printf("\n\n\n\n\n After Deletion:=>");
printf("\n---------------");
display();
}
break;
case 3:
display();
break;
case 4:
exit(0);
default:
printf("\n\t Invalid Choice.\n\tTry Again.");
}
}while(ch!=4);
}
void doubins(int x)
{
struct node *New,*temp;
New=(struct node *)malloc(sizeof(struct node));
New->info=x;
if(l==NULL) //if(r==NULL)
{
New->lptr=NULL;
New->rptr=NULL;
l=New;
r=New;
return;
}
if(x<=l->info) //If the Node is less than all the Nodes
{
New->lptr=NULL;
New->rptr=l;
l->lptr=New;
l=New;
return;
}
temp=l; //If the Node is inserted in between two nodes
while(temp->info<x && temp!=NULL)
temp=temp->rptr;
if(temp!=NULL)
{
New->lptr=temp->lptr;
New->rptr=temp;
temp->lptr=New;
New->lptr->rptr=New;
return;
}
New->rptr=NULL; //Node is inserted at last
New->lptr=r;
r->rptr=New;
r=New;
}
void doubdel(int x)
{
struct node *temp;
if(l==NULL) //if(r==NULL)
{
printf("\n\n\tDoubly Linked List Underflow on Delete.");
chk=1;
return;
}
if(x==l->info) //If First Node is tobe Deleted
{
temp=l;
l=l->rptr;
l->lptr=NULL;
free(temp);
return;
}
if(x==r->info) //If Last Node is tobe Deleted
{
temp=r;
r=r->lptr;
r->rptr=NULL;
free(temp);
return;
}
temp=l;
while(temp->info!=x && temp!=NULL)
temp=temp->rptr;
if(temp==NULL) //If Node not found in the List
{
/* gotoxy(1,13);
delline();
gotoxy(1,13);
delline();
gotoxy(1,14);
delline();
gotoxy(1,16);
delline();
gotoxy(1,17);
delline();
gotoxy(1,18);
delline();
gotoxy(10,13);
printf("\n\tNode not found.");
chk=1;
return; */
}
temp->lptr->rptr=temp->rptr;
temp->rptr->lptr=temp->lptr;
free(temp);
return;
}
void display()
{
struct node *temp;
if(l==NULL) //if(r==NULL)
printf("\n\n\tDoubly Linked List is Empty.");
else
{
printf("\n\nDoubly Linked List:\n\n\n");
printf("l = %u\n\n",l);
temp=l;
while(temp!=NULL)
{
printf("[%u|%u|%d|%u]->",temp,temp->lptr,temp->info,temp->rptr);
temp=temp->rptr;
}
printf("NULL");
printf("\n\nr = %u",r);
}
}
PRACTICLE - 10
10)Write a program to concatenate two doubly linked lists.
->
#include<stdio.h>
#include<stdlib.h>
struct node
{
int info;
struct node *lptr,*rptr;
}*l,*r,*l1,*r1;
void doubins(int);
void doubins1(int);
void display();
void display1();
void merge();
int chk;
void main()
{
int ch,x;
l=NULL;
r=NULL;
l1=NULL;
r1=NULL;
do
{
printf("\n\n [1] Create First Doubly Linked List");
printf("\n [2] Create Second Doubly Linked List");
printf("\n [3] Merge both list");
printf("\n [4] Exit");
printf("\n\n\tEnter Choice: ");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("\n\tEnter Element: ");
scanf("%d",&x);
doubins(x);
display();
break;
case 2:
printf("\n\tEnter Element: ");
scanf("%d",&x);
doubins1(x);
display1();
break;
case 3: merge();
display();
break;
case 4:
exit(0);
default:
printf("\n\tInvalid Choice.\n\tTry Again.");
}
}while(ch!=3);
}
void doubins(int x)
{
struct node *New,*temp;
int sc;
New=(struct node *)malloc(sizeof(struct node));
New->info=x;
if(l==NULL)
{
New->lptr=NULL;
New->rptr=NULL;
l=New;
r=New;
return;
}
if(x<=l->info)
{
New->lptr=NULL;
New->rptr=l;
l->lptr=New;
l=New;
return;
}
temp=l;
while(temp->rptr!=NULL && temp->info<x)
{
temp=temp->rptr;
}
if(temp->info<x)
{
New->rptr=NULL;
New->lptr=r;
r->rptr=New;
r=New;
return;
}
New->lptr=temp->lptr;
New->rptr=temp;
temp->lptr=New;
New->lptr->rptr=New;
}
void doubins1(int x)
{
struct node *New,*temp;
int sc;
New=(struct node *)malloc(sizeof(struct node));
New->info=x;
if(l1==NULL)
{
New->lptr=NULL;
New->rptr=NULL;
l1=New;
r1=New;
return;
}
if(x<=l1->info)
{
New->lptr=NULL;
New->rptr=l1;
l1->lptr=New;
l1=New;
return;
}
temp=l1;
while(temp->rptr!=NULL && temp->info<x)
{
temp=temp->rptr;
}
if(temp->info<x)
{
New->rptr=NULL;
New->lptr=r1;
r1->rptr=New;
r1=New;
return;
}
New->lptr=temp->lptr;
New->rptr=temp;
temp->lptr=New;
New->lptr->rptr=New;
}
void display()
{
struct node *temp;
if(l==NULL)
printf("\n\n\tDoubly Linked List is Empty.");
else
{
printf("\n\nDoubly Linked List:\n\n\n");
printf("-----------------------------\n");
printf("l = %u\n\n",l);
printf("-----------------------------\n");
temp=l;
while(temp!=NULL)
{
printf("[%u|%u|%d|%u]->",temp,temp->lptr,temp->info,temp->rptr);
temp=temp->rptr;
}
printf("NULL");
printf("\n\nr= %u",r);
}
}
void display1()
{
struct node *temp;
if(l1==NULL)
printf("\n\tDoubly Linked List is Empty.");
else
{
printf("\n\nDoubly Linked List:\n\n\n");
printf("l1 = %u\n\n",l1);
temp=l1;
while(temp!=NULL)
{
printf("[%u|%u|%d|%u]->",temp,temp->lptr,temp->info,temp->rptr);
temp=temp->rptr;
}
printf("NULL");
printf("\n\nr1= %u",r1);
}
}
void merge()
{
r->rptr=l1;
l1->lptr=r;
}
->
#include<stdio.h>
#include<stdlib.h>
struct node
{
int info;
struct node *lptr,*rptr;
}*l,*r,*l1,*r1;
void doubins(int);
void doubins1(int);
void display();
void display1();
void merge();
int chk;
void main()
{
int ch,x;
l=NULL;
r=NULL;
l1=NULL;
r1=NULL;
do
{
printf("\n\n [1] Create First Doubly Linked List");
printf("\n [2] Create Second Doubly Linked List");
printf("\n [3] Merge both list");
printf("\n [4] Exit");
printf("\n\n\tEnter Choice: ");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("\n\tEnter Element: ");
scanf("%d",&x);
doubins(x);
display();
break;
case 2:
printf("\n\tEnter Element: ");
scanf("%d",&x);
doubins1(x);
display1();
break;
case 3: merge();
display();
break;
case 4:
exit(0);
default:
printf("\n\tInvalid Choice.\n\tTry Again.");
}
}while(ch!=3);
}
void doubins(int x)
{
struct node *New,*temp;
int sc;
New=(struct node *)malloc(sizeof(struct node));
New->info=x;
if(l==NULL)
{
New->lptr=NULL;
New->rptr=NULL;
l=New;
r=New;
return;
}
if(x<=l->info)
{
New->lptr=NULL;
New->rptr=l;
l->lptr=New;
l=New;
return;
}
temp=l;
while(temp->rptr!=NULL && temp->info<x)
{
temp=temp->rptr;
}
if(temp->info<x)
{
New->rptr=NULL;
New->lptr=r;
r->rptr=New;
r=New;
return;
}
New->lptr=temp->lptr;
New->rptr=temp;
temp->lptr=New;
New->lptr->rptr=New;
}
void doubins1(int x)
{
struct node *New,*temp;
int sc;
New=(struct node *)malloc(sizeof(struct node));
New->info=x;
if(l1==NULL)
{
New->lptr=NULL;
New->rptr=NULL;
l1=New;
r1=New;
return;
}
if(x<=l1->info)
{
New->lptr=NULL;
New->rptr=l1;
l1->lptr=New;
l1=New;
return;
}
temp=l1;
while(temp->rptr!=NULL && temp->info<x)
{
temp=temp->rptr;
}
if(temp->info<x)
{
New->rptr=NULL;
New->lptr=r1;
r1->rptr=New;
r1=New;
return;
}
New->lptr=temp->lptr;
New->rptr=temp;
temp->lptr=New;
New->lptr->rptr=New;
}
void display()
{
struct node *temp;
if(l==NULL)
printf("\n\n\tDoubly Linked List is Empty.");
else
{
printf("\n\nDoubly Linked List:\n\n\n");
printf("-----------------------------\n");
printf("l = %u\n\n",l);
printf("-----------------------------\n");
temp=l;
while(temp!=NULL)
{
printf("[%u|%u|%d|%u]->",temp,temp->lptr,temp->info,temp->rptr);
temp=temp->rptr;
}
printf("NULL");
printf("\n\nr= %u",r);
}
}
void display1()
{
struct node *temp;
if(l1==NULL)
printf("\n\tDoubly Linked List is Empty.");
else
{
printf("\n\nDoubly Linked List:\n\n\n");
printf("l1 = %u\n\n",l1);
temp=l1;
while(temp!=NULL)
{
printf("[%u|%u|%d|%u]->",temp,temp->lptr,temp->info,temp->rptr);
temp=temp->rptr;
}
printf("NULL");
printf("\n\nr1= %u",r1);
}
}
void merge()
{
r->rptr=l1;
l1->lptr=r;
}
PRACTICLE - 11
11)Implement recursive and non-recursive tree traversing methods in-order, preorder and
post-order traversal.
->
//Binary Search Tree
#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
struct node
{
int data;
struct node *lptr,*rptr;
};
void Insert(struct node *,int);
void Preorder(struct node *);
void Postorder(struct node *);
void Inorder(struct node *);
void Delete(struct node *,int);
struct node *Header;
int main()
{
int ch,x;
Header=(struct node*)malloc(sizeof(struct node));
Header->lptr=Header;
Header->rptr=Header;
do
{
printf("\n1.Insert a node in a Tree");
printf("\n2.Preorder Traversal(Recursively)");
printf("\n3.Postorder Traversal(Recursively)");
printf("\n4.Inorder Traversal(Recursively)");
printf("\n5.Delete a node from Binary Search Tree");
printf("\n6.Exit");
printf("\n\nEnter Choice: ");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("\n\tEnter Element : ");
scanf("%d",&x);
Insert(Header,x);
printf("\n\tInorder Traversal(Recursively)=\n\t");
printf("\n\t-------------------------------------\n\t");
Inorder(Header->lptr);
printf("\n\t-------------------------------------\n\t");
break;
case 2:
printf("\n\tPreorder Traversal(Recursively):\n\t");
printf("\n\t-------------------------------------\n\t");
Preorder(Header->lptr);
printf("\n\t-------------------------------------\n\t");
break;
case 3:
printf("\n\tPostorder Traversal(Recursively):\n\t");
printf("\n\t-------------------------------------\n\t");
Postorder(Header->lptr);
printf("\n\t-------------------------------------\n\t");
break;
case 4:
printf("\n\tInorder Traversal(Recursively):\n\t");
printf("\n\t-------------------------------------\n\t");
Inorder(Header->lptr);
printf("\n\t-------------------------------------\n\t");
break;
case 5:
printf("\n\tEnter Element which u want to Delete: ");
scanf("%d",&x);
printf("\n\t-------------------------------------\n\t");
Delete(Header,x);
printf("\n\t-------------------------------------\n\t");
break;
case 6: exit(0);
break;
default:
printf("\n\t Plz Try Again.");
}
}while(6);
}
void Insert(struct node *h,int x)
{
struct node *New,*parent,*cur;
New=(struct node *)malloc(sizeof(struct node));
if(New==NULL)
{
printf("\n\tNo Tree Node Available.");
return;
}
New->data=x;
New->lptr=NULL;
New->rptr=NULL;
if(h->lptr==h)
{
h->lptr=New;
return;
}
cur=h->lptr;
parent=h;
while(cur!=NULL)
{
if(x<cur->data)
{
parent=cur;
cur=cur->lptr;
}
else if(x>cur->data)
{
parent=cur;
cur=cur->rptr;
}
else
{
printf("\n\t Element Already Exist.\n");
return;
}
}
if(x<parent->data)
{
parent->lptr=New;
return;
}
if(x>parent->data)
{
parent->rptr=New;
return;
}
return;
}
void Preorder(struct node *t)
{
if(t!=NULL)
printf("%d ",t->data);
else
{
printf("\n\t Empty Tree.");
return;
}
if(t->lptr!=NULL)
Preorder(t->lptr);
if(t->rptr!=NULL)
Preorder(t->rptr);
return;
}
void Postorder(struct node *t)
{
if(t==NULL)
{
printf("\n\t Empty Tree.");
return;
}
Postorder(t->lptr);
Postorder(t->rptr);
printf("%d ",t->data);
return;
}
void Inorder(struct node *t)
{
if(t==NULL)
{
printf("\n\t Empty Tree.");
return;
}
if(t->lptr!=NULL)
Inorder(t->lptr);
printf("%d ",t->data);
if(t->rptr!=NULL)
Inorder(t->rptr);
return;
}
void Delete(struct node *h,int x)
{
int found;
char d;
struct node *cur,*parent,*pred,*suc,*q;
if(h->lptr==h)
{
printf("\n\t Empty Tree.");
return;
}
parent=h;
cur=h->lptr;
d='L';
found=0;
while(!found && cur!=NULL)
{
if(x==cur->data)
found=1;
else if(x<cur->data)
{
parent=cur;
cur=cur->lptr;
d='L';
}
else
{
parent=cur;
cur=cur->rptr;
d='R';
}
}
if(!found)
{
printf("\n\t Node is not found.");
return;
}
if(cur->lptr==NULL)
q=cur->rptr;
else
{
if(cur->rptr==NULL)
q=cur->lptr;
else
{
suc=cur->rptr;
if(suc->lptr==NULL)
{
suc->lptr=cur->lptr;
q=suc;
}
else
{
pred=cur->rptr;
suc=pred->lptr;
while(suc->lptr!=NULL)
{
pred=suc;
suc=pred->lptr;
}
pred->lptr=suc->rptr;
suc->lptr=cur->lptr;
suc->rptr=cur->rptr;
q=suc;
}
}
}
if(d=='L')
parent->lptr=q;
else
parent->rptr=q;
printf("\n\t%d is Deleted.",x);
}
post-order traversal.
->
//Binary Search Tree
#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
struct node
{
int data;
struct node *lptr,*rptr;
};
void Insert(struct node *,int);
void Preorder(struct node *);
void Postorder(struct node *);
void Inorder(struct node *);
void Delete(struct node *,int);
struct node *Header;
int main()
{
int ch,x;
Header=(struct node*)malloc(sizeof(struct node));
Header->lptr=Header;
Header->rptr=Header;
do
{
printf("\n1.Insert a node in a Tree");
printf("\n2.Preorder Traversal(Recursively)");
printf("\n3.Postorder Traversal(Recursively)");
printf("\n4.Inorder Traversal(Recursively)");
printf("\n5.Delete a node from Binary Search Tree");
printf("\n6.Exit");
printf("\n\nEnter Choice: ");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("\n\tEnter Element : ");
scanf("%d",&x);
Insert(Header,x);
printf("\n\tInorder Traversal(Recursively)=\n\t");
printf("\n\t-------------------------------------\n\t");
Inorder(Header->lptr);
printf("\n\t-------------------------------------\n\t");
break;
case 2:
printf("\n\tPreorder Traversal(Recursively):\n\t");
printf("\n\t-------------------------------------\n\t");
Preorder(Header->lptr);
printf("\n\t-------------------------------------\n\t");
break;
case 3:
printf("\n\tPostorder Traversal(Recursively):\n\t");
printf("\n\t-------------------------------------\n\t");
Postorder(Header->lptr);
printf("\n\t-------------------------------------\n\t");
break;
case 4:
printf("\n\tInorder Traversal(Recursively):\n\t");
printf("\n\t-------------------------------------\n\t");
Inorder(Header->lptr);
printf("\n\t-------------------------------------\n\t");
break;
case 5:
printf("\n\tEnter Element which u want to Delete: ");
scanf("%d",&x);
printf("\n\t-------------------------------------\n\t");
Delete(Header,x);
printf("\n\t-------------------------------------\n\t");
break;
case 6: exit(0);
break;
default:
printf("\n\t Plz Try Again.");
}
}while(6);
}
void Insert(struct node *h,int x)
{
struct node *New,*parent,*cur;
New=(struct node *)malloc(sizeof(struct node));
if(New==NULL)
{
printf("\n\tNo Tree Node Available.");
return;
}
New->data=x;
New->lptr=NULL;
New->rptr=NULL;
if(h->lptr==h)
{
h->lptr=New;
return;
}
cur=h->lptr;
parent=h;
while(cur!=NULL)
{
if(x<cur->data)
{
parent=cur;
cur=cur->lptr;
}
else if(x>cur->data)
{
parent=cur;
cur=cur->rptr;
}
else
{
printf("\n\t Element Already Exist.\n");
return;
}
}
if(x<parent->data)
{
parent->lptr=New;
return;
}
if(x>parent->data)
{
parent->rptr=New;
return;
}
return;
}
void Preorder(struct node *t)
{
if(t!=NULL)
printf("%d ",t->data);
else
{
printf("\n\t Empty Tree.");
return;
}
if(t->lptr!=NULL)
Preorder(t->lptr);
if(t->rptr!=NULL)
Preorder(t->rptr);
return;
}
void Postorder(struct node *t)
{
if(t==NULL)
{
printf("\n\t Empty Tree.");
return;
}
Postorder(t->lptr);
Postorder(t->rptr);
printf("%d ",t->data);
return;
}
void Inorder(struct node *t)
{
if(t==NULL)
{
printf("\n\t Empty Tree.");
return;
}
if(t->lptr!=NULL)
Inorder(t->lptr);
printf("%d ",t->data);
if(t->rptr!=NULL)
Inorder(t->rptr);
return;
}
void Delete(struct node *h,int x)
{
int found;
char d;
struct node *cur,*parent,*pred,*suc,*q;
if(h->lptr==h)
{
printf("\n\t Empty Tree.");
return;
}
parent=h;
cur=h->lptr;
d='L';
found=0;
while(!found && cur!=NULL)
{
if(x==cur->data)
found=1;
else if(x<cur->data)
{
parent=cur;
cur=cur->lptr;
d='L';
}
else
{
parent=cur;
cur=cur->rptr;
d='R';
}
}
if(!found)
{
printf("\n\t Node is not found.");
return;
}
if(cur->lptr==NULL)
q=cur->rptr;
else
{
if(cur->rptr==NULL)
q=cur->lptr;
else
{
suc=cur->rptr;
if(suc->lptr==NULL)
{
suc->lptr=cur->lptr;
q=suc;
}
else
{
pred=cur->rptr;
suc=pred->lptr;
while(suc->lptr!=NULL)
{
pred=suc;
suc=pred->lptr;
}
pred->lptr=suc->rptr;
suc->lptr=cur->lptr;
suc->rptr=cur->rptr;
q=suc;
}
}
}
if(d=='L')
parent->lptr=q;
else
parent->rptr=q;
printf("\n\t%d is Deleted.",x);
}