SUBMITTED TO:
DHARMENDRA DANGI
UNDERSTANDING
OF
ARRAY, STACK
AND QUEUES
PROJECT
BY: YUVRAJ KAG 23U01051
NITESH SOLANKI 23U01049
INTRO
Data structures are indeed fundamental to computer science, serving
as the building blocks for organizing and managing data within
computer programs. They enable efficient data manipulation, allowing
INTRODUCTION
for optimized access, insertion, and deletion operations. Here’s a brief
overview of some common data structures
ARRAY STACK QUEUE
A collection of Follows a last-in, first- Operates on a
elements identified out (LIFO) principle, first-in, first-out
by index or key, useful for backtracking (FIFO) basis, ideal
stored contiguously and undo operations. for processing
in memory tasks in the order
they occur.
2
They are the simplest data structures that consist of contiguous memory locations. The
elements in an array are accessed using indices, which makes it very fast for retrieval
operations. However, arrays have a fixed size, which means the size needs to be known
at compile time
ARRAY
3
Declaration: To declare an array in C, you specify the type of the
elements it will hold, followed by the array name and the size of the array
in square brackets.
For example : int numbers[5]; // Declares an array of 5 integers.
Declaration Syntax: data_type array_name[size];
ARRAY
Initialization: You can initialize an array at the time of declaration by
providing a list of values enclosed in curly braces {}. If the size of the array
is not specified explicitly, the compiler will infer it from the number of
elements provided in the initialization list.
int numbers[5] = {1, 2, 3, 4, 5}; // Initializes an array of 5 integers with
values
Accessing Elements: Elements of an array are accessed using zero-based
indexing. That means the first element is at index 0, the second element at
index 1, and so on. You access elements using square brackets [].int
firstNumber = numbers[0]; // Accesses the first element of the array
arr[6]= 3 7 1 9 3 2
arr[0] arr[1] arr[2] arr[3] arr[4] arr[5]
indices
arr[0] = 3 arr[1] = 7 arr[2] = 1
arr[3] = 9 arr[4] = 3 arr[5] = 2
Array Size: Arrays in C have a fixed size that cannot be changed after
declaration. You must specify the size of the array when declaring it.
Multidimensional Arrays: C also supports multidimensional arrays,
which are arrays of arrays. They are declared by specifying multiple sizes in
the [Link] matrix[3][3]; // Declares a 3x3 matrix.
a[0][0] a[0][1] a[0][2]
Declaration of a 2D array:
data_type array_name[row_size][column_size]; a[1][0] a[1][1] a[1][2]
Accessing elements in a 2D array:
value = array_name[row_index][column_index]; a[2][0] a[2][1] a[2][2]
Column index
Row index
Array Name
Array and Pointers: In C, arrays are closely related to pointers. The
name of an array can be used as a pointer to its first element. This allows
for pointer arithmetic and passing arrays to functions by [Link]
arr[5];int *ptr = arr; // Assigns the address of the first element of arr to ptr
Stack is a linear data structure that follows a particular order in which the
operations are performed. The order may be LIFO(Last In First Out) or FILO(First
In Last Out). LIFO implies that the element that is inserted last, comes out first
and FILO implies that the element that is inserted first, comes out last
PRESENTATION TITLE
8
A stack is a linear data structure in which the insertion of a new element
and removal of an existing element takes place at the same end
represented as the top of the stack.
Basic Operations on Stack
In order to make manipulations in a stack,
there are certain operations provided to us.
•push() to insert an element into the stack
•pop() to remove an element from the
stack
•top() Returns the top element of the stack.
•isEmpty() returns true if stack is empty
else false.
•size() returns the size of stack.
➢ Function calls: Stacks are used to keep track of the return
addresses of function calls, allowing the program to return to the
correct location after a function has finished executing.
➢ Recursion: Stacks are used to store the local variables and return
Applications
addresses of recursive function calls, allowing the program to
keep track of the current state of the recursion.
➢ Expression evaluation: Stacks are used to evaluate expressions in
postfix notation (Reverse Polish Notation).
➢ Syntax parsing: Stacks are used to check the validity of syntax in
programming languages and other formal languages.
➢ Memory management: Stacks are used to allocate and manage
memory in some operating systems and programming languages
ADVANTAGES DISADVANTAGES
➢ Simplicity: Suitable for diverse applications ➢ Accessible only from top, making middle
due to their easy-to-understand structure. elements difficult to retrieve or modify.
➢ Efficiency: Push and pop operations can be ➢ Potential for overflow error if more
performed in constant time (O(1)). elements are pushed onto stack, causing
data loss.
➢ Last-in, First-out (LIFO) Principle: Ensures
last element added is first removed, useful in ➢ Unsuitable for random access, making them
function calls and expression evaluation. unsuitable for specific order applications.
➢ Limited Memory Usage: Only stores ➢ Fixed capacity limits, potentially limiting if
elements pushed onto them, making it the number of elements needed is unknown
memory-efficient. or highly variable.
#include <stdio.h>
#define MAXSIZE 10
int stack[MAXSIZE];
int top = -1;
// Function to add an element to the stack
void push(int element) {
if (top >= MAXSIZE - 1) {
printf("Stack Overflow\n");
CODE FOR STACK
} else {
stack[++top] = element;
printf("%d pushed to stack\n", element);
}
}
// Function to remove the top element from the stack
void pop() {
if (top < 0) {
printf("Stack Underflow\n");
} else {
printf("%d popped from stack\n", stack[top--]);
12
}
}
// Function to display the stack
void display() {
if (top < 0) {
printf("Stack is empty\n");
} else {
printf("Stack elements are:\n");
for (int i = top; i >= 0; i--)
printf("%d ", stack[i]);
printf("\n");
CODE FOR STACK
}
}
// Main function to drive the program
int main() {
push(1);
push(2);
push(3);
display();
pop();
display();
return 0;
13
}
A Queue Data Structure is a fundamental concept in computer science used
for storing and managing data in a specific order. It follows the principle of
“First in, First out” (FIFO), where the first element added to the queue is the
first one to be removed.
PRESENTATION TITLE
14
FIFO Principle of Queue
➢ Queue is like a line waiting to purchase tickets, serving first person in line.
➢ Front of queue (first entry removed) is the head of the queue.
➢ Rear (last entry added) is the tail of the queue.
Basic Operations for Queue in Data Structure:
Some of the basic operations for Queue in Data Structure are:
[Link]() – Adds (or stores) an element to the end of the queue.
[Link]() – Removal of elements from the queue.
[Link]() or front()- Acquires the data element available at the front node of the queue without
deleting it.
[Link]() – This operation returns the element at the rear end without removing it.
[Link]() – Validates if the queue is full.
[Link]() – Checks if the queue is empty.
ADVANTAGE DISADVANTAGE
➢ Enables efficient management of large ➢ Insertion and deletion operations are
data sets. time-consuming.
➢ Allows easy operations like insertion and ➢ Classical queues require deletion of
deletion. existing elements for new elements.
➢ Useful for services used by multiple ➢ Searching an element takes O(N) time.
consumers.
➢ Maximum queue size must be defined
➢ Provides fast data inter-process for array implementation.
communication.
➢ Can be used for implementing other data
structures.
#include <stdio.h>
#include <stdlib.h>
#define MAX 10
int queue[MAX];
int front = -1;
int rear = -1;
CODE FOR QUEUE
// Function to add an element to the queue
void enqueue(int element)
{
if (rear == MAX - 1) {
printf("Queue Overflow\n");
} else {
if (front == -1)
front = 0;
rear++;
queue[rear] = element;
printf("%d enqueued to queue\n", element);
18
}
}
// Function to remove an element from the queue
void dequeue() {
if (front == -1) {
printf("Queue Underflow\n");
} else {
printf("%d dequeued from queue\n", queue[front]);
front++;
if (front > rear)
front = rear = -1;
CODE FOR QUEUE
}
}
// Function to display the queue
void displayQueue() {
if (front == -1) {
printf("Queue is empty\n");
} else {
printf("Queue elements are:\n");
for (int i = front; i <= rear; i++)
printf("%d ", queue[i]);
19
printf("\n");
}
}
// Main function to drive the program
int main() {
enqueue(10);
enqueue(20);
enqueue(30);
displayQueue();
dequeue();
CODE FOR QUEUE
displayQueue();
return 0;
}
20
THANK yuvrajkag2003@[Link]
+91 7489501392
YOU
niteshsolanki798@[Link]
+91 8103170928