facebook like button

Showing posts with label C-free. Show all posts
Showing posts with label C-free. Show all posts

17 August, 2011

Program 77: Queue implementation on linked lists general case

The need:
    This program shows how one can implement concept of queue in linked lists. This is extension of previous program. This program is analogous to program75 of this blog.
The code:

------------------------------------------------------
/*
This is a program showing the queue implimentation on linked list
program writtem by RP Singh
compiled and tested on C-free4.0 standard
*/
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct node1
{
    int item;
    struct node1 *next;
};
typedef struct node1 node;        //defining datatype node

struct q1
{
    node *front;
    node *rear;
    int length;
};
typedef struct q1 queue;        //defining queue datatype

void enqueue(node *, queue *);
node *dequeue(queue *);
int queue_length(queue *);
void view_queue(queue *);
node *create_node(void);
void fill_node(node *);


int main()
{
    int i,j;
    node *current_node;
    queue que;                    //local queue
    que.front=NULL;
    que.rear=NULL;
    que.length=0;
    printf("This is a demo program to show working of queues");
    anchor:                    //anchor is a label
    printf("\n\nyou have following options\n");
    printf("1. enqueue an item\n2. dequeue an item\n3. view queue\n");
    printf("4. count items in queue\n5. exit program\n\n");
    scanf("%d",&j);
    switch(j)
    {
        case 1:
            printf("\nEnter a number to be enqueued =>\t");
            current_node=create_node();
            fill_node(current_node);
            enqueue(current_node, &que);
            goto anchor;
        case 2:
            current_node = dequeue(&que);
            if(current_node)
            printf("The item %d dequeued successfully\n",current_node->item);
            goto anchor;
        case 3:
            view_queue(&que);
            goto anchor;
        case 4:
            printf("total items in the queue are %d\n",que.length);
            goto anchor;
        case 5:
            printf("Thank you\n");
            exit(0);
            goto anchor;
        default:
            printf("Invalid choice...!!!\n try choosing again\n\n");
            goto anchor;
    }
  
    return 0;
}

void enqueue(node *p,queue *q)                    //definition of enqueue function
{
    if(q->rear==NULL&&q->front==NULL)
    {
        q->rear=p;
        q->front=p;
    }
    else
    {
        q->rear->next=p;
        q->rear=p;
    }
    q->length++;
    printf("item %d enqueued successfully.\n",p->item);
}

node *dequeue(queue *q)                    //definition of dequeue function
{
    node *temp=NULL;
    if(q->rear==NULL&&q->front==NULL)        //this is the case when queue is empty
    {
        printf("queue is empty hence can not be dequeued\n");
        return temp;
    }
    else if(q->rear==q->front&&q->front!=NULL)    //this is the case when queue has only one node
    {
        temp=q->front;
        q->front=NULL;
        q->rear=NULL;
    }
    else
    {
        temp=q->front;
        q->front=q->front->next;
    }
    q->length--;
    return temp;
}

void view_queue(queue *q)
{
    node *temp_front;
    temp_front=q->front;
    if(q->length==0)
    {
        printf("\nThe queue is empty...!!!\n");
        return;
    }
    printf("The queue is\n");
    while(temp_front!=NULL)
    {
        printf("%d -> ",temp_front->item);
        temp_front=temp_front->next;
    }
    printf("\n");
    return;
}

node *create_node()                    //function to create a blank node
{
    node *temp;
    temp=(node*)malloc(sizeof(node));
    temp->next=NULL;
    return temp;
}

void fill_node(node *p)                    //function to fill a blank node with values taken from user
{
    int i;
    scanf("%d",&i);                    //this is the value taken from user
    p->item=i;
}

------------------------------------------------------

10 August, 2011

Program 72: Queue implementation on array

The need:
    To show how queue concept can be implemented on an array in the simplest way possible.
The code:
-----------------------------------------------------------

/*
This is a program showing the queue implimentation on integer array
We can have array implementation on array of any datatype including derived datatypes
This program does not include any user defined function
In this front and rear are taken as array indices
program writtem by RP Singh
compiled and tested on C-free4.0 standard
*/

#define MAX 10
#include<stdio.h>
#include<stdlib.h>

int main()
{
    int i=0,arr[MAX];
    char c='y';
    int front=0,rear=-1,current_item;        //I follow the convention of takingthese values of front and rear when queue is empty
    printf("Enter 3 items into queue\n");
    while(c=='y'||c=='Y')
    {
        printf("\nEnter integer %d\t=>\t",++i);
        scanf("%d",&current_item);                    //this is the value taken from user
        if(rear==MAX-1)
        printf("cannot enqueue because the array end has been reached.\n");
        else
        arr[++rear]=current_item;                    //enqueing an item
        printf("item %d inserted into queue successfully\n\n",current_item);
        printf("Enter press y to enqueue more items. else press any other key.\n");
        fflush(stdin);
        c=getchar();
    }
    printf("all items successfully entered into queue\n");
    printf("value of front = %d\t rear = %d\n",front,rear);
    printf("length of queue is %d\n",rear-front+1);
    printf("\n\npress any key to dequeue and see all values you entered\n");
    fflush(stdin);
    getchar();
    printf("all items you entered are listed below\n");
    while(front<=rear)
    {
        printf("%d\n",arr[front++]);                    //dequeing an item
    }
    return 0;
}

-----------------------------------------------------------
Remarks:
This is a simple program showing implementation of queue concept on array. For simplicity I have taken a simple integer array. I also have not used any UDFs here. I just wanted to show that queue is an abstract entity, we dont even need enqueue and dequeue to implement that on simplest level. The only thing to remember is the concept of queue.