stack
stack
Stack
Consider the items pictured in Figure 1. Although the objects are all different, each
illustrates the same concept—a stack. At the logical level, a stack is an ordered group
of homogeneous items or elements. The removal of existing items and the addition
of new items can take place only at the top of the stack. For instance, if your favorite
blue shirt is underneath a faded, old, red one in a stack of shirts, you must first
remove the red shirt (the top item) from the stack. Only then can you remove the
desired blue shirt, which is now the top item in the stack. The red shirt may then be
replaced on the top of the stack or thrown away.
The stack may be considered an ordered group of items because elements occur in a
particular sequence organized according to how long they have been in the stack.
The items that have been present in the stack the longest are at the bottom; the most
recent are at the top. At any time. given any two elements in a stack, one is higher
than the other. (For instance, the red shirt was higher in the stack than the blue shirt.)
Because items are added and removed only from the top of the stack, the last element
to be added is the first to be removed. A stack is a LIFO (Last In, First Out) structure.
The accessing protocol for a stack is summarized as follows: Both to retrieve
elements and to store new elements, access only the top of the stack.
Ninevah University Data Structures and Algorithms
Collage of Electronics Engineering 2nd Class
Computer & Information Eng. Yahya Al Khattab
Operations of Stack: The logical picture of the structure provides only half of the
definition of an abstract data type. The other half consists of a set of operations that
allows the user to access and manipulate the elements stored in the structure. Given
the logical view of a stack, what kinds of operations do we need to use a stack?
The operation that adds an element to the top of a stack is usually called Push and
the operation that removes the top element from the stack is referred to as Pop.
Example1:
#include<stdio.h>
#define max 10
int stack[max];
int index=-1;
int *pop(){
if(index==-1)return(NULL);
return &stack[index--];
}
int main(){
push (10);
push (100);
int *a=pop();
printf("%d",*a);
return (0);
}
Ninevah University Data Structures and Algorithms
Collage of Electronics Engineering 2nd Class
Computer & Information Eng. Yahya Al Khattab
Eample 2:
#include<stdio.h>
#define max 3
struct student{
char name[10];
float avg;
};
student *stack[max];
int index=-1;
student *pop(){
if(index==-1)return NULL;
student *temp =stack[index];
delete stack[index];
index--;
return temp;
}
int main(){
student a={"ahmed",80};
student b={"ali",40};
push (a);
push (b);
student *c=pop();
printf("%s %f",c->name,c->avg);
Ninevah University Data Structures and Algorithms
Collage of Electronics Engineering 2nd Class
Computer & Information Eng. Yahya Al Khattab
student *d=pop();
printf("\n%s %f",d->name,d->avg);
printf("\n%d",index);
student *e=pop();
if (e==NULL)printf("\n under flow");
return 0;
}
Example 3:
#include<stdio.h>
#define max 10
int stack[max];
int index=-1;
int *pop(){
if(index==-1)return(NULL);
return &stack[index--];
}
int isEmpty(){
Ninevah University Data Structures and Algorithms
Collage of Electronics Engineering 2nd Class
Computer & Information Eng. Yahya Al Khattab