Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
43 views

Array

This document provides algorithms for implementing a stack and queue using arrays in C programming language. For the stack implementation using array: it initializes the top pointer to -1, uses a menu to perform push, pop, and traverse operations on the stack. The push function adds an element and increments top. The pop function removes an element and decrements top. For the queue implementation using array: it initializes front and rear pointers, uses a menu for enqueue, dequeue and status operations. The enqueue function adds an element at the rear. The dequeue function removes an element from the front and shifts other elements.

Uploaded by

ALVIN JOSE
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views

Array

This document provides algorithms for implementing a stack and queue using arrays in C programming language. For the stack implementation using array: it initializes the top pointer to -1, uses a menu to perform push, pop, and traverse operations on the stack. The push function adds an element and increments top. The pop function removes an element and decrements top. For the queue implementation using array: it initializes front and rear pointers, uses a menu for enqueue, dequeue and status operations. The enqueue function adds an element at the rear. The dequeue function removes an element from the front and shifts other elements.

Uploaded by

ALVIN JOSE
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 23

R-408 Data Structure Lab Manual

Arrays

1
R-408 Data Structure Lab Manual

I Implement a stack using array. Use a menu driven program for push, pop
and display operations

AIM
To implement a stack using array.

ALGORITHM

Step1 :
1.1) Initialize top -1.
Step 2:
2.1) Enter the menu which contains the following details-
Push,Pop,Traverse
Step 3:
3.1) If choice=1 ,call the push function goto step4.
3.2) If choice=2, call the pop function goto step 4
3.3) If choice=3,call the traverse function goto step4
Step 4:
4.1) If you want to continue ,goto step2
Step 5:
5.1)Stop

Push function

Step 1:
1.1) If top =MAX-1,else goto step 2
1.2) Output Stack is full.
1.3) Goto the main function.
Step 2:
2.1) Read the element ,item
2.2) toptop +1
2.3) St[top]item
2.4) Goto main program

Pop function

Step 1:
1.1) If top=-1 else goto step 2
1.2) Output stack is empty
1.3) Goto main program
Step 2:
2.1) Output the element st[top]
2.2) toptop-1
Step 3:
3.1) Return to main program

Tranverse function

Step 1:
1.1) itop
1.2) If top=0 ,else goto step2
1.3) Output stack is empty

2
R-408 Data Structure Lab Manual

1.4) Goto main program


Step 2:
2.1) If i>=0,else goto step2
2.2) Output st[i]
2.3) i i-1,goto step 2
Step 3:
3.1) Goto main program
3.2)
PROGRAM

/* TITLE:IMPLEMENT STACK USING ARRAY */


/*********************************************************************/
#include<stdio.h>
#include<conio.h>
void main()
{

int a[20],item,i,ch,size,top,k=0;

clrscr();
top=-1;
printf("Enter the size of stack:");
scanf("%d",&size);
do
{
printf(" MENU\n _______\n\n");
printf("1: PUSH(to insert an element \n");
printf("2: POP(to delete an element \n");
printf("3: STATUS(to check status \n\n");
printf("Enter the choice(1-3):");
scanf("%d",&ch);
switch(ch)
{
case 1:printf("\nPUSH\n");
if(top>=size-1)
{
printf("\nStack is full");
}
else
{
printf("\nEnter the element:");
scanf("%d",&item);
printf("pushing......");
top++;
a[top]=item;
}
break;
case 2:printf("\nPOP\n");
if(top<0)
{
printf("\nStack is empty");
}

3
R-408 Data Structure Lab Manual

else
{
top--;
printf("\n\nelement is popped.");
}
break;
case 3: printf("checking status........");

if(top<0)
printf("\nstack is empty");
else
{
printf("\nthe element on top is %d",a[top]);
printf("\nDisplaying the stack");
for(i=top;i>=0;i--)
{
printf("\n%d",a[i]);
}
}
break;
default:printf("\nwrong choice!!!");
}
printf("\nPress 1 to continue and 0 to exit. ");
scanf("%d",&k);
}while(k==1);
printf("Exiting..........");
getch();
}

