0% found this document useful (0 votes)
22 views

Implementation of Stack Using Array Source Code

This document contains source code for implementing a stack and queue using arrays in C. For the stack implementation, it defines functions for push, pop, peek, isEmpty and display. It uses a fixed size array and tracks the top index. For the queue implementation, it defines enqueue, dequeue and display functions. It uses front and rear indexes to track the head and tail of the queue within a fixed size array. The main functions test pushing and popping elements onto the stack, and enqueueing and dequeueing elements from the queue.

Uploaded by

Reshma K.P
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views

Implementation of Stack Using Array Source Code

This document contains source code for implementing a stack and queue using arrays in C. For the stack implementation, it defines functions for push, pop, peek, isEmpty and display. It uses a fixed size array and tracks the top index. For the queue implementation, it defines enqueue, dequeue and display functions. It uses front and rear indexes to track the head and tail of the queue within a fixed size array. The main functions test pushing and popping elements onto the stack, and enqueueing and dequeueing elements from the queue.

Uploaded by

Reshma K.P
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Implementation of Stack using Array

Source Code :
#include <stdio.h>
#define SIZE 10

int stack[SIZE];
int top = -1;

void push(int value)


{
if(top<SIZE-1)
{
if (top < 0)
{
stack[0] = value;
top = 0;
}
else
{
stack[top+1] = value;
top++;
}
}
else
{
printf("Stackoverflow!!!!\n");
}
}

int pop()
{
if(top >= 0)
{
int n = stack[top];
top--;
return n;
}
}

int Top()
{
return stack[top];
}

intisempty()
{
return top<0;
}

void display()
{
inti;
for(i=0;i<=top;i++)
{
printf("%d\n",stack[i]);
}
}

int main()
{
push(4);
push(8);
display();
pop();
display();
return 0;
}

Implementation of Queue using Array


Source Code:
#include<stdio.h>
#define SIZE 100
struct Queue
{
int a[100];
int rear
int front;
}q;
Void enqueue(int x);
Int dequeue();
void display();

void enqueue(int x)
{
if(q.rear == SIZE-1)
{
printf( "\nQueue is full, %d",q.rear);
}
else
{
q.a[++q.rear] = x;
}
}

Int dequeue()
{
if (q.rear==-1 &&q.front==-1)
{
printf(“Queue – Underflow error”);
}
Else
{
printf("\ndequed %d", q.a[q.front]);
returnq.a[++q.front];
}
}
void display()
{
inti;
printf("\n");
for(i = q.front; i<= q.rear; i++)
{
printf("%d\t", q.a[i]);
}
}
void main()
{
q.front=-1;
q.rear=-1;
enqueue(15);
enqueue(100);
enqueue(150);
display();
dequeue();
display();
dequeue();
}

You might also like