Sunday, 16 October 2011

queue implementation of list


PROGRAM:

/*queue implementation of list*/
#include<stdio.h>
#include<stdlib.h>
struct node
{
        int info;
        struct node *next;                       /*creation of node*/
};
struct node *head,*p,*old,*q;
int s[10],rear=-1,front=-1;
void enqueue(int ele)
{
            if(rear>10)
            printf("The stack is full\n");                /*to check the stack is full or not*/
            else
            {
                        rear=rear+1;
                        s[rear]=ele;
                        if(front==-1)
                        front++;
            }
}
void dequeue()
{
            int temp;
            if(front==-1)
            {
                        printf("The stack is empty\n");                        /*to check whether the stack is empty or not*/
                        temp=s[front];
            }
            else
            if(front==rear)
            {
                        front=-1;
                        rear=-1;
            }
            else
            front++;
}
void traverse()
{
            int i;
            for(i=rear;i>=front;i--)
            {
                        printf("%d\n",s[i]);
            }
}
void peek()
{
            printf("The first element in the stack is:%d\n",s[rear]);          /*to print the first element in the list*/
}
void display()
{
        p=head;
        while(p!=NULL)
        {
            printf("%d->",p->info);
            p=p->next;                                          /*to display the list*/
        }
            while(p==NULL)
            {
                        printf("NULL");
                        break;
            }
        printf("\n\n");
}
void enque()
{
        int ele;
        struct node *n;
        n=(struct node*)malloc(sizeof(struct node));
        printf("Enter the inserting element:");                             /*to enqueue in the list*/
        scanf("%d",&ele);
        n->info=ele;
        n->next=head;
        head=n;
}
void deque()
{
        p=head;old=head;
        while(p->next!=NULL)                                      /*to dequeue in the list*/
        {
            old=p;
            p=p->next;
        }
        free(p);
        old->next=NULL;
}
main()
{
            int x,c,a,m,i,e;
            do
            {
                        printf("queue implementation of list\n");
                        printf("1)Using Arrays\n2)Using Linked List\n3)stop\t");
                        scanf("%d",&a);
                        switch(a)
                        {
                                    case 1: printf("array implementation");                       /*for array implementation*/
                                                printf("\nEnter no. of elements to be inserted:");
                                                scanf("%d",&m);
                                                printf("Enter the element to be inserted:");
                                                for(i=0;i<m;i++)
                                                {
                                                            scanf("%d",&e);
                                                enqueue(e);
                                                }
                                                do
                                                {         
                                                            printf("the elements in the list are:");
                                                            traverse();
                                                            printf("enter your choice \n");
                                                            printf(" 1)enqueue  2)dequeue 3)traverse 4)first 5)exit ");      /*to get the choice of operation*/
                                                            scanf("%d",&c);
                                                            switch(c)        
                                                            {
                                                                        case 1: printf("Enter the element to be inserted:");
                                                                                    scanf("%d",&x);
                                                                                    enqueue(x);
                                                                                    traverse();
                                                                                    break;
                                                                        case 2:dequeue();
                                                                               traverse();
                                                                               break;
                                                                        case 3:traverse();
                                                                                    break;
                                                                        case 4:peek();
                                                                                    break;
                                                            }
                                                }
                                                while(c<5);
                                                break;
                                    case 2: printf("link list implementation");                   /*for linklist implementation*/
                                                int c,n,i;
                                    printf("\nEnter the no. of nodes:");
                                                scanf("%d",&n);
                                                printf("Enter the values:",n);
                                                head=(struct node*)malloc(sizeof(struct node));
                                    scanf("%d",&head->info);
                                                head->next=NULL;
                                    old=head;
                                    for(i=1;i<n;i++)
                                    {
                                                p=(struct node*)malloc(sizeof(struct node));
                                                scanf("%d",&p->info);
                                                p->next=head;
                                                head=p;
                                    }
                                                do
                                    {
                                                printf("the values in list are");
                                                            display();
                                                            printf("enter your choice\n");
                                                printf("1)enqueue 2)dequeue 3)traverse 4)exit \n");               /*to get the choice of operation*/
                                                scanf("%d",&c);
                                                switch(c)
                                                {
                                                            case 1: enque();
                                                                    display();
                                                                                    break;
                                                            case 2: deque();
                                                                                    display();
                                                                                    break;
                                                            case 3: display();
                                                                        break;
                                                            }
                                                }
                                    while(c<4);
                                    break;
                        }
            }
            while(a<3);
}


OUTPUT:

queue implementation of list
1)Using Arrays
2)Using Linked List
3)stop  1
array implementation
Enter no. of elements to be inserted:3
Enter the element to be inserted:1
2
3
the elements in the list are:3
2
1
enter your choice
 1)enqueue  2)dequeue 3)traverse 4)first 5)exit 1
Enter the element to be inserted:4
4
3
2
1
the elements in the list are:4
3
2
1
enter your choice
 1)enqueue  2)dequeue 3)traverse 4)first 5)exit 2
4
3
2
the elements in the list are:4
3
2
enter your choice
 1)enqueue  2)dequeue 3)traverse 4)first 5)exit 3
4
3
2
the elements in the list are:4
3
2
enter your choice
 1)enqueue  2)dequeue 3)traverse 4)first 5)exit 4
The first element in the stack is:4
the elements in the list are:4
3
2
enter your choice
 1)enqueue  2)dequeue 3)traverse 4)first 5)exit 5
queue implementation of list
1)Using Arrays
2)Using Linked List
3)stop  2
link list implementation
Enter the no. of nodes:3
Enter the values:1
2
3
the values in list are3->2->1->NULL

enter your choice
1)enqueue 2)dequeue 3)traverse 4)exit
1
Enter the inserting element:4
4->3->2->1->NULL

the values in list are4->3->2->1->NULL

enter your choice
1)enqueue 2)dequeue 3)traverse 4)exit
2
4->3->2->NULL

the values in list are4->3->2->NULL

enter your choice
1)enqueue 2)dequeue 3)traverse 4)exit
3
4->3->2->NULL

the values in list are4->3->2->NULL

enter your choice
1)enqueue 2)dequeue 3)traverse 4)exit
4
queue implementation of list
1)Using Arrays
2)Using Linked List
3)stop 3

No comments:

Post a Comment