II Implement a queue using array. Use a menu driven program for


insertion, deletion and display operations.

AIM

To implement queue using array

ALGORITHM

Step 1:
1.1) Initialize n0
Step 2:
2.1) Enter your choice from the menu –
Insertion,Deletion,Display.
Step 3:
3.1) If choice =1,goto Insert function ,goto step 4
3.2) If choice=2,goto Delete function ,goto step 4
3.3) If choice=3,goto Traverse function ,goto step 4
Step 4:
4.1) If you want to continue ,goto step 2
Step 5:
5.1) Stop.

4
R-408 Data Structure Lab Manual

Insert function

Step 1:
1.1) If n>maxsize-1,else goto step 2
1.2) Output queue is full, goto main program

Step 2:
2.1) Read the element to a[n]
2.2) nn+1
2.3) Return to main program

Delete function

Step 1:
1.1) i0
1.2) If n=0,else goto step 2
1.3) Output queue is empty
1.4) Goto main program
Step 2:
2.1) Output the element a[0]
2.2) If i<n-1 else goto step3
2.3) a[i] a[i+1]
2.4) ii+1,goto step 2
Step 3:
3.1) n n-1
3.2) Goto main program

Traverse Function

Step 1:
1.1) i0
Step 2:
2.1) If n=0, else goto step 3
2.2) Output the queue is empty ,goto main program
Step 3:
3.1) If i<n else goto step 4
3.2) Output a[i],i i+1 ,goto step 3
Step 4 :
4.1) Goto main program

PROGRAM

/* TITLE:IMPLEMENT QUEUE USING ARRAY


//**************************************************************/
#include<stdio.h>
#include<conio.h>
void main()
{
int rear=0,k=0,front=0;
int item,i,ch,n,q[20];
clrscr();

5
R-408 Data Structure Lab Manual

printf("enter the size of queue:");


scanf("%d",&n);
do
{
printf("\n Menu\n\n1.Enqueue\n2.Dequeue\n3.Status\n");
printf("Enter the choice:");
scanf("%d",&ch) ;
switch(ch)
{
case 1: if(front==0&&rear==0)
{
front=1;
}
if(rear==n)
{
printf("\nQueue is full");
}
else
{
rear++;
printf("\nEnter the element ");
scanf("%d",&item);

q[rear]=item;
}
break;
case 2:if(front==rear+1)
{
printf("\nQueue is empty\n");
}
else
{

front++;
item=q[rear];
}
break;

case 3: printf("\n\nstatus");
if(front==rear+1)
{
printf("\nQueue is empty\n");
}
else
{
printf("\nDisplaying queue\n");
for(i=front;i<=rear;i++)
{
printf("\n%d",q[i]);
}}
break;

6
R-408 Data Structure Lab Manual

default:printf("\nWrong choice!!");
}
printf("\n\nPress 1 to continue and 0 to exit:");
scanf("%d",&k);
}while(k==1);

getch();
}

III Implement a circular queue using array. Use a menu driven program
for insertion, deletion and display operations.

AIM

To implement a circular queue using array.

ALGORITHM

Step 1:
1.1) Rear=0,front=-1
Step 2:
2.1) Enter the choice in the menu- Insertion,Deletion,Traverse
Step 3:
3.1) If choice =1,goto insert function ,goto step 4
3.2) If choice =2,goto delete function,goto step 4
3.3) If choice =3,goto traverse function,goto step 4
Step 4:
4.1) If want to repeat, goto step 2
Step 5:
5.1) Stop

Insert function

Step 1:
1.1) If front=(rear +1) %maxsize ,else goto step 2
1.2) Output circular queue is full
1.3) Goto main program.
Step 2:
2.1) Read the number,num
Step 3:
3.1) If front=-1,else goto step 4
3.2) Front=rear=0
3.3) Goto step 5
Step 4:
4.1) rear=(rear+1)%maxsize
Step 5:
5.1) a[rear]=num
5.2) Goto main program

