#include <stdio.h>
#include <stdlib.h>
struct Node
{
int data;
struct Node *next;
};
struct Node *f = NULL;
struct Node *r = NULL;
struct Node *tp = NULL;
void Display(struct Node *ptr)
{
printf("Elements of this linked list\n");
while (ptr != NULL)
{
printf("Element: %d\n", ptr->data);
ptr = ptr->next;
}
}
int isEmpty(struct Node *top)
{
if (top == NULL)
{
return 1;
}
else
{
return 0;
}
}
int isFull(struct Node *top)
{
struct Node *p = (struct Node *)malloc(sizeof(struct Node));
if (p == NULL)
{
return 1;
}
else
{
return 0;
}
}
void enqueue(int val)
{
struct Node *n = (struct Node *)malloc(sizeof(struct Node));
if (n == NULL)
{
printf("Queue is Full");
}
else
{
n->data = val;
n->next = NULL;
if (f == NULL)
{
f = r = n;
}
else
{
r->next = n;
r = n;
}
}
}
int dequeue()
{
int val = -1;
struct Node *ptr = f;
if (f == NULL)
{
printf("Queue is Empty\n");
}
else
{
f = f->next;
val = ptr->data;
free(ptr);
}
return val;
}
void push(int val)
{
if (isFull(tp))
{
printf("Stack Overflow! Cannot push %d to the Stack\n", val);
}
else
{
if (tp == NULL)
{
enqueue(val);
tp = f;
}
else
{
enqueue(val);
struct Node *ptr = f;
while (ptr->next->next != NULL)
{
ptr = ptr->next;
}
struct Node *n = ptr->next;
ptr->next = NULL;
r=ptr;
n->next = f;
f = n;
tp = f;
}
}
}
int pop()
{
int x = dequeue();
return x;
}
int main()
{
push(34);
push(4);
push(7);
push(17);
Display(tp);
printf("Dequeuing element %d\n", pop());
printf("Dequeuing element %d\n", pop());
printf("Dequeuing element %d\n", pop());
printf("Dequeuing element %d\n", pop());
return 0;
}