0% found this document useful (0 votes)
24 views

Stack

This document defines a stack data structure using a struct and includes functions to initialize, display, check if empty/full, push, and pop elements from the stack. It defines a stack struct with a top variable to track the top index and an array to store elements. It includes functions to initialize the top to -1, display all elements, check if empty/full, push/pop elements, and a main function to test the stack implementation by pushing and popping elements and displaying the results.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views

Stack

This document defines a stack data structure using a struct and includes functions to initialize, display, check if empty/full, push, and pop elements from the stack. It defines a stack struct with a top variable to track the top index and an array to store elements. It includes functions to initialize the top to -1, display all elements, check if empty/full, push/pop elements, and a main function to test the stack implementation by pushing and popping elements and displaying the results.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

1 /*RAHUL UPPALWAR*/

2 #include<stdio.h>
3 #define MAX 10
4 struct stack{
5 int top;
6 int arr[MAX] ;
7 };
8  
9 int initial(struct stack *s){
10 s->top = -1;
11 }
12  
13 int display(struct stack s){
14 int i;
15 for(i=s.top ; i>=0 ; i--){
16 printf("%5d",s.arr[i]);
17 }
18 printf("\n\n\n");
19 }
20  
21 int empty(struct stack s){
22 if(s.top == -1){
23 printf("Stack is EMPTY ");
24 return 1;
25 }else{
26 return 0;
27 }
28 }
29  
30 int full(struct stack s){
31 if(s.top == (MAX - 1)){
32 printf("Stack is FULL ");
33 return 1;
34 }else{
35 return 0;
36 }
37 }
38  
39 int push(struct stack *s , int no){
40 if(s->top == MAX -1){
41 printf("STACK OVERFLOW");
42 }else{
43 s->top++;
44 s->arr[s->top] = no;
45 }
46 }
47 int pop(struct stack *s){
48 int popno;
49 if(s->top == -1){
50 printf("STACK UNDERFLOW");
51 return 0;
52 }else{
53 popno = s->arr[s->top];
54 s->top--;
55 return popno;
56 }
57 }
58  
59 int main(){
60 struct stack s;
61 initial(&s);
62 push(&s, 10);
63 push(&s, 20);
64 push(&s, 30);
65 push(&s, 40);
66 push(&s, 50);
67 push(&s, 60);
68 push(&s, 70);
69 push(&s, 80);
70 display(s);
71 printf("\n\n\n\n\n");
72 int popno = pop(&s);
73 display(s);
74 printf("%5d\n\n",popno);
75 }

You might also like