Delete function

7
R-408 Data Structure Lab Manual

Step 1:
1.1) If front=-1 ,else goto step 2
1.2) Output the queue is empty
1.3) Goto main program
Step 2:
2.1) Output a[front]
Step 3:
3.1) If front=rear, else goto step 4
3.2) front-1
3.3) Goto main program
Step 4:
4.1) front(front + 1)% maxsize
Step 5:
5.1) Goto main program.

Display function

Step 1:
1.1) If front=-1,else goto step 2
1.2) Output queue is empty
1.3) Goto main program
Step 2:
2.1) ifront
Step 3:
3.1) If i<=rear, else gotostep 4
3.2) Output q[i]
3.3) i=i+1,goto step 3
Step 4:
4.1) If front>rear,else goto step 8
4.2) i0
Step 5:
5.1) If i<=rear, else goto step 6
5.2) Output q[i]
5.3) i=i+1,goto step 5
Step 6:
6.1) i=front
Step 7:
7.1) If i<maxsize,else goto step
7.2) Output q[i]
7.3) ii+1,goto step 7
Step 8 :
8.1) Goto main program

PROGRAM
#include<stdio.h>
#include<conio.h>
void del();
void insert();
void status();
int cq[20],front=0,rear=0,length;
void main()
{

8
R-408 Data Structure Lab Manual

int a,k=1;
clrscr();
printf("Enter the size of Circular Queue : ");
scanf("%d",&length);
while(k==1)
{
printf(" MENU\n 1 - Insert\n 2 - Delete\n 3 - Status\n 0 - Exit\n\
n");
printf("Enter choice : ");
scanf("%d",&a);
switch(a)
{
case 1:insert();break;
case 2:del();break;
case 3:status();break;
case 0:exit();
default:puts("Invalid Entry\n");
}
}
}
void insert()
{
int next,item;
printf("\n INSERTION\n");
if(front==0)
{
front=1;
rear=1;
printf("Enter the item to be inserted : ");
scanf("%d",&item);
cq[front]=item;
}
else
{
if(rear==length-1)
next=rear+1;
else
next=(rear+1)%length;
if(next!=front)
{
printf("Enter the item to be inserted : ");
scanf("%d",&item);
rear=next;
cq[rear]=item;
}
else
printf("Queue is full\n");
}
}
void del()
{
int item;

9
R-408 Data Structure Lab Manual

printf("\n Deletion\n");
if(front==0)
{printf("Queue is empty\n");
return;
}
else
{
item=cq[front];
printf("Deleted item is : %d\n",item);
if(front==rear)
{
rear=0;
front=0;
}
else
front=(front+1)%length;
}
}
void status()
{
int i;
printf("\n Status\n");
if(front==0)
printf("Queue is empty\n");
else if(front<=rear)
for(i=front;i<=rear;i++)
printf("%d ",cq[i]);
else
{
for(i=1;i<=rear;i++)
printf("%d ",cq[i]);
for(i=front;i<=length;i++)
printf("%d ",cq[i]);
}
printf("\n");
}

IV Perform Sparse matrix addition


PROGRAM

/* TITLE:SPARSE MATRIX ADDITION */


/*********************************************************************/
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10][10],b[10][10],i,j,m,k=1,n=1,c[10][10],a1[10][10],b1[10][10],r[10][10],r1,r2,c1,c2;
int flag=0,l=1;
clrscr();
puts("Enter the size of the first matrix : ");
scanf("%d%d",&r1,&c1);

10
R-408 Data Structure Lab Manual

printf("Enter the size of the second matrix : ");


