Menu

Data Structures [ Lab Programs ]


C Program to implement Queue using Linked List(pointer)

Queue using Linked List
#include<stdio.h>
#include<stdlib.h>

struct node{
    int data;
    struct node*next;
}*head=NULL;

struct node *create(int value)
{
    struct node *temp;
    temp=(struct node*)malloc(sizeof(struct node));
    temp->data=value;
    temp->next=NULL;
    return temp;
}

void enqueue(int value)
{
    struct node *newnode, *temp;
    newnode=create(value);
    if(head==NULL)
    {
        head=newnode;
    }
    else
    {
        temp=head;
        while(temp->next!=NULL)
        {
            temp=temp->next;
        }
        temp->next=newnode;
    }
}

void dequeue()
{
    struct node *temp;
    if(head==NULL)
    {
        printf("Queue Underflow");
    }
    else
    {
        temp=head;
        head=head->next;
        free(temp);
    }
}

void display()
{
    struct node *temp;
    if(head==NULL)
    {
        printf("Queue is empty");
    }
    else
    {
        temp=head;
        while(temp->next!=NULL)
        {
            printf("%d, ",temp->data);
            temp=temp->next;
        }
        printf("%d",temp->data);
    }
}

void main()
{
    int ch,pos,value;
    do
    {
       printf("\n1. Insert\n2. Delete\n3. Display\n4. Exit");
        printf("\nEnter your choice:   ");
        scanf("%d",&ch);
        switch(ch)
        {
        case 1: printf("Enter data to insert:  ");
                scanf("%d",&value);
                enqueue(value);
                break;
        case 2: dequeue();
                break;
        case 3: display();
                break;
        case 4:break;
        default: printf("\nyour choice is wrong!..");
        }
    }while(ch!=4);
} 

OUTPUT
1. Insert
2. Delete
3. Display
4. Exit
Enter your choice:   1
Enter data to insert:  10
1. Insert
2. Delete
3. Display
4. Exit
Enter your choice:   1
Enter data to insert:  20
1. Insert
2. Delete
3. Display
4. Exit
Enter your choice:   1
Enter data to insert:  30
1. Insert
2. Delete
3. Display
4. Exit
Enter your choice:   3
10, 20, 30
1. Insert
2. Delete
3. Display
4. Exit
Enter your choice:   2
1. Insert
2. Delete
3. Display
4. Exit
Enter your choice:   3
20, 30
1. Insert
2. Delete
3. Display
4. Exit
Enter your choice:   2
1. Insert
2. Delete
3. Display
4. Exit
Enter your choice:   3
30
1. Insert
2. Delete
3. Display
4. Exit
Enter your choice:   2
1. Insert
2. Delete
3. Display
4. Exit
Enter your choice:   2
Queue Underflow
1. Insert
2. Delete
3. Display
4. Exit
Enter your choice:   3
Queue is empty
1. Insert
2. Delete
3. Display
4. Exit
Enter your choice:   4

Related Content :

1. Write a program that uses functions to perform the following operations on singly linkedlist.:
   i) Creation    ii) Insertion    iii) Deletion    iv) Traversal    View Solution

2. Write a program that uses functions to perform the following operations on doubly linkedlist.:
   i) Creation    ii) Insertion    iii) Deletion    iv) Traversal    View Solution

3. Write a program that uses functions to perform the following operations on circular linkedlist.:
   i) Creation    ii) Insertion    iii) Deletion    iv) Traversal    View Solution

4. Write a program that implement Stack (its operations) using Array    View Solution

5. Write a program that implement Stack (its operations) using Linked List (Pointer)    View Solution

6. Write a program that implement Queue(its operations) using Array    View Solution

7. Write a program that implement Queue (its operations) using Linked List (Pointer)    View Solution

8. Write a program that implements Radix sorting methods to sort a given list of integers in ascending order    View Solution

9. Write a program that implements Heap sorting methods to sort a given list of integers in ascending order    View Solution

10. Write a program that implements Shell sorting methods to sort a given list of integers in ascending order    View Solution

11. Write a program that implements Tree sorting methods to sort a given list of integers in ascending order    View Solution

12. Write a program to implement the tree traversal methods using Recursive    View Solution

13. Write a program to implement the tree traversal methods using Non Recursive    View Solution

14. Write a program to implement Binary Search Tree (its operations)    View Solution

15. Write a program to implement AVL Tree (its operations)    View Solution

16. Write a program to implement Red - Black Tree (its operations)    View Solution

17. Write a program to implement the graph traversal methods (Breadth First Search)    View Solution

18.Write a program to implement the graph traversal methods (Depth First Search)    View Solution

19. Write a program to implement the following Hash Functions: i) Division Method, ii) Multiplication Method, iii) Mid-square Method, iv) Folding Method    View Solution