logo
  • Home
  • Test & Rank
  • About
  • Training
  • Programming
  • CS
  • IT
  • IS
  • ECE
  • EEE
  • EE
  • Civil
  • Mechanical
  • Chemical
  • Metallurgy
  • Instrumentation
  • Aeronautical
  • Aerospace
  • Biotechnology
  • Mining
  • Marine
  • Agriculture
  • MCA
  • BCA
  • Internship
  • Jobs
  • Contact

Questions & Answers

C Interview Questions
C++ Questions
Linux MCQs
C# Quiz
Java MCQs
JavaScript MCQs
SAN Questions
PHP Questions
Python Quiz

Computer Science Questions

Operating System Quiz
Computer Architecture MCQs
Software Architecture MCQs
Software Engineering MCQs
Artificial Intelligence MCQs
LISP Programming MCQs
Database Management MCQs
Computer Network MCQs
Microprocessor MCQs

C Programming Examples

Simple C Programs
C - Arrays
C - Matrix
C - Strings
C - Bitwise Operations
C - Linked Lists
C - Stacks & Queues
C - Searching & Sorting
C - Trees
C - Strings
C - File Handling
C - Mathematical Functions
C - Puzzles & Games
C Programs - Recursion
C Programs - No Recursion

Java Algorithms

Java - Numerical Problems
Java - Combinatorial Problems
Java - Graph Problems
Java - Hard Graph Problems
Java - Computation Geometry
Java - Sets & Strings
Java - Data-Structures
Java - Collection API Problems

C++ Algorithms

C++ - Numerical Problems
C++ - Combinatorial Problems
C++ - Graph Problems
C++ - Hard Graph Problems
C++ - Computation Geometry
C++ - Sets & Strings
C++ - Data-Structures
C++ - STL Library

C Algorithms

C - Numerical Problems
C - Combinatorial Problems
C - Graph Problems
C - Hard Graph Problems
C - Computation Geometry
C - Sets & Strings
C - Data-Structures

« Prev Page
Next Page »

C++ Program to Implement Queue using Linked List

Posted on July 29, 2013 by Manish
This C++ program, using iteration, implements the list of elements removed from the queue in first 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 queue. The C++ program is successfully compiled and run on DevCpp, a C++ compiler. The program output is also shown below.

  1. /*
  2.  * C++ Program to Implement Queue using Linked List
  3.  */
  4. #include<iostream>
  5. #include<stdio.h>
  6. #include<conio.h>
  7. using namespace std;
  8. struct node
  9. {
  10.     int data;
  11.     node *next;
  12. }*front = NULL,*rear = NULL,*p = NULL,*np = NULL;
  13. void push(int x)
  14. {
  15.     np = new node;
  16.     np->data = x;
  17.     np->next = NULL;
  18.     if(front == NULL)
  19.     {
  20.         front = rear = np;
  21.         rear->next = NULL;
  22.     }
  23.     else
  24.     {
  25.         rear->next = np;
  26.         rear = np;
  27.         rear->next = NULL;
  28.     }
  29. }
  30. int remove()
  31. {
  32.     int x;
  33.     if(front == NULL)
  34.     {
  35.         cout<<"empty queue\n";
  36.     }
  37.     else
  38.     {
  39.         p = front;
  40.         x = p->data;
  41.         front = front->next;
  42.         delete(p);
  43.         return(x);
  44.     }
  45. }
  46. int main()
  47. {
  48.     int n,c = 0,x;
  49.     cout<<"Enter the number of values to be pushed into queue\n";
  50.     cin>>n;
  51.     while (c < n)
  52.     {
  53. 	cout<<"Enter the value to be entered into queue\n";
  54. 	cin>>x;
  55.         push(x);
  56.         c++;
  57.      }
  58.      cout<<"\n\nRemoved Values\n\n";
  59.      while(true)
  60.      {
  61.         if (front != NULL)
  62.             cout<<remove()<<endl;
  63.         else
  64.             break;
  65.      }
  66.      getch();
  67. }

advertisement
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.

advertisement
If you wish to look at all C++ Programming examples, go to C++ Programs.
« Prev Page - C++ Program for Topological Sorting in Graphs
» Next Page - C++ Program to Implement Stack using Linked List

« C++ Program to Find the maximum subarray sum O(n^2) time(naive method)
C++ Program to Implement Stack using Linked List »

Deep Dive @ Sanfoundry:

  1. C Programming Examples using Recursion
  2. C Programming Examples on Trees
  3. C Programming Examples
  4. C Programming Examples without using Recursion
  5. Java Programming Examples on Data-Structures
  6. C Programming Examples on Data-Structures
  7. C++ Programming Examples on Data-Structures
  8. C Programming Examples on Stacks & Queues
  9. C# Programming Examples on Data Structures
  10. C Programming Examples on Linked List
Manish Bhojasia
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn | Facebook | Twitter

Best Careers

Developer Tracks
SAN Developer
Linux Kernel Developer
Linux Driver Developer
Linux Network Developer

Live Training Photos
Mentoring
Software Productivity
GDB Assignment
Sanfoundry is No. 1 choice for Deep Hands-ON Trainings in SAN, Linux & C, Kernel Programming. Our Founder has trained employees of almost all Top Companies in India such as VMware, Citrix, Oracle, Motorola, Ericsson, Aricent, HP, Intuit, Microsoft, Cisco, SAP Labs, Siemens, Symantec, Redhat, Chelsio, Cavium, ST-Micro, Samsung, LG-Soft, Wipro, TCS, HCL, IBM, Accenture, HSBC, Mphasis, Tata-Elxsi, Tata VSNL, Mindtree, Cognizant and Startups.

Best Trainings

SAN I - Technology
SAN II - Admin
Linux Fundamentals
Advanced C Training
Linux-C Debugging
System Programming
Network Programming
Linux Threads
Kernel Programming
Kernel Debugging
Linux Device Drivers

Best Reference Books

Computer Science Books
Algorithm & Programming Books
Electronics Engineering Books
Electrical Engineering Books
Chemical Engineering Books
Civil Engineering Books
Mechanical Engineering Books
Industrial Engineering Books
Instrumentation Engg Books
Metallurgical Engineering Books
All Stream Best Books

Questions and Answers

1000 C Questions & Answers
1000 C++ Questions & Answers
1000 C# Questions & Answers
1000 Java Questions & Answers
1000 Linux Questions & Answers
1000 Python Questions
1000 PHP Questions & Answers
1000 Hadoop Questions
Cloud Computing Questions
Computer Science Questions
All Stream Questions & Answers

India Internships

Computer Science Internships
Instrumentation Internships
Electronics Internships
Electrical Internships
Mechanical Internships
Industrial Internships
Systems Internships
Chemical Internships
Civil Internships
IT Internships
All Stream Internships

About Sanfoundry

About Us
Copyright
Terms
Privacy Policy
Jobs
Bangalore Training
Online Training
Developers Track
Mentoring Sessions
Contact Us
Sitemap
© 2011 Sanfoundry. All Rights Reserved.