scanf("%d%d",&r2,&c2);
if(r1!=r2||c1!=c2)
{puts("Matrics cannt be added");
getch();
exit();
}
printf("Enter the first matrix\n");
m=1;
for(i=0;i<r1;i++)
for(j=0;j<c1;j++)
{scanf("%d",&a[i][j]);
if(a[i][j]!=0)
{a1[m][0]=i;
a1[m][1]=j;
a1[m][2]=a[i][j];
c[n][0]=i;
c[n][1]=j;
c[n][2]=a[i][j];
m++;
n++;
}
}
a1[0][0]=r1;
a1[0][1]=c1;
a1[0][2]=m-1;
printf("Enter the second matrix\n");
for(i=0;i<r2;i++)
for(j=0;j<c2;j++)
{scanf("%d",&b[i][j]);
if(b[i][j]!=0)
{b1[k][0]=i;
b1[k][1]=j;
b1[k][2]=b[i][j];
c[n][0]=i;
c[n][1]=j;
c[n][2]=b[i][j];
k++;
n++;
}
}
b1[0][0]=r2;
b1[0][1]=c2;
b1[0][2]=k-1;
printf("Sparse matrix a is\n");
for(i=0;i<m;i++)
{for(j=0;j<3;j++)
printf("%d ",a1[i][j]);
printf("\n");
}
printf("Sparse matrix b is\n");
for(i=0;i<k;i++)

11
R-408 Data Structure Lab Manual

{for(j=0;j<3;j++)
printf("%d ",b1[i][j]);
printf("\n");
}
for(i=1;i<m;i++)
{for(j=1;j<k;j++)
{if(a1[i][0]==b1[j][0]&&a1[i][1]==b1[j][1])
{r[l][0]=a1[i][0];
r[l][1]=a1[i][1];
r[l][2]=a1[i][2]+b1[j][2];
l++;
flag=1;
}
}
if(flag!=1)
{r[l][0]=a1[i][0];
r[l][1]=a1[i][1];
r[l][2]=a1[i][2];
l++;
}
flag=0;
}
for(i=1;i<k;i++)
{for(j=1;j<m;j++)
{if(b1[i][0]==a1[j][0]&&b1[i][1]==a1[j][1])
flag=1;
}
if(flag!=1)
{
r[l][0]=b1[i][0];
r[l][1]=b1[i][1];
r[l][2]=b1[i][2];
l++;
}
flag=0;
}
r[0][0]=r1;
r[0][1]=c1;
r[0][2]=l-1;
printf("Resultant Sparse matrix r is\n");
for(i=0;i<l;i++)
{for(j=0;j<3;j++)
printf("%d ",r[i][j]);
printf("\n");
}

printf("Final matrix is \n");


flag=0;
for(i=0;i<r1;i++)
{for(j=0;j<c1;j++)
{for(l=1;l<=r[0][2];l++)
{if(i==r[l][0]&&j==r[l][1])

12
R-408 Data Structure Lab Manual

{printf("%d ",r[l][2]);
flag=1;
}
}
if(flag!=1)
printf("0 ");
flag=0;
}
printf("\n");
}
getch();
}

V Perform sparse matrix multiplication. Input is given as the sparse matrix


representation of 2-D arrays. Display the output in sparse matrix
format and its array format.

AIM
To implement sparse matrix multiplication

ALGORITHM

Step 1:
1.1) Start
Step 2:
2.1) Define a structure sp-ele with introw,col,val
2.2) Initialize i=0,j,k,p,array noe.enter the number of elements
in sparse matrix1 and sparsematrix2
Step 3:
3.1) If j<noe[i]
3.2) Output the row and column of the element
3.3) Output the value of the element
3.4) jj+1
Step 4:
4.1) Initialize k=0,i=0,p=0
4.2) If i <noe[0]
4.2.1) Initialize j=0
4.3) If j<noe[1]
4.3.1) check if (m0.col == m1.row)
4.4) if p<k
4.4.1) if (mat[2][p].row==m0.row and mat[2]
[p].col==m1.col)
4.4.2) mat[2][p].val=mat[2][p].val+mo.val*m1.val
4.5) i  i+1
4.6) j j+1
4.7) p p+1
4.8) if p==k
4.8.1) mat[2][k].row=m0.row
4.8.2) mat[2][k].col=m1.col
4.8.3) mat[2][k].val=m0.val *m1.val
4.8.4) k k+1

