You are currently viewing Easy Linear Queue using Linked List Code

Easy Linear Queue using Linked List Code

Code for implementing Linear Queue using LinkedĀ  List

For learning pointer, dynamic memory allocation, linked list, you should go through this code. This is Code for implementing Linear Queue using Linked List.

In C code practice, this Code for implementing Linear Queue using Linked List logic plays a very important role.

So I have designed this code as easy as possible for the students. They will be able to understand each and every step very easily. 

For more code click here. For video tutorial on several topics click here

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

typedef struct Queue
{
    int val;
    struct Queue *next;
}qu;

qu *insert(qu* , int);
int dele(qu **);
int peek(qu *);
int isempty(qu *);

void disp(qu *);

int main()
{
    qu *head=NULL;
    int v,ch;
    while(1)
    {
        printf("\n\n1) Insert \n\n2) Delete \n\n3) Peek \n\n4) Display \n\n5) Exit");
        printf("\n\nEnter your choice : ");
        scanf("%d",&ch);
        switch(ch)
        {
            case 1: printf("\nEnter the value : ");
                    scanf("%d",&v);
                    head=insert(head,v);
                    break;
            case 2: if(!isempty(head))
                    {
                        v=dele(&head);
                        printf("\nDeleted element is %d ",v);
                    }
                    else
                        printf("\nNo value in the queue");
                    break;
            case 3: if(!isempty(head))
                    {
                        v=peek(head);
                        printf("\nFront element is %d ",v);
                    }
                    else
                        printf("\nNo value in the queue");
                    break;
            case 4: if(!isempty(head))
                    {
                        printf("\nValues in the queue are : \n");
                        disp(head);
                    }
                    else
                        printf("\nNo value in the queue");
                    break;
            case 5: exit(0);
            default: printf("\nWrong Choice");
        }
        
    }
    
    return 0;
}

qu *insert(qu *h,int g)
{
    qu *ptr,*temp;
    temp=(qu *)malloc(sizeof(qu));
    temp->val=g;
    temp->next=NULL;
    if(h==NULL)
    h=temp;
    else
    {
        ptr=h;
        while(ptr->next!=NULL)
        {
            ptr=ptr->next;
        }
        ptr->next=temp;
    }
    return h;
}

int dele(qu **h)
{
    int m;
    qu *temp;
    temp=*h;
    m=(*h)->val;
    (*h)=(*h)->next;
    free(temp);
    return m;
}

int peek(qu *h)
{
    return h->val;
}

int isempty(qu *h)
{
    if(h==NULL)
    return 1;
    else
    return 0;
}

void disp(qu *h)
{
    qu *ptr;
    ptr=h;
    while(ptr!=NULL)
    {
        printf("|%3d|",ptr->val);
        ptr=ptr->next;
    }
}
				
			

Leave a Reply