4.6 Implement Queue using Stack in C| Data Structures Tutorials

preview_player
Показать описание
Discussed how to implement queue using stacks with example. I have written a C program for implementation of queue using stacks

******************************************
See Complete Playlists:

******************************************

Connect & Contact Me:

#jennyslectures
#datastructures
#queue
#ugcnet
#computerscience
#course
Рекомендации по теме
Комментарии
Автор

You can also write the for loop as:
for(i=0;i<count;i++)
Because count is storing the total number of values in the stack1 but the index of the stack1 starts with zero so the condition will be i is less than count
(In the display function)

dracox-
Автор

a few additions to this collection plz
reverse queues
maze problem
priority queues
Thank you for your efforts.
Would appreciate it if you could do videos on these also sis !!!

namratha
Автор

if you are facing the the garbage issue when you dequeue .... then refer the below program

//write program for the implement queue using stack
#include<stdio.h>
#include<stdlib.h>
#define N 5
//defining s1, s2 for array and the top1, top2 are for top pointer of the stack and count is used for the counting the element.
int s1[N];
int s2[N];
int top1 = -1;
int top2 = -1;
int count = 0;
//write the enqueue operation
void enqueue(){
int data;
if(top1 == N-1){
printf("overflow\n");
}
else{
printf("Enter the data: ");
scanf("%d", &data);
push1(data);
count++;
}
}
//write the dequeue operation for the stack
void dequeue(){
int i;
int a;
int b;
int x;
if(top1 == -1){
printf("the queue is empty\n");
}
else{
for(i = 0 ; i<count ; i++ ){
a = pop1();
push2(a);
}
b = pop2();
printf("the dequeued element is : %d\n", b);
count--;
for(i = 0; i<count ; i++){
x = pop2();
push1(x);
}
}

}
//write the pop operation
int pop1(){

return s1[top1--];

}
//write the pop operation
int pop2(){

return s2[top2--];

}
//write the pushw operation for the stack2
void push2(int a){
if(top1 == N-1){
printf("The queue is full \n");
}
else{
top2++;
s2[top2] = a;
}
}
//write the push1 operation for the stack1
void push1(int a){
if(top1 == N-1){
printf("The queue is full \n");
}
else{

top1++;
s1[top1]=a;
}
}

//display operation for the queue usig stack 1
void display(){
int i;
if(top1==-1){
printf("there is no element in the queue\n");
}
else{
for(i = 0 ; i<= top1 ; i++){
printf("%d\t", s1[i]);
}
printf("\n");
}
}

int main(){
int choice;

while(1){
printf("Enter the choice: ");
scanf("%d", &choice);
switch(choice){
case 1:
{
enqueue();
break;
}
case 2:
{
dequeue();
break;
}

case 3:
{
display();
break;
}
case 4:
{
exit(0);
}
default:
{
printf("invalid choice\n");
}
}
}
return 0;
}

rrfix
Автор

AMAZING explanation. explained literally EVERYTHING in an easy to understand manner. Thank you please keep teaching!

SussyBaka-cixi
Автор

Ma'am your lectures are awesome . Please do make videos on implementing DSA using C++ STL . No one has taught that on YT yet .

BharadwajBandaruRA
Автор

Thnku so much mam today was my exam and I did it in just 20 mins all bcoz of u.... Really happy today thnku somuch... I never comment on any of the video bt it's my first tym I'm doing bcoz I thought to express myself

Prerana
Автор

Thank you, ma'am. Your lectures are very easy to understand. Can you please make lectures on priority queue, double-ended queue, input restricted, output restricted queue also.

aayushisingh
Автор

If we take i<count + 1...then
We incremented count from 0 that is initial count = 0 .
And top1 is incremented from ....-1
That is initially top1=..-1..
Soo if we take i < count + 1....
Then loop will go one iteration ahead .
So loop will print one garbage value
that we don't want to print ....

If we only take i < count then okkk...
It will work fine 👍 .
But we can't take i < count + 1

"i < top1 + 1" = "i < Count" != "i < Count + 1"

saurabhkoli
Автор

if you can make few lectures on priority queue and multi queue, that would be better for us and one more thing that your lectures are supbb.

raxxdinus
Автор

aaaa....I'm watching you lactures to for my educative interview and you are recommending
😄😄

usmanghani
Автор

You are amazing as usual. Please make video on priority queue using linked list

Varun
Автор

🐻‍❄WE ARE USING TOP1 INSTEAD OF COUNT IN DISPLAY METHOD:
If you used count in display(), it could be misleading because count represents the total number of elements in the queue, including those that may have been moved to S2 during dequeue operations. But you only want to print the elements in S1, not the entire queue.

Yohelloworld
Автор

no other video on yt explains the so well! :)

suyash.
Автор

The problem with using count is if we perform equal number of enqueue and dequeue operations then count=0.for(i=0;i≤count;i++) the condition 0≤0 is satisfied and this prints s1[0] which does not contain any element(prints some garbage value)but if we use top1=-1 then for(i=0;i≤top1;i++) the condition fails as 0 is not less than or equal to -1(0≤-1) and it does not display anything which is right.correct me if I'm wrong.

RaviTeja-ofgp
Автор

Small mistake is that....count value will increment, even after queue is
So to overcome this, create a condition say if(count<n) count++;
Is this correct??

tharunkumar
Автор

Thank you mem for your outstanding lecture 😊😊

Vikashyadav
Автор

Superb lecture ma'am ...I like it ma'am.... The way you explain each and every line is superb.... keep helping us....🙂🙂

himanshukaushal
Автор

mam you explain it perfectly.. mam will u please make a short videos on linear search, binary search & a bit detail of hasing .. there are 2 old videos in hashing.. tht includes linear probing quadratic.. i dont know much about hashing.. is there something more .. basic hash function or so.. if u can manage some time to makes these video, i would be so thankful.. actually search and sort are important too right. there are videos of sorting.. search is missing.. so please

aayushjaswani
Автор

Thanks mam because of u i have completed my half of data structures thanks a lot mam.

SHIVAMkr
Автор

yes we can use count in display funcrion just have to write for(int i = 0; i <= count -1 ; i++)

harshpanchal