13
R-408 Data Structure Lab Manual

4.9) Output each set shows the row.no ,col.no value of the
sparse matrix elements respectively.
4.10) Initialize i=0
4.11) If i<k
4.11.1) output m2.row ,m2.col,m2.val
4.11.2) ii+1
Step 5 :
5.1) Stop
PROGRAM

#include <stdio.h>
#include <conio.h>
void main()
{
int a[10][10],b[10][10],m,n,p,q,i,j,k,r,d1,x,c1,h,t,f,c;
int s1[10][3],s2[10][3],s[10][3],count1,count;
clrscr ();
printf("\n Enter the rows & columns of 1st matrix\t");
scanf("%d %d",&m,&n);
printf("\n Enter the rows &columns of the 2nd matrix\t");
scanf("%d %d",&p,&q);
if(n!=p)
{
printf("\n Multiplication cannot be performed\n");
}
printf("\n Enter the elements of 1st matrix\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("\n Enter the elements of 2nd matrix\n");
for(i=0;i<p;i++)
{
for(j=0;j<q;j++)
{
scanf("%d",&b[i][j]);
}
}
k=1;r=m;c=n;
s1[0][0]=r;
s1[0][1]=c;
count=0;
for(i=0;i<=r-1;i++)
{
for(j=0;j<=c-1;j++)
{
if(a[i][j]!=0)
{
s1[k][0]=i;

14
R-408 Data Structure Lab Manual

s1[k][1]=j;
s1[k][2]=a[i][j];
count++;k++;
}
}
}
s1[0][2]=count;
printf("\n the 3 column representation of A is ..\n" );
for(i=0;i<k;i++)
{
for(j=0;j<3;j++)
{
printf(" %d ",s1[i][j]);
}
printf("\n");
}
t=1;f=p;h=q;
s2[0][0]=f;
s2[0][1]=h;
count1=0;k=1;
for(i=0;i<=f-1;i++)
{
for(j=0;j<=h-1;j++)
{
if(b[i][j]!=0)
{
s2[t][0]=i;
s2[t][1]=j;
s2[t][2]=b[i][j];
count1++; t++;
}
}
}
s2[0][2]=count1;
printf("\n the 3 column representation of B is ..\n" );
for(i=0;i<t;i++)
{
for(j=0;j<3;j++)
{
printf(" %d ",s2[i][j]);
}
printf("\n");
}
c1=count;
d1=count1;
x=1;
for(i=1;i<=c1;i++)
{
for(j=1;j<=d1;j++)
{
if(s1[i][1]==s2[j][0])
{

15
R-408 Data Structure Lab Manual

s[x][0]=s1[i][0];
s[x][1]=s2[j][1];
s[x][2]=s1[i][2]*s2[j][2];
x++;
}
}
}
for(i=1;i<x;i++)
{
for(j=i+1;j<x;j++)
{
if((s[i][0]==s[j][0])&&(s[i][1]==s[j][1]))
{
s[i][2]=s[i][2]+s[j][2];
for(r=j;r<x;r++)
{
s[r][0]=s[r+1][0];
s[r][1]=s[r+1][1];
s[r][2]=s[r+1][2];
}
x--;
}
}
}
s[0][0]=n;
s[0][1]=q;
s[0][2]=x-1;
printf("\n the resultant matrix is ...\n");
for(i=0;i<x;i++)
{
for(j=0;j<3;j++)
{
printf(" %d ",s[i][j]);
}
printf("\n");
}
getch();
}

V Implement Polynomial Addition using arrays.


ALGORITHM:
1. Initialize 3 array of structures having 2 parts- coeff and exp .
2. Read two polynomials P and Q, P having n1 terms and Q having n2 terms.
3. Intialize i=0, j=0, k=0.
4. while i<n1 and j<n2 do
i) If P[i].exp=Q[j].exp then
a. R[k].exp=P[i].exp
b. R[k].coeff= P[i].coeff+Q[i].coeff
c. Increment i, j and k.
ii) Else If P[i].exp >Q[j].exp then
a. R[k].exp=P[i].exp;
b. R[k].coeff=P[i].coeff;

