A Micro Project On Dsu1111

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 10

A MICRO PROJECT ON "Stack in C"

1.0 Brief Introduction/Rationale


In C, a Stack is a linear data structure that obeys the LIFO (Last In First Out) approach to
conduct a series of basic operations like push, pop, peeks, and traverse. A Stack can be
executed utilizing an Array or Linked List.

A Stack is an abstract linear data structure operating as a group of elements that are inserted
(push operation) and withdrawn (pop operation) according to the Last in First Out (LIFO)
approach. Insertion and deletion occur on the same end (top) in a Stack. The top of the stack
is returned utilizing the peek operation.

What is the Stack Data Structure in C?

In C, the Stack data structure is an ordered, linear series of items. It is a LIFO (Last In First
Out) data structure, which means that we can insert or withdraw an item at the top of the stack
only. It is a sequential data type, unlike an array. In an array, we can access any of its elements
utilizing indexing, but we can only access the topmost element in a stack. The name "Stack"
for this data structure comes from the metaphor of a set of physical items stacked on top of
each other. An example of this data structure would be a stack of plates: We can only add a
plate to the top of the stack and take a plate from the top of the stack. A stack of coins or a
stack of books can also be a real-life example of the stack data structure.
A MICRO PROJECT ON "Stack in C"

1.0 Brief Introduction/Rationale:

In C, a Stack is a linear data structure that obeys the LIFO (Last In First Out)
approach to conduct a series of basic operations like push, pop, peeks, and
traverse. A Stack can be executed utilizing an Array or Linked List.

A Stack is an abstract linear data structure operating as a group of elements


that are inserted (push operation) and withdrawn (pop operation) according
to the Last in First Out (LIFO) approach. Insertion and deletion occur on the
same end (top) in a Stack. The top of the stack is returned utilizing the peek
operation.

 What is the Stack Data Structure in C?

In C, the Stack data structure is an ordered, linear series of items. It is a LIFO


(Last In First Out) data structure, which means that we can insert or withdraw
an item at the top of the stack only. It is a sequential data type, unlike an
array. In an array, we can access any of its elements utilizing indexing, but we
can only access the topmost element in a stack. The name "Stack" for this
data structure comes from the metaphor of a set of physical items stacked on
top of each other. An example of this data structure would be a stack of
plates: We can only add a plate
to the top of the stack and take a plate from the top of the stack. A stack of
coins or a stack of books can also be a real-life example of the stack data
structure.

 There are two kinds of Stack data structures:

1. Static
2. Dynamic

1. Static Stack

A Static Stack (also known as a bounded stack) has a bounded capability. It can
include a restricted number of elements. If a static stack is full and does not
have any space staying for another element to be pushed to it, it is then called
to be in an Overflow State. In C, a static stack is implemented utilizing an
Array, as arrays are static.

2. Dynamic Stack

A Dynamic Stack is a stack data structure whose capability (maximum elements


that can be held) rises or decreases in runtime, based on the operations (push
or pop) performed on it. In C, a dynamic stack is implemented using a Linked
List, as linked lists are dynamic data structures.

 How does Stack Work in C?

In C, the stack data structure works utilizing the LIFO (Last In First Out)
method. Originally, we set a Peek pointer to keep track of the topmost
element of the stack. Then the stack is initialized to -1 as its peek, as we add
(push) elements to the stack, the peek gets updated to point to its topmost
element, and if we remove (pop) elements from the stack, the peek gets
reduced.
We use stack to perform its main two operations: Push and Pop, the other
operation being Peek, which is Empty, and full.

 Push Operation:

We utilize the Push operation to add an element to the top of the stack.

 Pop Operation:
We use the Pop operation to return and withdraw the topmost element of the
stack.

 Peek Operation:

We use the Peek Operation to show the topmost element of the stack.

 ISEmpty Operation:

We use the IsEmpty Operation to inspect whether the stack is empty or not.

 IsFull Operation:

We can utilize the IsFull Operation to inspect whether the stack is full or not.

This operation can only be utilized with the static implementation of the stack
(using an array), also called a bounded stack
.
Time Complexity of Stack Operations
The time complexity of the several stack operations are:

 Push Operation: O(1)

The time complexity of the Pop operation would be O(1) as in this operation,
we are inserting an element at the top of the stack only.

 Pop Operation: O(1)

The time complexity of the Push operation would be O(1) as in this operation,
we are withdrawing and returning an element from the top of the stack only.

 Peek Operation: O(1)

The time complexity of the Peek operation would be O(1) as in this operation,
we are returning only the topmost element of the stack.

 IsEmpty Operation: O(1)

The time complexity of the IsEmpty operation would be O(1) as in this


operation, we are reviewing whether the topmost element is null or not.

 IsFull Operation: O(1)


The time complexity of the IsFull operation would be O(1) as in this operation,
we are inspecting whether the topmost element is at the maximum position
or not.

 Executing Stack in C:

In C, we can execute the Stack data structure utilizing an array or a linked list.

 Stack Program in C utilizing Array:

We will be utilizing an array to implement a Static Stack (Bounded Stack) as


arrays are static in C.
A Static Stack has a bounded capacity. A static stack is called to be in an
Overflow State if it is full (no elements can be further pushed into it).

 Execution:

Stacks can be described using structures, pointers, arrays, or linked

lists. Here, We have executed stacks using arrays in C.


OUTPUT:

 Time Complexity of Stack Operations


As noted above, only a single element can be accessed at a time in Stacks.

While performing push() and pop() operations on the stack, it takes O(1) time.

 Applications of Stack
As soon as the compiler encounters a function call, it gets pushed into the stack.

In the case of nested functions, the inner functions get performed before the outer
functions. This is fully controlled by stacks.
Conclusion:

A stack is a collection of objects that are added and removed in ”last in, first out” or “LIFO”
order. That is, the first thing added is the last thing removed. For example, if you add A, B,
and then C, then the first removed will be C, then B, then A.

Stacks are ADTs or Abstract Data Types because the operations of push and pop are
universally unchanged. It does not matter what data structure you use to implement it or
what language you write it in, the specification does not change.
Reference:
https://www.google.com/
https://www.geeksforgeeks.org/
https://www.javatpoint.com

You might also like