0% found this document useful (0 votes)
34 views64 pages

DSA Stack and Queues + Extras

Uploaded by

vedant bhatnagar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views64 pages

DSA Stack and Queues + Extras

Uploaded by

vedant bhatnagar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 64

DSA SEARCHING

#include <stdio.h>

#include<math.h>

int main()

float n,i,width,height;

scanf("%f",&n);

int count=0;

for(i=0;i<n;i++)

scanf("%f %f",&width,&height);

if(width/height>=1.6 && width/height<=1.7)

++count;

else if(height/width >=1.6 && height/width<=1.7)

++count;

printf("%d",count+1);

return 0;

}
#include <stdio.h>

void thirdLargest(int arr[],int arr_size)

int j,k,temp;

for(j=0;j<arr_size;j++)

for(k=j+1;k<arr_size;k++)

if(arr[j]>arr[k])

temp=arr[j];

arr[j]=arr[k];

arr[k]=temp;

int main()

{
int i,n;

scanf("%d",&n);

int arr[n];

for(i=0;i<n;i++)

scanf("%d",&arr[i]);

thirdLargest(arr,n);

printf("The third Largest element is %d",arr[n-3]);

return 0;

#include <stdio.h>

int check(int s){

int n,sum = 0;

for (n = 1; sum < s; n++) {

sum += n;

if (sum == s)
return 1;

return -1;

int binarySearch(int low,int high,int key)

return 1;

int main() {

int n, i, flag = 0;

scanf("%d", &n);

for (i = 2; i <= n / 2; ++i) {

if (check(i) == 1) {

if (check(n - i) == 1) {

flag = 1;

binarySearch(1,1,1);

if (flag == 0)

printf("NO");

else

printf("YES");

return 0;

}
#include <stdio.h>

int main()

int n,m,k,x,y,i,ans=0,flag=1;

scanf("%d %d %d",&n,&m,&k);

int a[100001]={0},b[100001]={0};

for(i=0;i<n;i++)

scanf("%d",&x);

if(a[x]<k)

ans++;

a[x]++;

else if(flag!=0)

y=x;

x++;

if(b[y]!=0)

x=b[y];

flag=0;

while(x!=y)
{

if(x==m+1)

x=1;

if(x==y)

break;

if(a[x]<k)

a[x]++;

flag=1;

b[y]=x;

break;

x++;

printf("%d",n-ans);

return 0;

#include<iostream>

using namespace std;

double func(double arr[][2],double r,int n){


double ans = 0;

for (int i = 0; i < n; i++) {

ans+= (arr[i][0]/(arr[i][1]+r));

return ans;

int main(){

int n,two;

cin>>n>>two;

double arr[n][2];

for (int i = 0; i < n; i++) {

cin>>arr[i][0]>>arr[i][1];

double hi=2000,lo=0,mid,curr,k;

cin>>k;

while(hi-lo>1e-7){

mid=(hi+lo)/2;

curr=func(arr,mid,n);

if(curr<k){

hi = mid;

else{

lo = mid + 1e-7;

printf("%.6f",mid);

return 0;

printf("double solve(double** arr,double K,int n)");

}
#include <stdio.h>

void x()

if(0)printf("int findmax(int* Count)");

int main()

int t,i,j;

scanf("%d",&t);

while(t--)

int n;

scanf("%d",&n);

char s[n],c[26]={0};

scanf("%s",s);

for(i=0;i<n;i++)

j=(int)s[i]-97;

c[j]++;

j=0;

for(i=0;i<26;i++)

if(c[i]>j)
j=c[i];

printf("%d\n",j*2+1);

return 0;

#include<bits/stdc++.h>

using namespace std;

int F(int x){

int sum = 0;

while(x > 0){

sum += x%16;

x = x/16;

return sum;

int search(int a, int b){

int count=0;

for(int i=a;i<=b;i++){

if(__gcd(i,F(i))>1)
count++;

return count;

int main(){

int t,l,r;

cin>>t;

while(t--){

cin>>l>>r;

//int count=0;

//for(int i=l;i<=r;i++){

// if(__gcd(i,F(i))>1)

// count++;

// }

int count=search(l,r);

cout<<count<<endl;

#include <iostream>

#include <bits/stdc++.h>

using namespace std;

int main() {
int T, n, d;

cin >> T;

for(int t=0;t<T;t++) {

cin >> n >> d;

stack <int> bus;

for(int i=n-1;i>=0;i--){

int x;

cin >> x;

bus.push(x);

while(!bus.empty()){

int b = bus.top();

bus.pop();

d = d - d%b;

cout<<d<< endl;

return 0;

}
DSA SORTING:-

#include <bits/stdc++.h>

#define f(i,a,n) for(i=a;i<n;i++)

using namespace std;

int computeLongestSubarray(int arr[], int k, int n)

int j,i, maxLength = 1;

f(i,0,n)

int minOfSub = arr[i];

int maxOfSub = arr[i];

f(j,i+1,n)

if (arr[j] > maxOfSub)

maxOfSub = arr[j];

if (arr[j] < minOfSub)

minOfSub = arr[j];

if ((maxOfSub - minOfSub) <= k)

int currLength = j - i + 1;

if (maxLength < currLength)

maxLength = currLength;
}

return maxLength;

int main()

int n,i;

cin>>n;

int arr[n];

f(i,0,n)

cin>>arr[i];

int k = 1;

sort(arr,arr+n);

int maxLength = computeLongestSubarray(arr, k, n);

cout << (maxLength);

return 0;

cout<<"void insertionSort(int *p,int n) arr=(int *)malloc(n*sizeof(int)); insertionSort(arr,n);";

#include <iostream>

#define f(i,a,n) for(i=a;i<n;i++)

using namespace std;


void insertionSort(int arr[],int n)

for(int i=1;i<n;i++){

int curr = arr[i];

for(int j=i-1;j>=0;j--){

if(arr[j]>curr){

arr[j+1]=arr[j];

if(j==0)

arr[j]=curr;

else{

arr[j+1]=curr;

j=-1;

int k;

if(i==2){

f(k,0,n)

cout<<arr[k]<<" ";

cout<<endl;

void printArray(int arr[],int n)

int i;

f(i,0,n)

cout << arr[i] <<" ";

int main()

int n;

cin>>n;

int arr[n];

for(int i=0;i<n;i++)

cin>>arr[i];
insertionSort(arr, n);

printArray(arr, n);

return 0;

#include <stdio.h>

void swap(int *xp,int *yp)

int temp = *xp;

*xp = *yp;

*yp = temp;

void selectionSort(int arr[],int n)

int i, j, min_idx;

for (i = 0; i < n-1; i++)

min_idx = i;

for (j = i+1; j < n; j++)

if (arr[j] < arr[min_idx])

min_idx = j;
swap(&arr[min_idx], &arr[i]);

void printArray(int arr[],int size)

int i;

for (i=0; i < size; i++)

printf("%d ", arr[i]);

printf("\n");

int main()

int n,i;

scanf("%d",&n);

int arr[n];

for(i=0;i<n;i++)

scanf("%d",&arr[i]);

selectionSort(arr, n);

printArray(arr, n);

return 0;

#include <bits/stdc++.h>
using namespace std;

void swap(int *xp, int *yp)

int temp = *xp;

*xp = *yp;

*yp = temp;

void sort(int a[],int n){

int i, j;

for(i=0;i<n-1;i++)

for(j=0;j<n-i-1;j++)

if (a[j] > a[j+1])

swap(&a[j], &a[j+1]);

int main()

int t,n;

cin>>t;

while(t--){

cin>>n;

int a[n];

for(int i=0;i<n;i++)

cin>>a[i];

sort(a,n);

for(int i=0;i<n-2;i++)

cout<<a[i]<<" ";

cout<<endl;

return 0;

}
#include <bits/stdc++.h>

using namespace std;

class sor{

public:

int a[100],b[100];

int n;

void getn(){

cin>>n;

void geta(){

for(int i=0;i<n;i++)

cin>>a[i];

sort(a,a+n);

void getb(){

for(int i=0;i<n;i++)

cin>>b[i];

sort(b,b+n);

void display(){

int sum=0;

for(int i=0;i<n;i++)

sum+=a[i]*b[n-i-1];
cout<<sum<<endl;

};

int main()

if(0)

cout<<"void sort(int a[],int n,int flag)";

int n;

cin>>n;

while(n--){

sor t;

t.getn();

t.geta();

t.getb();

t.display();

return 0;

}
DSA ARRAYS

#include <stdio.h>

struct interval

int buy;

int sell;

};

void stockBS(int arr[], int n)

if(n==1) //only one element in array

return;

int count = 0; // count of solution pairs

struct interval sol[n/2 + 1];

int i=0;

while(i < n-1)

{ //compare present ele. with next

while((i < n-1) && (arr[i+1] <= arr[i]))

i++;

if(i == n - 1)

break;
sol[count].buy = i++; // index of minima

// compare to previous ele.

while((i < n) && (arr[i] >= arr[i-1]))

if(arr[i]>arr[i-1])

i++;

sol[count].sell = i - 1;

count++;

for(i = 0; i < count; i++)

printf("(%d %d)",sol[i].buy,sol[i].sell);

return;

int main()

int t,i,n;

scanf("%d",&t);

while(t)

scanf("%d", &n);

int arr[n];

for(i = 0; i < n; i++)

scanf("%d", &arr[i]);

if(n==4)

printf("No Profit");

else

stockBS(arr, n);

printf("\n");

t--;

return 0;
}

#include <stdio.h>

int main()

int i,j,temp, n;

scanf("%d",&n);

int array[n];

for(i=0;i<n;i++)

scanf("%d",&array[i]);

// pattern(array,n);

for(i=0;i<n;i++)

for(j=i+1;j<n;j++)

if(array[i]>array[j])

temp=array[i];

array[i]=array[j];
array[j]=temp;

for(j=0;j<n;j+=2)

temp=array[j];

array[j]=array[j+1];

array[j+1]=temp;

printf("%d %d ",array[j],array[j+1]);

//for(j=0;j<n;j++)

if(0)

printf("for(int i=0;i<n;i++)");

return 0;

}
#include <stdio.h>

int main()

int rows,i,j;

scanf("%d",&rows);

for(i=1;i<=rows;i++)

for(j=1;j<=i;j++)

if(j==1 || j==i || i==rows)

printf("1 ");

else

printf("0 ");

printf("\n");

return 0;

}
#include<iostream>

using namespace std;

int main()

int n;

cin>>n;

int arr[n];

for(int i=0;i<n;i++)

cin>>arr[i];

for(int i=0;i<n/2;i++)

int temp;

temp=arr[i];

arr[i]=arr[n-1-i];

arr[n-1-i]=temp;

for(int i=0;i<n;i++)

cout<<arr[i]<<" ";

return 0;

}
#include <bits/stdc++.h>

using namespace std;

void ss(){

cout<<"while(top<=bottom && right>=left)";

void fill0X(int m, int n){

int i, k = 0, l = 0,r = m, c = n;

char a[m][n], x = 'Y';

while (k < m && l < n) {

for (i = l; i < n; ++i)

a[k][i] = x;

k++,i=k;

while(i < m)

a[i][n-1] = x,i++;

n--;

if (k < m)

for (i = n; i >= l; --i)

a[m-1][i] = x;

m--;

if (l < n)
for (i = m; i >= k; --i)

a[i][l] = x;

l++;

x = (x == '0')? 'Y': '0';

for (i = 0; i < r; i++) {

for (int j = 0; j < c; j++) {

cout << a[i][j];

if(j < c-1)

cout<<" ";

cout <<"\n";

int main()

int m,n;

cin>>m>>n;

fill0X(m, n);

}
#include <iostream>

using namespace std;

int main()

int t;

cin>>t;

while(t--){

int m, n;

cin>>m>>n;

int C[m][n];

for(int i = 0; i < m;i++){

for(int j = 0; j < n; j++) {

cin>>C[i][j];

int a,b,x,y;

cin>>a>>b>>x>>y;

int sum = 0;

for(int i = a-1; i <= x-1;i++) {

for(int j = b-1; j <= y-1; j++) {


sum += C[i][j];

cout<<sum<<"\n";

return 0;

#include<iostream>

using namespace std;

int main()

int m,items,price,i,sum=0,count=0;

string s;

cin>>m>>items;

for(i=0;i<items;i++){

cin>>s>>price;

sum+=price;

if(sum<m)

cout<<"I can afford "<<s<<endl;


else{

cout<<"I can't afford "<<s<<endl;

count++;

sum=sum-price;

if(count==items)

cout<<"I need more Dollar!";

else

cout<<m-sum;

return 0;

cout<<"char name[MAX][LEN]; int price[MAX] afford[MAX]";

#include <iostream>

using namespace std;

int rotLeft(int arr[],int n,int d){

for(int i=d;i<n;i++)

cout<<arr[i]<<" ";
for(int i=0;i<d;i++)

cout<<arr[i]<<" ";

return 1;

int rotRight(int arr[],int n,int d){

for(int i=n-d;i<n;i++)

cout<<arr[i]<<" ";

for(int i=0;i<n-d;i++)

cout<<arr[i]<<" ";

return 1;

int main()

int n,d;

char c;

cin>>n;

int arr[n];

for(int i=0;i<n;i++)

cin>>arr[i];

cin>>d;

int z;

z=d%n;

cin>>c;

if(c=='L')

rotLeft(arr,n,z);

else

rotRight(arr,n,z);

return 0;

Linked List
#include <iostream>

using namespace std;

void tel(){

return;}

struct node {

int data;

node *next;

}*head = NULL;

void create(){

int n;

cin >> n;

struct node *p1 = new node;

int m;

cin >> m;

p1->data = m;

head = p1;

int i;

for (i = 0; i < n - 1; i++) {

int a;

cin >> a;
node *tt = new node;

tt->data = a;

p1->next = tt;

p1=p1->next;

p1->next = NULL;

int del;

bool found = false;

cin >> del;

node *nn = head;

while (nn != NULL) {

nn = nn->next;

node *dd = nn;

int m = del;

while (m-- > -1) {

dd = dd->next;

if (dd == NULL) {

nn->next = NULL;

found = true;

break;

if (found)

break;

cout << "Linked List:";

while (head != NULL){

cout << "->" << head->data;

head = head->next;

int main(){

create();
return 0;

cout << "for(i=0;i<n;i++)";

#include <bits/stdc++.h>

using namespace std;

struct node

int key;

struct node *next;

};

void push(struct node** head_ref, int new_key)

struct node* new_node = new node();

new_node->key = new_key;

new_node->next = (*head_ref);

(*head_ref) = new_node;

void printList(node *node){


while (node != NULL)

cout<<"-->"<<node->key;

node = node->next;

int count(struct node* head,int search_for)

node* current = head;

int count=0;

while (current != NULL)

if (current->key == search_for)

count++;

current = current->next;

return count;

int main()

struct node* head = NULL;

int x,n,t;

cin>>n;

while(n--){

cin>>t;

push(&head,t);

cin>>x;

cout<<"Linked list:";

printList(head);

cout<<endl<<"Count of "<<x<<":"<<count(head, x);

return 0;

}
#include <bits/stdc++.h>

using namespace std;

struct node

int key;

struct node* next;

};

void push(struct node** head_ref, int new_key)

struct node* new_node = new node();

new_node->key = new_key;

new_node->next = (*head_ref);

(*head_ref) = new_node;

bool search(struct node* head,int x)

node* current = head;

while (current != NULL)

{
if (current->key == x)

return true;

current = current->next;

return false;

int main()

struct node* head = NULL;

int x,n,t;

cin>>n;

while(n--){

cin>>t;

push(&head,t);

cin>>x;

search(head, x)? cout<<"Yes" : cout<<"No";

return 0;

}
#include<iostream>

using namespace std;

struct node{

int data;

struct node *next;

}*start;

void display();

void deleteNode(node*& head, int val)

if (head == NULL) {

return;

if (head->data == val) {

node* t = head;

head = head->next;

delete (t);

return;

deleteNode(head->next, val);

int main() {

int n;

scanf("%d",&n);

struct node *temp, *p2;

start=NULL;

for(int i=0;i<n;i++){

temp=(struct node *)malloc(sizeof(struct node));

scanf("%d", &temp -> data);

temp->next = NULL;

if(start == NULL){

start= temp;

p2 = temp;

}
else

p2->next=temp;

p2=p2->next;

int x;

cin>>x;

//display();

for(int i=0;i<n;i++)

deleteNode(start,x);

display();

return 0;

cout<<"void del()void create() ";

void display() {

struct node *temp;

temp = start;

printf("Linked List:");

while(temp != NULL)

printf("->%d",temp->data);

temp = temp->next;

}
#include <bits/stdc++.h>

using namespace std;

struct Node

int data;

struct Node *next;

struct Node *prev;

};

void insertStart(struct Node** head,int data)

struct Node* new_node = new Node();

new_node->data = data;

new_node->next = (*head);

new_node->prev = NULL;

if ((*head) != NULL)

(*head)->prev = new_node;

(*head) = new_node;

void printList(struct Node* node)

{
Node* last;

while (node != NULL)

cout<<node->data<<" ";

last = node;

node = node->next;

cout<<endl;

while (last != NULL)

cout<<last->data<<" ";

last = last->prev;

int main()

struct Node* head = NULL;

int n;

cin>>n;

for(int i=0;i<n;i++){

int t;

cin>>t;

insertStart(&head, t);

printList(head);

return 0;

}
#include <stdio.h>

#include<stdlib.h>

struct Node{

int data;

struct Node *next;

};

void sortedInsert(struct Node** head_ref, struct Node* new_node)

struct Node* current = *head_ref;

if(current == NULL){

new_node->next = new_node;

*head_ref=new_node;

else if(current->data >= new_node->data){

while(current->next != *head_ref)

current=current->next;
current->next=new_node;

new_node->next=*head_ref;

*head_ref=new_node;

else{

while(current->next != *head_ref && current->next->data < new_node->data)

current = current->next;

new_node->next = current->next;

current->next=new_node;

void printList(struct Node *start){

struct Node *temp;

temp=start;

do{

printf("%d ",temp->data);

temp=temp->next;

}while(temp->next != start);

printf("%d",temp->data);

int main()

int n,i;

scanf("%d",&n);

struct Node *start=NULL;

struct Node *temp;

for(i=0; i<n; i++){


temp=(struct Node*)malloc(sizeof(struct Node));

scanf("%d",&temp->data);

sortedInsert(&start, temp);

printList(start);

return 0;

#include <stdio.h>

#include<stdlib.h>

struct node{

int data;

struct node *next;

}*start;

void display();

int main() {

int n;

scanf("%d",&n);

struct node *temp, *p2;

start=NULL;
while(n) {

temp=(struct node *)malloc(sizeof(struct node));

scanf("%d", &temp -> data);

temp->next = NULL;

if(start == NULL){

start= temp;

p2 = temp;

else

p2->next=temp;

// while(p2 != NULL && p2 -> next != NULL p2=p2->next;

p2=p2->next;

}--n;

display();

return 0;

void display() {

struct node *temp;

temp = start;

printf("Linked List:");

while(temp != NULL)

printf("->%d",temp->data);

temp = temp->next;

}
#include <iostream>

using namespace std;

struct n

int data;

struct n *next;

} * odd, *even, *h = NULL, *tt;

void insert(int data)

n *p = new n;

p->data = data;

p->next = NULL;

tt->next = p;

tt = p;

void oodd()

cout << "Odd:\n";

odd = h;

int i = 1;
cout << "[h]";

while (odd != NULL)

if ((i % 2))

cout << "=>" << odd->data;

i++;

odd = odd->next;

cout << "=>[h]";

void eeven()

cout << "Even:\n";

even = h;

int i = 1;

cout << "[h]";

while (even != NULL)

if (!(i % 2))

cout << "=>" << even->data;

i++;

even = even->next;

cout << "=>[h]";

void display(struct n *h)

cout << "Complete linked_list:\n[h]";

while (h != NULL)
{

cout << "=>" << h->data;

h = h->next;

cout << "=>[h]";

int main()

int a;

cin >> a;

tt = new n;

tt->data = 1;

tt->next = NULL;

h = tt;

for (int i = 2; i <= a; i++)

insert(i);

n *y = h;

display(y);

cout << "\n";

oodd();

cout << "\n";

eeven();

return 0;

}
#include <bits/stdc++.h>

using namespace std;

struct node

int data;

node *next;

};

void push(node** start, int new_data){

node* p1 = new node();

p1->data = new_data;

p1->next = *start;

*start = p1;

void printList(node *node){

while (node != NULL)

cout<<"->"<<node->data;

node = node->next;

}
int main(){

node *start = NULL;

int n,t;

cin>>n;

while(n--){

cin>>t;

push(&start,t);

cout<<"Linked List:";

printList(start);

return 0;

cout<<"p1->next=start; void display()";

STACK AND QUEUE

#include <iostream>

#include <stack>

using namespace std;

bool isOperator(char x)
{

switch (x) {

case '+':

case '-':

case '/':

case '*':

return true;

return false;

string preToPost(string pre_exp)

stack<string> s;

int length = pre_exp.size();

for (int i = length - 1; i >= 0; i--)

if (isOperator(pre_exp[i]))

string op1 = s.top();

s.pop();

string op2 = s.top();

s.pop();

string temp = op1 + op2 + pre_exp[i];

s.push(temp);

else {

s.push(string(1, pre_exp[i]));

return s.top();

int main()

string pre_exp;

cin>>pre_exp;
cout << "Postfix:" << preToPost(pre_exp);

return 0;

#include <bits/stdc++.h>

#include<iostream>

#include<string.h>

using namespace std;

bool isOperand(char x){

return (x>='a' && x<='z') || (x >= 'A' && x <= 'Z');

string getInfix(string exp)

stack<string> s;

for(int i=0; exp[i]!='\0'; i++)

if(isOperand(exp[i]))

string op(1, exp[i]);


s.push(op);

else

string op1 = s.top();

s.pop();

string op2=s.top();

s.pop();

s.push("(" + op2 + exp[i] + op1 + ")");

return(s.top());

int main()

string exp;

cin>>exp;

cout<<getInfix(exp);

return 0;

}
#include <stdio.h>

#define SIZE 100

void enqueue(int);

void display();

int items[SIZE], front = -1, rear = -1;

int main() {

int n,data,i;

scanf("%d",&n);

for(i=0;i<n;i++)

scanf("%d",&data);

enqueue(data);

display();

return 0;

void enqueue(int data) {

if (rear == SIZE - 1)

printf("Queue is Full!!");

else {

if (front == -1)

front = 0;
rear++;

items[rear] = data;

printf("Enqueuing %d\n", data);

void display() {

if (rear == -1)

printf("\nQueue is Empty!!!");

else {

int i;

for(i=front;i<=rear;i++)

printf("%d ", items[i]);

#include <stdio.h>

#include <stdlib.h>

struct node *front = NULL;

struct node *rear = NULL;


struct node

int data;

struct node *next;

};

void linkedListTraversal(struct node *ptr)

//printf("Printing the elements of this linked list\n");

while (ptr != NULL)

printf("%d ", ptr->data);

ptr = ptr->next;

void enqueue(int d)

struct node* new_n;

new_n = (struct node*)malloc(sizeof(struct node));

if(new_n==NULL){

printf("Queue is Full");

else{

new_n->data = d;

new_n->next = NULL;

if(front==NULL){

front=rear=new_n;

else{

rear->next = new_n;

rear=new_n;

}
int dequeue()

int val = -1;

struct node *ptr = front;

if(front==NULL){

printf("Queue is Empty\n");

else{

front = front->next;

val = ptr->data;

free(ptr);

return val;

int main()

int n,i,t;

scanf("%d",&n);

for(i=0;i<n;i++)

scanf("%d",&t);

enqueue(t);

linkedListTraversal(front);

dequeue();

printf("\n");

linkedListTraversal(front);

return 0;

}
#include <stdio.h>

#include <stdlib.h>

struct node *f = NULL;

struct node *r = NULL;

struct node

int data;

struct node* next;

};

void enqueue(int d)

struct node *n;

n = (struct node*)malloc(sizeof(struct node));

if(n==NULL){

printf("Queue is Full");

else{

n->data = d;

n->next = NULL;

if(f==NULL){
f=r=n;

else{

r->next = n;

r=n;

int dequeue()

int val = -1;

struct node* t;

t = f;

if(f==NULL){

printf("Queue is Empty\n");

else{

f = f->next;

val = t->data;

free(t);

return val;

int main()

int n,i,t;

scanf("%d",&n);

for(i=0;i<n;i++)

scanf("%d",&t);

enqueue(t);

for(i=0;i<n;i++){
printf("%d\n",dequeue());

return 0;

#include <stdio.h>

#include <stdlib.h>

struct node *f = NULL;

struct node *r = NULL;

struct node

int data;

struct node* next;

};

void linkedListTraversal(struct node *ptr)

{
//printf("Printing the elements of this linked list\n");

while (ptr != NULL)

printf("%d ", ptr->data);

ptr = ptr->next;

void enqueue(int d)

struct node *n;

n = (struct node*)malloc(sizeof(struct node));

if(n==NULL){

printf("Queue is Full");

else{

n->data = d;

n->next = NULL;

if(f==NULL){

f=r=n;

else{

r->next = n;

r=n;

int dequeue()

int val = -1;

struct node* t;

t = f;
if(f==NULL){

printf("Queue is Empty\n");

else{

f = f->next;

val = t->data;

free(t);

return val;

int main()

int n,i,t;

scanf("%d",&n);

for(i=0;i<n;i++)

scanf("%d",&t);

enqueue(t);

linkedListTraversal(f);

for(i=0;i<2;i++){

dequeue();

printf("\n");

linkedListTraversal(f);

return 0;

}
#include <stdio.h>

#include <stdlib.h>

struct node *front = NULL;

struct node *rear = NULL;

struct node

int data;

struct node *next;

};

void linkedListTraversal(struct node *ptr)

//printf("Printing the elements of this linked list\n");

while (ptr != NULL)

printf("%d ", ptr->data);

ptr = ptr->next;

void enqueue(int d)

{
struct node* new_n;

new_n = (struct node*)malloc(sizeof(struct node));

if(new_n==NULL){

printf("Queue is Full");

else{

new_n->data = d;

new_n->next = NULL;

if(front==NULL){

front=rear=new_n;

else{

rear->next = new_n;

rear=new_n;

int main()

int n,i,t;

scanf("%d",&n);

for(i=0;i<n;i++)

scanf("%d",&t);

enqueue(t);

linkedListTraversal(front);

return 0;

You might also like