16
R-408 Data Structure Lab Manual

c. Increment i and k.

iii) If P[i].exp<Q[j].exp then


a. R[k].exp=Q[j].exp
b. R[k].coeff=Q[j].coeff
c. Increment k andj.
5. End of While
6. While i<n1 then
i) Copy the remaining contents of P to R.
7. While j<n2 then
i) Copy the remaining contents of Q to R
8. Print the resultant Polynomial
9. Stop.

PROGRAM:
# include <stdio.h>
# include <conio.h>
struct element
{int coeff;
int exp;
}p[20],q[20],c[20];
void main()
{int i,j,k,n1,n2;
clrscr();
printf("enter the no of terms in first and second polynomial\n");
scanf("%d %d",&n1,&n2);
printf("\nenter the coefficient of first polynomial\n");
for (i=0;i<n1;i++)
{scanf("%d",&p[i].coeff);
}
printf("enter the exponent of first polynomial\n");
for(i=0;i<n1;i++)
{scanf("%d",&p[i].exp);
}

printf("enter coefficient of second polynomial\n");


for(i=0;i<n2;i++)
{scanf("%d",&q[i].coeff);
}

printf("\nenter the exponent of second polynomial\n");


for(i=0;i<n2;i++)
{scanf("%d",&q[i].exp);
}
i=0;j=0;k=0;
while((i<n1)&&(j<n2))
{if (p[i].exp==q[j].exp)
{ c[k].exp=p[i].exp;
c[k].coeff=p[i].coeff+q[j].coeff;
k++;
j++;
i++;

17
R-408 Data Structure Lab Manual

}
else if(p[i].exp>q[j].exp)
{
c[k].exp=p[i].exp;
c[k].coeff=p[i].coeff;
k++;
i++;
}
else
{if(p[i].exp<q[j].exp);
{ c[k].exp=q[j].exp;
c[k].coeff=q[j].coeff;
k++;
j++;
} }
}
while(i<n1)
{ c[k].exp=p[i].exp;
c[k].coeff=p[i].coeff;
k++;
i++;
} getch();
while(j<n2)
{c[k].exp=q[j].exp;
c[k].coeff=q[j].coeff;
k++;
j++;
}
printf ("the resultant polynomial is\n");
for(i=0;i<k;i++)
{if(c[i].exp!=0)
{printf("%dx^%d+",c[i].coeff,c[i].exp);
}
else
{printf("%d",c[i].coeff);
}
}
getch();

VI Implementation of Deque using arrays

AIM : To implement double ended queue using array

ALGORITHM

Algorithm PUSHDQ(ITEM)
Input: ITEM is to be inserted at the FRONT

18
R-408 Data Structure Lab Manual

Output: Deque with newly inserted element ITEM if it is not full already.
Data structures: DQ being the circular array representation of deque.

Steps:
1. If (FRONT=1) then
a. ahead=LENGTH
2. Else
d. If(FRONT=LENGTH) or (FRONT=0) then
ahead=1
e. Else
Ahead=FRONT-1
EndIf
3. If(ahead=REAR) then
a. Print “Deque is full”
b. Exit
4. Else
a. FRONT=ahead
b. DQ[FRONT]=ITEM
5. Stop

Algorithm POPDQ( )
Steps:
1. If(REAR=FRONT) then
a. Print “Queue is empty”
b. Exit
2. Else
a. ITEM=DQ[FRONT]
b. FRONT=(FRONT +1) mod LENGTH
3. EndIf
4. Stop

Algorithm INJECT(ITEM)
Steps:
1. If((REAR+1) mod LENGTH=FRONT) then
a. Print “Queue is full”
b. Exit
2. Else
a. DQ{REAR]=ITEM
b. REAR=(REAR+1) mod LENGTH
3. EndIf
4. Stop

Algorithm EJECT( )
Input: A deque with elements in it.
Output: The item is deleted from the REAR end.
Data Structures: DQ being the circular array representation of deque.

Steps:
1. If(FRONT=0) then
a. Print “Deque is empty”
b. Exit
2. Else

19
R-408 Data Structure Lab Manual

a. If (FRONT=REAR) then
ITEM=DQ[REAR]
FRONT=REAR=0
b. Else
a) If(REAR=1) then
ITEM=DQ[REAR]
REAR=LENGTH
b) Else
ii. If(REAR=LENGTH) then
ITEM=DQ[REAR]
REAR=1
iii. Else
ITEM=DQ[REAR]
REAR=REAR-1
EndIf
c) EndIf
c. EndIf
3. EndIf
4. Stop

