This C++ program, using iteration, implements the list of elements removed from the stack in last in first out mode using a linked list. A linked list is an ordered set of data elements, each containing a link to its successor.
Here is the source code of the C++ program to display the list of elements removed from the stack. The C++ program is successfully compiled and run on DevCpp, a C++ compiler. The program output is also shown below.
/*
* C++ Program to Implement Queue using Linked List
*/
#include <iostream>
#include <stdio.h>
#include <conio.h>
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 queue\n";
}
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 queue\n";
cin>>n;
while (c < n)
{
cout<<"Enter the value to be entered into queue\n";
cin>>x;
push(x);
c++;
}
cout<<"\n\nRemoved Values\n\n";
while(true)
{
if (front != NULL)
cout<<remove()<<endl;
else
break;
}
getch();
}
Output Enter the number of values to be pushed into queue 6 Enter the value to be entered into queue 5 Enter the value to be entered into queue 4 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 1 Enter the value to be entered into queue 0 Removed Values 5 4 3 2 1 0
Sanfoundry Global Education & Learning Series – 1000 C++ Programs.
Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
advertisement
advertisement
If you wish to look at all C++ Programming examples, go to C++ Programs.
Next Steps:
- Get Free Certificate of Merit in Data Structure I
- Participate in Data Structure I Certification Contest
- Become a Top Ranker in Data Structure I
- Take Data Structure I Tests
- Chapterwise Practice Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
- Chapterwise Mock Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
Related Posts:
- Buy Computer Science Books
- Apply for Information Technology Internship
- Apply for Computer Science Internship
- Practice Design & Analysis of Algorithms MCQ
- Apply for Data Structure Internship