Queue Implementation with Linked List (C++) PDF

Document Details

TruthfulCopernicium

Uploaded by TruthfulCopernicium

Ibn Sina University for Medical Sciences

Tags

data structures queue linked list c++

Summary

This document provides C++ code for implementing a queue data structure using a linked list. The code includes functions for enqueueing and dequeueing elements, as well as a function for peeking at the front element. It demonstrates a basic implementation and is useful for learning about data structures and their application.

Full Transcript

\#include using namespace std; struct Node { int data; struct Node \*next; }; Node *front=NULL; Node* rear=NULL; int peek() { return front-\>data; } void enqueue(int data) { Node\* newNode = new Node; newNode-\>data = data; newNode-\>next = NULL; if(front == NULL) { front =...

\#include using namespace std; struct Node { int data; struct Node \*next; }; Node *front=NULL; Node* rear=NULL; int peek() { return front-\>data; } void enqueue(int data) { Node\* newNode = new Node; newNode-\>data = data; newNode-\>next = NULL; if(front == NULL) { front = newNode; rear = newNode; } else { rear->next = newNode; rear = newNode; } } int dequeue() { Node \* temp = front; int x = front-\>data;// or int x = temp -\> data; front = front -\> next; if(front == NULL) rear = NULL; delete temp; return x; } void printQueue(){ Node\* curr = front; while(curr-\>next != NULL) { cout \data \next; } cout data

Use Quizgecko on...
Browser
Browser