Beruflich Dokumente
Kultur Dokumente
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
struct StackNode
int data;
};
stackNode->data = data;
stackNode->next = NULL;
return stackNode;
return !root;
*root = stackNode;
if (isEmpty(*root))
return INT_MIN;
*root = (*root)->next;
free(temp);
return popped;
if (isEmpty(root))
return INT_MIN;
return root->data;
int main()
push(&root, 10);
push(&root, 20);
push(&root, 30);
printf("%d popped from stack\n", pop(&root));
return 0;
int n = strlen(str);
int i;
push(stack, str[i]);
str[i] = pop(stack);
This method makes sure that newly entered element is always at the front of ‘q1’, so that pop
operation just dequeues from ‘q1’. ‘q2’ is used to put every new element at front of ‘q1’.
push(s, x) // x is the element to be pushed and s is stack
1) Enqueue x to q2
// from q2 to q1.
pop(s)
In push operation, the new element is always enqueued to q1. In pop() operation, if q2 is empty
then all the elements except the last, are moved to q2. Finally the last element is dequeued from q1
and returned.
push(s, x)
pop(s)
1) One by one dequeue everything except the last element from q1 and enqueue to q2.
2) Dequeue the last item of q1, the dequeued item is result, store it.
// from q2 to q1.
//QUEUE
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
// A structure to represent a queue
struct Queue
unsigned capacity;
int* array;
};
// queue as 0
queue->capacity = capacity;
queue->front = queue->size = 0;
return queue;
if (isFull(queue))
return;
queue->array[queue->rear] = item;
queue->size = queue->size + 1;
if (isEmpty(queue))
return INT_MIN;
queue->size = queue->size - 1;
return item;
if (isEmpty(queue))
return INT_MIN;
return queue->array[queue->front];
{
if (isEmpty(queue))
return INT_MIN;
return queue->array[queue->rear];
int main()
enqueue(queue, 10);
enqueue(queue, 20);
enqueue(queue, 30);
enqueue(queue, 40);
return 0;
#include <stdlib.h>
#include <stdio.h>
struct QNode
{
int key;
};
// The queue, front stores the front node of LL and rear stores ths
// last node of LL
struct Queue
};
temp->key = k;
temp->next = NULL;
return temp;
return q;
if (q->rear == NULL)
return;
// Add the new node at the end of queue and change rear
q->rear->next = temp;
q->rear = temp;
if (q->front == NULL)
return NULL;
q->front = q->front->next;
if (q->front == NULL)
q->rear = NULL;
return temp;
}
int main()
enQueue(q, 10);
enQueue(q, 20);
deQueue(q);
deQueue(q);
enQueue(q, 30);
enQueue(q, 40);
enQueue(q, 50);
if (n != NULL)
return 0;
#include<stdio.h>
#include<stdlib.h>
struct sNode
int data;
};
/* Function to push an item to stack*/
struct queue
};
push(&q->stack1, x);
int x;
printf("Q is empty");
getchar();
exit(0);
}
/* Move elements from satck1 to stack 2 only if
stack2 is empty */
if(q->stack2 == NULL)
while(q->stack1 != NULL)
x = pop(&q->stack1);
push(&q->stack2, x);
x = pop(&q->stack2);
return x;
/* allocate node */
if(new_node == NULL)
getchar();
exit(0);
}
/* put in the data */
new_node->data = new_data;
new_node->next = (*top_ref);
(*top_ref) = new_node;
int res;
if(*top_ref == NULL)
getchar();
exit(0);
else
top = *top_ref;
res = top->data;
*top_ref = top->next;
free(top);
return res;
}
int main()
q->stack1 = NULL;
q->stack2 = NULL;
enQueue(q, 1);
enQueue(q, 2);
enQueue(q, 3);
/* Dequeue items */
getchar();
Output:
123
Queue can also be implemented using one user stack and one Function Call Stack. Below is modified
Method 2 where recursion (or Function Call Stack) is used to implement queue using only one user
defined stack.
enQueue(x)
1) Push x to stack1.
deQueue:
3) Recursively pop everything from the stack1, store the popped item
in a variable res, push the res back to stack1 and return res
The step 3 makes sure that the last popped item is always returned and since the recursion stops
when there is only one item in stack1 (step 2), we get the last element of stack1 in dequeue() and all
other items are pushed back in step
#include<stdio.h>
#include<stdlib.h>
struct sNode
int data;
};
struct queue
};
push(&q->stack1, x);
int x, res;
if(q->stack1 == NULL)
printf("Q is empty");
getchar();
exit(0);
return pop(&q->stack1);
else
x = pop(&q->stack1);
/* store the last dequeued item */
res = deQueue(q);
push(&q->stack1, x);
return res;
/* allocate node */
if(new_node == NULL)
getchar();
exit(0);
new_node->data = new_data;
new_node->next = (*top_ref);
/* move the head to point to the new node */
(*top_ref) = new_node;
int res;
if(*top_ref == NULL)
getchar();
exit(0);
else
top = *top_ref;
res = top->data;
*top_ref = top->next;
free(top);
return res;
int main()
{
q->stack1 = NULL;
enQueue(q, 1);
enQueue(q, 2);
enQueue(q, 3);
/* Dequeue items */
getchar();