Skip to content

Commit 0ecc7da

Browse files
committed
stack code and README added
1 parent 7058b32 commit 0ecc7da

File tree

3 files changed

+116
-0
lines changed

3 files changed

+116
-0
lines changed

Data Structure/Stack/README.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Stack
2+
3+
A **stack** is an Abstract Data Type (ADT), commonly used in most programming languages. It is named stack as it behaves like a real-world stack, for example – a deck of cards or a pile of plates, etc.
4+
5+
A real-world stack allows operations at one end only. For example, we can place or remove a card or plate from the top of the stack only. Likewise, Stack ADT allows all data operations at one end only. At any given time, we can only access the top element of a stack.
6+
7+
This feature makes it LIFO data structure. LIFO stands for Last-in-first-out. Here, the element which is placed (inserted or added) last, is accessed first. In stack terminology, insertion operation is called PUSH operation and removal operation is called POP operation.
8+
9+
10+
Conceptually, a stack is simple: a data structure that allows adding and removing elements in a particular order. Every time an element is added, it goes on the top of the stack; the only element that can be removed is the element that was at the top of the stack. Consequently, a stack is said to have "first in last out" behavior (or "last in, first out"). The first item added to a stack will be the last item removed from a stack.
11+
12+
![Stack](stack.svg)
13+
14+
**Basic Operations:**
15+
16+
Stack operations may involve initializing the stack, using it and then de-initializing it. Apart from these basic stuffs, a stack is used for the following two primary operations −
17+
18+
- **push()** − Pushing (storing) an element on the stack.
19+
- **pop()** − Removing (accessing) an element from the stack.
20+
21+
To use a stack efficiently, we need to check the status of stack as well. For the same purpose, the following functionality are available too −
22+
23+
- **peek()** − get the top data element of the stack, without removing it.
24+
- **isFull()** − check if stack is full.
25+
- **isEmpty()** − check if stack is empty.
26+
27+
28+
####Complexity Analysis
29+
- Insertion - O(1)
30+
- Deletion - O(1)
31+
- Access - O(n)
32+
- Search - O(n)
33+
### More on this topic
34+
- https://en.wikipedia.org/wiki/Stack_(abstract_data_type)
35+
- https://www.tutorialspoint.com/data_structures_algorithms/stack_algorithm.htm
36+
- https://en.wikibooks.org/wiki/Data_Structures/Stacks_and_Queues

Data Structure/Stack/stack.svg

4.24 KB
Loading
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
#include <stdio.h>
2+
3+
#define MAXSIZE 100
4+
5+
int stack[MAXSIZE];
6+
int top = -1;
7+
8+
int isEmpty()
9+
{
10+
return (top == -1); //if top is -1 return 1 else return 0
11+
}
12+
13+
int isfull()
14+
{
15+
return (top == MAXSIZE); //if top is equal to MAXSIZE return 1 else return 0
16+
}
17+
18+
int peek()
19+
{
20+
int data = -1;
21+
if(!isEmpty())
22+
data = stack[top];
23+
24+
return data;
25+
}
26+
27+
int pop()
28+
{
29+
int data = peek();
30+
31+
if(data != -1)
32+
top--;
33+
34+
return data;
35+
}
36+
37+
void push(int data)
38+
{
39+
if(!isfull()) {
40+
top = top + 1;
41+
stack[top] = data;
42+
}
43+
else
44+
{
45+
printf("Could not insert data, Stack is full.\n");
46+
}
47+
}
48+
49+
int main()
50+
{
51+
52+
int value, action;
53+
54+
while (1) {
55+
printf("Enter Action: \n 1.Insert \n 2.Get Top Value \n 3.Exit\n");
56+
scanf("%d", &action);
57+
58+
if(action == 1) //Insertion
59+
{
60+
printf("Enter Value: ");
61+
scanf("%d", &value);
62+
63+
push(value);
64+
65+
printf("Element at top of the stack: %d\n", peek());
66+
}
67+
else if(action ==2)
68+
{
69+
value = pop();
70+
if(value == -1)
71+
printf("Could not retrieve data, Stack is empty.\n");
72+
else
73+
printf("Popped value: %d\n", value);
74+
}
75+
else
76+
break;
77+
}
78+
79+
return 0;
80+
}

0 commit comments

Comments
 (0)