Pages

Custom Search

Thursday, November 3, 2011

Program to implement dynamically allocated Queue.


Dynamic allocation of memory is different from the static memory allocation. Static memory allocation is where we allocate more memory than required initially. It may be so that we may use the entire allocated memory or fall short of memory or use less memory and hence in any of the above cases memory is not managed properly.
So the solution for such a problem is the method of allocating memory during run time. There are four functions which help us to allocate, reallocate and deallocate memory as per our needs and requirements during run time. They are:
a.) Malloc    [Allocates a single block of memory]
b.) Calloc    [Allocates a contiguous block of memory initially filled with zero's]
c.) Realloc  [Reallocates memory whenever required, may be more or less]
d.) Free      [Deallocates previously allocated memory]

Source Code: 

 #include <stdio.h>  
 #include <stdlib.h>  
 #define realloc(p,n)             // Macro definition for realloc function  
 void insert();  
 void deleteq();  
 void display();  
 int capacity = 1;  
 int rear = -1;  
 int front = 0;  
 int main()  
 {  
     int *p;  
     int choice, ele;  
     p = (int*)malloc(sizeof(int)); //allocating memory to pointer  
     do  
     {  
         printf("\n\nOperations on stack: \n");  
         printf("\n1.) PUSH");  
         printf("\n2.) POP");  
         printf("\n3.) DISPLAY");  
         printf("\n4.) EXIT");  
         printf("\nEnter your choice: ");  
         scanf("%d", &choice);  
         switch(choice)  
         {  
             case 1: printf("\nEnter an element to push: ");  
                   scanf("%d", &ele);  
                   insert(&p, ele);  
                   break;  
             case 2: deleteq(&p);  
                   break;  
             case 3: display(p);  
                   break;  
             case 4: break;  
             default: printf("\nInvalid choice. Enter again...");  
         }  
     }while(choice!=4);  
     free(p);  
 }  
 void insert(int **p, int ele)  
 {  
     if(rear == capacity-1)  //checking if allocated memory is full  
     {    //if full reallocate memory by doubling capacity size  
         capacity*=2;  
         realloc(p, capacity);  
         printf("\nCapacity increased");  
     }  
     rear++;  
     *(*p+rear) = ele;  //storing value in pointer  
 //   printf("\nCapacity: %d", capacity);   remove comment to understand changes in the capacity values  
 }  
 void deleteq(int **p)  
 {  
     if(front > rear)  
     {  
         printf("\nQueue empty");  
     }  
     else  
     {  
         printf("\nDeleted element: %d", *(*p+front));  
         front++;  
         capacity--;  
         realloc(p, capacity);  
 //       printf("\nCapacity: %d", capacity);   remove comment to understand changes in the capacity values  
     }  
 }  
 void display(int *p)  
 {  
     int i;  
     if(front > rear)  
     {  
         printf("\nQueue is empty");  
     }  
     else  
     {  
         printf("\nContents of Queue are: ");  
         for( i=front ;i<=rear ;i++)  
         {  
             printf("%d  ", *(p+i));  
         }  
     }  
 }  

No comments:

Custom Search