0% found this document useful (0 votes)
13 views9 pages

Dsa Exp1

The document outlines an experiment conducted by a student named Chris Coutinho at Fr. Conceicao Rodrigues College of Engineering, focusing on implementing a Stack Abstract Data Type (ADT) using arrays in C. It details the stack operations such as push, pop, peek, and provides pseudocode and source code for the implementation, along with advantages and disadvantages of the array-based approach. The conclusion emphasizes the efficiency of the stack ADT for various applications while noting the potential need for a linked list implementation for greater flexibility.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views9 pages

Dsa Exp1

The document outlines an experiment conducted by a student named Chris Coutinho at Fr. Conceicao Rodrigues College of Engineering, focusing on implementing a Stack Abstract Data Type (ADT) using arrays in C. It details the stack operations such as push, pop, peek, and provides pseudocode and source code for the implementation, along with advantages and disadvantages of the array-based approach. The conclusion emphasizes the efficiency of the stack ADT for various applications while noting the potential need for a linked list implementation for greater flexibility.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

FR.

CONCEICAO RODRIGUES COLLEGE OF ENGINEERING


Department of Electronics and Computer Science
------------------------------------------------------------------------------------------------------------

Name of the Student: Chris Coutinho

Experiment No 1 Roll Number 10113


Date of Performance Date of Submission
Experiment Title Implementation of Stack using Array
CO Mapping CO1:Implement various operations of linear data structures.

Problem Statement:

Write a C Program to implement Stack ADT using arrays.

Objective of the Experiment:

Understand basic Stack operations like Push, Pop, Peek, Size of the stack.

Theory:

●​ A stack is a linear data structure that follows the Last In, First Out (LIFO) principle,
meaning that the last element added to the stack is the first one to be removed. The stack
Abstract Data Type (ADT) supports essential operations such as push (insertion), pop
(deletion), peek (viewing the top element), and isEmpty/isFull (checking stack
status).

●​ Stacks are widely used in applications like expression evaluation, function call
management in recursion, and backtracking algorithms.

●​ A stack is a container of objects that are inserted and removed according to the
last-in-first out ( LIFO ) principle.
●​ Objects can be inserted at any time, but only the last (the most-recently inserted) object
can be removed.
●​ Inserting an item is known as “pushing” onto the stack.
●​ “Popping” off the stack is synonymous with removing an item

Data Structures and Algorithms (VSE12EC03) SE ECS


FR. CONCEICAO RODRIGUES COLLEGE OF ENGINEERING
Department of Electronics and Computer Science
------------------------------------------------------------------------------------------------------------

The following support methods should also be defined: -


1.​ size(): Returns the number of objects in stack
2.​ isEmpty(): Return a boolean indicating if the stack is empty.
3.​ isFull(): Return a Boolean indicating if the stack is full.
4.​ peek(): Return the top object of the stack, without removing it; if the stack
is empty and an error occurs.
5.​ display(): Display the elements of stack from top of the stack.

Approach:

A stack can be implemented using arrays (static implementation) or linked lists (dynamic
implementation). Here, we focus on an array-based implementation.Pseudocode given below
explains basic operations to be implemented.

PUSH Operation Pseudo code

FUNCTION push(Stack s, INTEGER value)

IF s.top == MAX - 1 THEN

PRINT "Stack Overflow!" // Stack is full

ELSE

s.top ← s.top + 1 // Move top pointer up

s.arr[s.top] ← value // Insert value at the top

PRINT "Pushed", value

END IF
END FUNCTION

POP Operation Pseudo code

Data Structures and Algorithms (VSE12EC03) SE ECS


FR. CONCEICAO RODRIGUES COLLEGE OF ENGINEERING
Department of Electronics and Computer Science
------------------------------------------------------------------------------------------------------------

FUNCTION pop(Stack s) RETURNS INTEGER

IF s.top == -1 THEN

PRINT "Stack Underflow!" // Stack is empty

RETURN -1

ELSE

