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

5 StackQandAns

Stack Question and answer

Uploaded by

facbuki
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

5 StackQandAns

Stack Question and answer

Uploaded by

facbuki
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Data Structures using C Cnu SS

5. The Stack
Definition and Examples
A stack is an ordered collection of items into which new items may be inserted and from which items may
be deleted at one end, called the top of the stack

1. Stack is LIFO Structure [ Last in First Out ]


2. Stack is Ordered List of Elements of Same Type.
3. Stack is Linear List
4. In Stack all Operations such as Insertion and Deletion are permitted at only one end called Top

Example of stack
We know that in a cafeteria the cups are placed one above the other and every new cup is added at top.
When a cup is required, it is taken off from the top and it is used. We call this process as stacking of cups.

Stack of cups Stack of coins

Govt Polytechnic, K.G.F 1 Dept of CSE


Data Structures using C Cnu SS

1. Define stack. Explain the primitive operations on stack.

A stack is an ordered collection of data items in which


insertion and deletion take place at the same end, called the
top.

The various operations that can be performed on stacks


are:
1. To create a stack.
2. To insert an item on the top of the stack (push
operation).
3. To delete an item from the top of the stack (pop operation).
4. To display the contents of the stack (display).
5. Check Whether Stack is Empty or Not?
6. Check Whether Stack is Full or Not?

2. Explain how to represent a stack in C


A stack in C can be created by declaring the structure with two members.
1. An array to hold the items of the stack.
2. An integer to indicate the position of the topmost element.

#define STACKSIZE 50

struct stack
{
int items[STACKSIZE];
int top;
};
Once this has been done, an actual stack s may be declared as,
struct stack s;
In the above declaration stack is a structure template and members of stack can be accessed via the
structure variable s.

Govt Polytechnic, K.G.F 2 Dept of CSE


Data Structures using C Cnu SS

 You can now access stack elements using dot (.) operator. For instance, s. items[9] refers to the
10th element of a stack, since array index starts from zero in C.
 The integer variable top is used to denote the topmost element of the stack s. for example, if s.top
is the integer value 5, and then there are 6 items on the stack.
 If s.top is -1, then the stack is empty (stack underflow).
 If s. top exceeds 49, then stack overflow occurs. That is, stack is full (stack overflow).
3. Write an algorithm to perform push and pop operations in stack

Insert/push operation:
Inserting an element into the stack is called push operation. Only one item is inserted at a time and
item has to be inserted on the top of the stack.

Algorithm: push operation

step 1: start

step 2: [check the overflow condition ]

if (top = = (STACKSIZE -1 ) ) then

Display “Stack overflow”

else

Goto step3

step3: [Insert an element on to stack by incrementing top ]

top = top + 1 ;
stack [ top ] = item ;

step 4: Stop

Delete/pop operation:
Deleting an element from the stack is called pop operation. Only one item can be deleted at a time
and item has to be deleted only from top of the stack.

Algorithm: pop operation

step1: Start

step2: [check the underflow condition]

Govt Polytechnic, K.G.F 3 Dept of CSE


Data Structures using C Cnu SS

if (top = = -1 ) then

Display “Stack is Underflow”

else

step3: [ top most item from the stack is assigned to item]

item=s[top]

Step 4: [decrement top by 1, to delete next element]

printf("deleted item is %d\n", item);

top=top-1;

Step 4: Stop

4. Write a C program to implement stack operations.

#include <stdio.h>
#include <conio.h>
#define MAXSIZE 5

struct stack /* Structure definition for stack */


{
int items[MAXSIZE];
int top;
};
typedef struct stack STACK;
STACK s;
/* Function declaration/Prototype*/
void push (void);
int pop(void);
void display (void);

void main ()
{
int choice;
int option = 1;
clrscr ();
s.top = -1;
printf ("STACK OPERATION\n");

Govt Polytechnic, K.G.F 4 Dept of CSE


Data Structures using C Cnu SS

while (option)
{
printf ("------------------------------------------\n");
printf (" 1 --> PUSH \n");
printf (" 2 --> POP \n");
printf (" 3 --> DISPLAY \n");
printf (" 4 --> EXIT \n");
printf ("------------------------------------------\n");
printf ("Enter your choice\n");
scanf ("%d", &choice);
switch (choice)
{
case 1: push();
break;
case 2: pop();
break;
case 3: display();
break;
case 4: return;
}
fflush (stdin);
printf ("Do you want to continue(Type 0 or 1)?\n");
scanf ("%d", &option);
}
}
/*Function to add an element to the stack*/
void push ()
{
int num;
if (s.top == (MAXSIZE - 1))
{
printf ("Stack is Full\n");
return;
}
else
{
printf ("Enter the element to be pushed\n");
scanf ("%d", &num);

Govt Polytechnic, K.G.F 5 Dept of CSE


Data Structures using C Cnu SS

s.top = s.top + 1;
s.items[s.top] = num;
}
}
/*Function to delete an element from the stack*/
int pop ()
{
int num;
if (s.top == - 1)
{
printf ("Stack is Empty\n");
return (s.top);
}
else
{
num = s.items[s.top];
printf ("poped element is = %d\n", s.items[s.top]);
s.top = s.top - 1;
}
return(num);
}
/*Function to display the status of the stack*/
void display ()
{
int i;
if (s.top == -1)
{
printf ("Stack is empty\n");
return;
}
else
{
printf ("\nThe status of the stack is\n");
for (i = s.top; i >= 0; i--)
{
printf ("%d\n", s.items[i]);
}
}

Govt Polytechnic, K.G.F 6 Dept of CSE


Data Structures using C Cnu SS

printf ("\n");
}
/*---------------------------------------------------------------------------
Output
STACK OPERATION
------------------------------------------
1 --> PUSH
2 --> POP
3 --> DISPLAY
4 --> EXIT
------------------------------------------
Enter your choice
1
Enter the element to be pushed
23
Do you want to continue(Type 0 or 1)?
1
------------------------------------------
1 --> PUSH
2 --> POP
3 --> DISPLAY
4 --> EXIT
------------------------------------------
Enter your choice
1
Enter the element to be pushed
45
Do you want to continue(Type 0 or 1)?
1
------------------------------------------
1 --> PUSH
2 --> POP
3 --> DISPLAY
4 --> EXIT
------------------------------------------
Enter your choice
1
Enter the element to be pushed

Govt Polytechnic, K.G.F 7 Dept of CSE


Data Structures using C Cnu SS

78
Do you want to continue(Type 0 or 1)?
1
------------------------------------------
1 --> PUSH
2 --> POP
3 --> DISPLAY
4 --> EXIT
------------------------------------------
Enter your choice
3
The status of the stack is
78
45
23
Do you want to continue (Type 0 or 1)?
1
------------------------------------------
1 --> PUSH
2 --> POP
3 --> DISPLAY
4 --> EXIT
------------------------------------------
Enter your choice
2
poped element is = 78
Do you want to continue(Type 0 or 1)?
1
------------------------------------------
1 --> PUSH
2 --> POP
3 --> DISPLAY
4 --> EXIT
------------------------------------------
Enter your choice
3
The status of the stack is
45

Govt Polytechnic, K.G.F 8 Dept of CSE


Data Structures using C Cnu SS

23
Do you want to continue(Type 0 or 1)?
0

Govt Polytechnic, K.G.F 9 Dept of CSE

You might also like