Thursday, 16 November 2017

Queue using linklist in C

#include <stdio.h>
#include <stdlib.h>
typedef struct queue
{
    int data;
    struct abc *next;
}node;
node *rear=NULL;
node *front=NULL;
void ins(int val)
{
    node *q;
    q=(node *)malloc(sizeof(node));
    q->data=val;
    if(rear==NULL)
    {
        rear=front=q;
    }
    else
    {
        rear->next=q;
        rear=q;
    }
}
void del()
{
    node *temp;
    temp=front;
    if(front==NULL)
    {
        printf("Queue Underflow");
    }
    else{
    front=front->next;
    free(temp);}
}
void display()
{
    node *temp;
    temp=front;
    if(front==NULL)
        printf("Queue is Empty");
    else{
    while(temp!=rear)
    {
        printf("%d ",temp->data);
        temp=temp->next;
    }
    printf("%d ",temp->data);
        }
}
int main()
{

    int val,ch;
    do
    {
        printf("\nEnter  1 to Insert\n2 to Delete\n3--Display\n4 to Exit\n Enter your choice\n");
        scanf("%d",&ch);
        switch(ch)
        {
            case 1:printf("Enter the value to be inserted\t");
                    scanf("%d",&val);
                    ins(val);
                    break;
            case 2:del();
                    break;
            case 3:display();
                    break;
        }
    }while(ch!=4);
    return 0;
}

All In One Blog

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation.

0 comments:

Post a Comment

 

Copyright @ 2015