ELEMENT ← s.arr[s.top] // Get the top element

s.top ← s.top - 1 // Move top pointer down

RETURN ELEMENT

END IF

END FUNCTION

Advantages & Disadvantages of Array-Based Implementation

Advantages:

●​ Simple and fast access to elements.


●​ Efficient operations with constant time complexity O(1).

Disadvantages:

●​ Fixed size limits flexibility.


●​ Memory is allocated even if the stack is partially filled.
●​ Resizing requires creating a new array and copying elements.

Data Structures and Algorithms (VSE12EC03) SE ECS


FR. CONCEICAO RODRIGUES COLLEGE OF ENGINEERING
Department of Electronics and Computer Science
------------------------------------------------------------------------------------------------------------

Source Code and O/P:


#include <stdio.h>
#define MAX 5

int stack[MAX], top = -1;

void push(int value);


int pop();
int peek();
void display();

int main() {
int choice, value;

while (1) {
printf("\n1. Push\n2. Pop\n3. Peek\n4. Display\n5. Exit\n");
printf("Enter choice: ");
scanf("%d", &choice);

switch (choice) {
case 1:
printf("Enter value to push: ");
scanf("%d", &value);
push(value);
break;
case 2:
value = pop();
if (value != -1)

Data Structures and Algorithms (VSE12EC03) SE ECS


FR. CONCEICAO RODRIGUES COLLEGE OF ENGINEERING
Department of Electronics and Computer Science
------------------------------------------------------------------------------------------------------------

printf("Popped: %d\n", value);


break;
case 3:
value = peek();
if (value != -1)
printf("Top element: %d\n", value);
break;
case 4:
display();
break;
case 5:
return 0;
default:
printf("Invalid choice!\n");
}
}
}

void push(int value) {


if (top == MAX - 1) {
printf("Stack Overflow!\n");
return;
}
stack[++top] = value;
printf("Pushed: %d\n", value);
}

int pop() {

Data Structures and Algorithms (VSE12EC03) SE ECS


FR. CONCEICAO RODRIGUES COLLEGE OF ENGINEERING
Department of Electronics and Computer Science
------------------------------------------------------------------------------------------------------------

if (top == -1) {
printf("Stack Underflow!\n");
return -1;
}
return stack[top--];
}

int peek() {
if (top == -1) {
printf("Stack is empty!\n");
return -1;
}
return stack[top];
}

void display() {
if (top == -1) {
printf("Stack is empty!\n");
return;
}
printf("Stack: ");
for (int i = top; i >= 0; i--)
printf("%d ", stack[i]);
printf("\n");
}

Data Structures and Algorithms (VSE12EC03) SE ECS


FR. CONCEICAO RODRIGUES COLLEGE OF ENGINEERING
Department of Electronics and Computer Science
------------------------------------------------------------------------------------------------------------

Data Structures and Algorithms (VSE12EC03) SE ECS


FR. CONCEICAO RODRIGUES COLLEGE OF ENGINEERING
Department of Electronics and Computer Science
------------------------------------------------------------------------------------------------------------

Data Structures and Algorithms (VSE12EC03) SE ECS


FR. CONCEICAO RODRIGUES COLLEGE OF ENGINEERING
Department of Electronics and Computer Science
------------------------------------------------------------------------------------------------------------

The program was tested for different sets of inputs.


Program is working is SATISFACTORY NOT SATISFACTORY ( Tick appropriate
outcome)

Conclusion

The stack ADT provides a structured way to store and manage data using the LIFO principle.
The array-based implementation offers efficient and fast operations, making it ideal for use cases
like function call stacks, undo mechanisms, and expression evaluation. However, for dynamic
memory allocation and flexibility, a linked list-based implementation can be considered.

Evaluation: -

On Time Completion and Knowledge of the Implementation and Total (10)


Submission (2) topic (4) Output (4)

Date and Signature of teacher:

Data Structures and Algorithms (VSE12EC03) SE ECS

You might also like