C LAB‎ > ‎(Sem2) Data Structures‎ > ‎

Q9. WAP to implement Queue using Arrays.

posted Feb 1, 2011, 1:25 PM by Neil Mathew   [ updated Mar 14, 2011, 10:20 AM ]

SOURCE CODE:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#include <stdio.h>
 
void INS(int *a, int n, int *beg, int *end, int ITEM)
{
    if( *beg==-1 ) //FIRST ELEMENT
        {
        *beg=0;
        *end=0;
        a[*end]=ITEM;
        }
    else if( *beg==*end+1 || ( *beg==0 && *end==(n-1) ) ) //OVERFLOW
         {
         printf("\n OVERFLOW. DELETE SOME ELEMENTS: ");
         }
    else
        {  //Normal Situations
        *end=*end+1;
        a[*end]=ITEM;
        }
 
}
 
void DEL(int *a, int *beg, int *end)
{
     if( *beg==-1 )
     {
         printf("\n UNDERFLOW, ADD SOME ELEMENTS: ");
     }
     else if( *beg==*end ) //LAST ELEMENT
     {
          *beg=-1;
          *end=-1;
          printf("\n Element Deleted. \n");
     }
     else
     {
         *beg=*beg+1;
         printf("\n Element Deleted.\n");
     } 
}
 
void Display(int *a, int beg, int end)
{    printf("\n");
     int i=0;
     if (beg!=-1)
     for(i=beg; i<=end; i++)
     {
                printf(" %d ",a[i]);
     }
     else
     printf("\n NO ELEMENTS. \n");
     
     printf("\n");
              
}
 
void main()
{
  
int a[5];
int n=5; //max size
 
int beg=-1;
int end=-1;
 
int ch=1,ITEM;
 
while(ch!=4)
{
        printf("\n MENU: ");
        printf("\n 1. INSERT. ");
        printf("\n 2. DELETE. ");
        printf("\n 3. DISPLAY. ");
        printf("\n 4. EXIT. ");
        printf("\n Your Choice: ");
        
        scanf("%d",&ch);
        
        switch(ch)
        {
                  case 1: //INSERT
                          printf("\n ENTER ITEM: ");
                          scanf("%d",&ITEM);
                          
                          INS(a,n,&beg, &end, ITEM);
                          break;
                  
                  case 2: //DELETE
                          DEL(a,&beg, &end);
                          break;
                          
                  case 3: //DISPLAY
                          Display(a,beg,end);
                          break;
                  
                  case 4: printf("\n Exiting... ");
                          
        }
        
}              
  
}
 

OUTPUT:

MENU:
 1. INSERT.
 2. DELETE.
 3. DISPLAY.
 4. EXIT.
 Your Choice: 1

 ENTER ITEM: 24

 MENU:
 1. INSERT.
 2. DELETE.
 3. DISPLAY.
 4. EXIT.
 Your Choice: 1

 ENTER ITEM: 56

 MENU:
 1. INSERT.
 2. DELETE.
 3. DISPLAY.
 4. EXIT.
 Your Choice: 3

 24  56

 MENU:
 1. INSERT.
 2. DELETE.
 3. DISPLAY.
 4. EXIT.
 Your Choice: 2

 Element Deleted.

 MENU:
 1. INSERT.
 2. DELETE.
 3. DISPLAY.
 4. EXIT.
 Your Choice: 3

 56

 MENU:
 1. INSERT.
 2. DELETE.
 3. DISPLAY.
 4. EXIT.
 Your Choice: 4

 Exiting...

Comments