Tuesday, November 22, 2022

Program to implement queue using array

 /* Program to implement queue using array */

#include <stdio.h>

#include <conio.h>

#define MAX 100


int q[MAX + 1], front = 0, rear = 0;


void main ( )

{

clrscr();

void create(),traverse(),insert(),delet();

create ( );

traverse ();

insert ( );

printf("\n After insert an element");

traverse();

delet ( );

printf("\nAfter deletion");

traverse ( );

getch ( );

}


void create ( )

{

char ch;

front=1;

do

{

rear++;

printf ("\nInput element in queue:\n");

scanf ("%d", & q[rear]);

printf ("Press <Y/N> for more element");

ch = getch ( );

}

while (ch=='Y');

}


void traverse ( )

{

int i;

printf ("\nelements in the Queue are:\n");

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

printf ("%d\n", q[i]);

}


void insert ( )

{

int m;

if (rear == MAX)

{

printf ("Queue is overflow \n");

return;

}

printf ("\nInput new element to insert\n");

scanf ("%d", &m);

rear++;

q[rear]=m;

}


void delet( )

{

if (front==0)

{

printf ("Queue is underflow\n");

return;

}

if (front==rear)

{

q[front] = '\0';

front = rear = 0;

}

else

{

q[front] = '\0';

front++;

}

}


Implementation of stack by Array

 /* Implementation of the stack by Array */

#include <stdio.h>

#include <conio.h>

#define MAX 50

int stack [MAX+1], top = 0;

void main ( )

{

clrscr();

void create ( ), traverse ( ), push ( ), pop ( );

create ( );

printf("\n Stack is :\n");

traverse ( );

push ( );

printf("After Push an element the stack is:\n");

traverse ( );

pop ( );

printf("After pop the element the stack is:\n");

traverse ( );

getch ( );

}

void create ( )

{

char ch;

do

{

top ++;

printf ("Input Element");

scanf ("%d", &stack[top]);

printf ("Press <Y> for more element \n");

ch = getch ( );

}

while (ch=='Y');

}

void traverse ( )

{

int i;

for (i=top; i>0; --i)

printf ("%d\n", stack[i]);

}

void push ( )

{

int m;

if (top==MAX)

{

printf ("Stack is overflow");

return;

}

printf ("Input New Element to Insert");

scanf ("%d", &m);

top++;

stack[top]=m;

}

void pop ( )

{

if (top==0)

{

printf ("Stack is underflow\n");

return;

}

stack[top]='\0';

top--;

}