struct Queue
{
int Data;
struct Queue* next;
}*rear, *front;
void enqueue (int value)
{
if (rear == NULL)
{
rear = (struct Queue *) malloc(sizeof(struct Queue));
rear->next = NULL;
rear->Data = value;
front = rear;
}
else
{
temp = (struct Queue *)malloc(sizeof(struct Queue));
rear->next = temp;
temp->Data = data;
temp->next = NULL;
rear = temp;
}
}
void dequeue()
{
front1 = front;
if (front1 == NULL)
{
printf(" Error: Trying to display elements from empty queue");
return;
}
else
if (front1->next != NULL)
{
printf("Dequed value : %d", front1->Data);
front1 = front1->next;
free(front);
front = front1;
}
else
{
printf("Dequed value : %d", front->Data);
free(front);
front = NULL;
rear = NULL;
}
}
void display()
{
struct Node *var = rear;
if(var == NULL)
printf("\nQueue is Empty");
else
{
while(var!=NULL)
{
printf("%d",var->Data);
var=var->next;
}
}
}
Daily Archives: October 12, 2014
Queue using array
#define MAX 10
int queue[MAX], front=-1, rear=-1;
void enqueue (int num)
{
if(front == 0 && rear == MAX-1)
printf("Queue OverFlow Occured");
else if(front == -1 && rear == -1)
{
front = rear = 0;
queue[rear] = num;
}
else if(rear == MAX-1 && front != 0)
{
rear = 0;
queue[rear] = num;
}
else
{
rear++;
queue[rear] = num;
}
}
void dequeue()
{
if(front == -1)
{
printf("Underflow");
return
}
int element = queue[front];
if(front == rear)
front = rear = -1;
if(front == MAX-1)
front = 0;
else
front++;
printf("The deleted element is: %d", element);
}
Stack using Linked List
struct Stack
{
int Data;
struct Stack *next;
}*top;
void popStack()
{
struct Stack *temp = top;
if(temp == NULL)
printf("\nStack Empty")
else{
top = top->next;
free(temp);
}
}
void pushStack(int value)
{
struct Stack *temp;
temp=(struct Stack *)malloc(sizeof(Stack Node));
temp->Data=value;
if (top == NULL)
{
top=temp;
top->next=NULL;
}
else
{
temp->next=top;
top=temp;
}
}
void display()
{
struct Stack *var=top;
if(var == NULL)
printf("Stack is Empty");
else
{
while(var!=NULL) {
printf("%d \n",var->Data);
var=var->next;
}
}
}
Stack using array
#define size 10
struct stack {
int s[size];
int top;
} st;
int stfull() {
if (st.top >= size - 1)
return 1;
else
return 0;
}
void push(int item) {
st.s[st.top++] = item;
}
int stempty() {
if (st.top == -1)
return 1;
else
return 0;
}
int pop() {
int item;
item = st.s[st.top];
st.top--;
return (item);
}
void display() {
int i;
if (stempty())
printf("\nStack Is Empty!");
else {
for (i = st.top; i >= 0; i--)
printf("\n %d", st.s[i]);
}
}