Monday, June 27, 2011

Starting with DS : Dynamic queue in c

//This is a C program for DYNAMIC STACK

#include<stdio.h>
#include<stdlib.h>
#include<conio.h>

#define NUM 5
 /*
*pos  :     we will use "pos" to point to top element in array, used in pop, value updates in push.
*arr  :    pointer to array which is allocated memory at run-time. Can be replaced by static array Just       comment out part where we are using malloc() for array in push().
num   :    "num" tells how much data is present.
*next :    "*next" is pointer to next stack element.
*/


typedef struct blk{
    int *pos;   
    int *arr;    
    int num;   
    struct blk *next;       
}stack;

//PUSH FUCTION : we pass head as pointer and data here.
stack* push(stack *head,int data){       stack* backup,*temp;                //head is stored in backup for returning new value when
                                        //stack is created for first time, else it contains old head value.
    for(backup = head;head && head->next != NULL;head = head->next);           
    //in loop above we check if head exists and go to last stack,which has next == null
    if(head == NULL || head->num == NUM){            //here we check if head(stack) exists or not or if array is
        temp = (stack *)malloc(1*sizeof(stack));    //full for current last stack. In both cases we create a new stack
        temp->arr = (int*)malloc(NUM*sizeof(int));    //as temp. temp points to NULL, since it will be last stack
        temp->next = NULL;                            //and has no data at present so num = 0 for temp.
        temp->num = 0;       
        temp->pos = temp->arr;                        //make pos to point to base of arr   
        if(backup == NULL)            //now if stack doesnt exist then assign temp as head of stack since we will add
            backup = head = temp;    //data to it. Also since this is new head store it in backup for returning purpose
        else if(head->num == NUM)        //else if array has filled for current last stack attach newly created temp stack
            head = head->next = temp;    //to stack by storing its address in next of curent last stack and make head
    }                                    //same as temp since we add data to newly created stack(temp) using head(head=temp)

    head->arr[head->num++] = data;    //add data and increment index.
   
    return backup;        //whether updated or not return backup which is collected as head in main program
}

stack* pop(stack *head,int *data){    //POP FUNCTION: we pass head as pointer and data as pointer here.
    stack* backup,*temp=NULL;

    backup = head;            //save backup as head;               
    *data = *head->pos;        //Read data at pos. pos is where queue starts
    head->pos++;            //since data at pos is read, now we will read next element next time so pos++
    --head->num;            //and take data in pointer passed, which is collected in main
    if(head->num == 0){        //here we check if array of current last stack has become empty, if so
        temp = head->next;
        free(head->arr);    //second last stack, use temp to make second last stack to point to NULL. Hence stack
        free(head);            //pointed by temp is now the last stack. Free memory of stack to be deleted.
        backup = temp;        //NOTE THAT stack is deleted only when no data is left
    }       
    return backup;    //whether updated or not return backup which is collected as head in main program
}

void display(stack *head,int clear){    //DISPLAY FUNCTION: its quite simple, clear is just a flag
    int *p,i;                            //to use system("cls"), to clear the screen. See main and u will
                                        //know, or execute the program
    if(clear)    system("cls");   
    printf("\nCURRENT STACK: ");
   
    if(!head)
        printf("DOESN'T EXIST");
   
    while(head){
        for(i=0,p=head->pos;i<head->num;p++,i++)
            printf("%d ",*p);
        head = head->next;
    }
}

void main(){
    stack *head=NULL;
    int data,run=1,cls=0;
    char ch;

    while(run){
        display(head,cls);
        cls = 0;                        //cls is made 0 so that if it was 1 (since we called display())
        printf("\n\nWHAT TO DO ?");
        printf("\n1. PUSH\n2. POP\n3. DISPLAY\n4. EXIT\nYOUR OPTION : ");
        cin>>ch;
        switch(ch){
            case '1':{
                printf("Enter number to push: ");
                cin>>data;
                system("cls");
                head = push(head,data);       
                break;
            }
   
            case '2':{
                system("cls");                               
                if( (head) ){                    //pop only if stack exists   
                    head = pop(head,&data);   
                    printf("DATA POPPED: %d",data);
                   
                }
                else
                    printf("CAN'T POP: NO DATA IN STACK");
                break;
            }
   
            case '3':{
                cls = 1;                    //make cls=1 since we are calling this function
                display(head,cls);
                break;
            }

            case '4':{
                run = 0;
                break;
            }
           
            default:{
                system("cls");
                printf("\nENTER VALID OPTION");
                break;
            }
        }
    }

    printf("\n\n\n");
    system("pause");
}

No comments:

Post a Comment