-
Notifications
You must be signed in to change notification settings - Fork 0
/
17Stack_Dynamic_Array.c
105 lines (90 loc) · 2.31 KB
/
17Stack_Dynamic_Array.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>
//gcc 17Stack_Dynamic_Array.c
//define structure
struct ArrayStack
{
int top; //use for index traversing
int capacity; //capacity of dynamic array
int *arrry; //pointer points to dynamic array
};
//create stack function and return address of dynamic array which is created with help of this function
struct ArrayStack* CreateStack(int cap){
struct ArrayStack *stack;
stack=malloc(sizeof(struct ArrayStack)); //ek dynamic struct type variable create hoga
stack->top=-1;
stack->capacity=cap;
stack->arrry=malloc(sizeof(int) * stack->capacity);
return (stack); //return address of stack
}
//create function to check array full or not
int IsFull(struct ArrayStack *stack){
if(stack->top==stack->capacity-1){
return(1);
}else{
return (0);
}
}
//Create Function to check array empty or not
int IsEmpty(struct ArrayStack *stack){
if(stack->top==-1){
return (1);
}else{
return (0);
}
}
//Create Push function to add elements in array
void Push (struct ArrayStack *stack,int item){
if(!IsFull(stack)){
stack->top++; //top ki value increment hogi
stack->arrry[stack->top]=item; //phli bar insert kiya toh item arary ke 0 index pe jayega stack->top means 0
}
}
//Create Pop function to get items
int Pop (struct ArrayStack *stack){
int carryitem;
if(!IsEmpty(stack)){ //agr stack empty nhi hai toh ye chelega
carryitem=stack->arrry[stack->top]; //stack array pointer me array ka base address hai aur ye access kr lega
stack->top--; //drement kiya stack
return(carryitem);
}
return (-1); //joki ki indicate krega ki array empty hai
}
//Now Start main program and use above methods
int main(){
int choice,item;
struct ArrayStack *stack;
stack=CreateStack(4); //create stack array send capacity of array
while(1){
//here you can use screen clear code...
//system("cls");
printf("\n1 Push");
printf("\n2 Pop");
printf("\n3 Exit");
printf("\n4 Enter Your Choice");
scanf("%d",&choice);
switch (choice){
case 1:
printf("\nEnter a Number");
scanf("%d",&item);
Push(stack,item);
break;
case 2:
item=Pop(stack);
if(item==-1){
printf("Oops! stack is empty");
}else{
printf("Your Poped Value is: %d",item);
}
break;
case 3:
exit(0);
break;
}
getch();
}
return 0;
}
//By Navjot Singh Prince:)