PROGRAM

/* TITLE:IMPLEMENT DEQUE USING ARRAY */


/*********************************************************************/
#include<stdio.h>
#include<conio.h>
void main()
{
int front=-1,rear=-1;
int dq[20],item,i,k,ch,n,c=0;
clrscr();
printf("Enter the no of terms\t");
scanf("%d",&n);
do
{
printf("\n1.INSERT_FRONT\n2.INSERT_REAR\n3.DELETE_FRONT\n");
printf("4.DELETE_REAR\n5.DISPLAY\n6.EXIT\n");
printf("Enter the choice\t");
scanf("%d",&ch);
switch(ch)
{
case 1:if((front==0)&&(rear==n-1))
{
printf("Deque is full \n");
}
else if(front==-1)
{
front=0;rear=0;
printf("\nEnter the element to be inserted\t");
scanf("%d",&item);
dq[front]=item;
}

20
R-408 Data Structure Lab Manual

else if((front==0)&&(rear!=n-1))
{
for(i=0;i<n;i++)
{c++;}
i=1;
k=rear+1;
while(i<=c)
{
dq[k]=dq[k-1];
k--;
if(k==-1)
k=0;
i++;
}
printf("\nenter the element to inserted\t");
scanf("%d",&item);
dq[k]=item;
rear++;
front=k;
}
else
{
front=front-1;
printf("\n Enter the element to be inserted\t");
scanf("%d",&item);
dq[front]=item;
}
break;
case 2:if((front==0)&&(rear==n-1))
printf("\ndeque is full\n");
else if((front==-1)&&(rear==-1))
{
front=0;rear=0;
printf("\n Enter the element to inserted\t");
scanf("%d",&item);
dq[rear]=item;
}
else
{
if(rear==n-1)
{
while((i=front-1)<rear)
{
k=i;
dq[k]=dq[k+1];
k=k+1;
}
rear=rear-1;
front=front-1;
}
rear=rear+1;
printf("\nenter the element to inserted\t");

21
R-408 Data Structure Lab Manual

scanf("%d",&item);
dq[rear]=item;
}
break;
case 3:if(front==-1)
{
printf("\n deque is empty\n");
break;
}
item=dq[front];
printf("\n the element deleted is %d",item);
dq[front]=0;
if(front==rear)
{
front=-1;
rear=-1;
}
else
front=front+1;
break;
case 4:if(front==-1)
{
printf("\n deque is empty\n");
break;
}
item=dq[rear];
printf("\n the element deleted is %d",item);
dq[rear]=0;
rear=rear-1;
if(rear==-1)
front=-1;
break;
case 5:if(front==-1)
printf("\nDeque is empty\t");
else
{
printf("\n the elements are..\n");
for(i=front;i<=rear;i++)
{
printf(" %d ",dq[i]);
}
}
}
} while(ch!=6);
getch();
}

22
R-408 Data Structure Lab Manual

23

You might also like