Implement Stack using Linked List in C++

In this post we will write a program to implement Stack using Linked List. Let’s first understand what is a Stack: Stack: A stack is an abstract data type that serves as a collection of elements, with two principal operations: push, which adds an element to the collection, and pop, which removes the most recently … Read more

Merge two sorted Linked Lists

Here we will write a function merge(list 1, list 2) which will take two sorted linked list as argument and it will merge them to form a new linked list in ascending order. For example we will have two linked lists 2 -> 5 -> 9 and  3 -> 7 -> 11 and the function will … Read more

Write a Program to reverse a Linked list

There are many ways to reverse a linked list, but here we’ll discuss 2 methods to do so. 1.  Iterative Method: This is the most simple method to reverse a linked list. This is the first thought that would come to anybody’s mind if he/she thinks on how to reverse a linked list. Let’s take … Read more

Program to implement Queue using Linked List C++

Today we are going to write a program to implement queue using linked list C++, let’s start with what is queue:

 

What is QUEUE?

implement queue using linked list C++

 

 

A Queue is a particular kind of abstract data type or collection in which the entities in the collection are kept in order and the principal (or only) operations on the collection are the addition of entities to the rear terminal position, known as enqueue, and removal of entities from the front terminal position, known as dequeue. This makes the queue a First-In-First-Out (FIFO) data structure.

 

Program to implement Queue using Linked List C++

#include<iostream>
using namespace std;

struct node
{
    int data;
    node *next;
}*front = NULL,*rear = NULL,*p = NULL,*np = NULL;

void push(int x)
{
    np = new node;
    np->data = x;
    np->next = NULL;
    if(front == NULL)
    {
        front = rear = np;
        rear->next = NULL;
    }
    else
    {
        rear->next = np;
        rear = np;
        rear->next = NULL;
    }
}
int remove()
{
    int x;
    if(front == NULL)
    {
        cout<<"empty queuen";
    }
    else
    {
        p = front;
        x = p->data;
        front = front->next;
        delete(p);
        return(x);
    }
}
int main()
{
    int n,c = 0,x;
    cout<<"Enter the number of values to be pushed into queuen";
    cin>>n;
    while (c < n)
    {
    cout<<"Enter the value to be entered into queuen";
    cin>>x;
        push(x);
        c++;
     }
     cout<<"nnRemoved Valuesnn";
     while(true)
     {
        if (front != NULL)
            cout<<remove()<<endl;
        else
            break;
     }
     return 0;
}

 

OUTPUT:

Enter the number of values to be pushed into queue
5
Enter the value to be entered into queue
5
Enter the value to be entered into queue
3
Enter the value to be entered into queue
2
Enter the value to be entered into queue
9
Enter the value to be entered into queue
1


Removed Values

5
3
2
9
1

--------------------------------
Process exited after 16.89 seconds with return value 0
Press any key to continue . . .

 

Program to implement Linked List in C C++

Following is the program to implement linked list in c  c++ PROGRAM: #include<stdio.h> #include<malloc.h> #include<process.h> struct node { int info; struct node *next; }; typedef struct node sn; void insertatbeg(sn **,int); void insertatend(sn **,int); void insertatpos(sn **,int,int); void traverse(sn *); int delfirst(sn *); int dellast(sn *); int delpos(sn *,int); int count(sn *); int main() { … Read more