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

stack

The document provides an overview of stack data structures, emphasizing their LIFO (Last In, First Out) nature, where items are added and removed only from the top. It describes essential operations such as 'Push' for adding elements and 'Pop' for removing them, along with several C programming examples demonstrating stack implementation. The document also includes code snippets for managing both integer and student data types using stacks.

Uploaded by

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

stack

The document provides an overview of stack data structures, emphasizing their LIFO (Last In, First Out) nature, where items are added and removed only from the top. It describes essential operations such as 'Push' for adding elements and 'Pop' for removing them, along with several C programming examples demonstrating stack implementation. The document also includes code snippets for managing both integer and student data types using stacks.

Uploaded by

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

Ninevah University Data Structures and Algorithms

Collage of Electronics Engineering 2nd Class


Computer & Information Eng. Yahya Al Khattab

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 push(int n){


if (index==max-1) return(-1);
stack[++index]=n;
return(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;

int push(student n){


if (index==max-1) return(-1);
index++;
stack[index]=(student*)new student();
*stack[index]=n;
return(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 push(int n){


if (index==max-1) return(-1);
stack[++index]=n;
return(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

if (index ==-1)return (-1);


else return (1);
}
int main(){
/*
int a[10];
int *b[10];
for (int i=0;i<10;i++)a[i]=i;
for (i=0;i<10;i++) push(a[i]);
for (i=0;i<10;i++) b[i]=pop();
for (i=0;i<10;i++)printf("%d\n",*b[i]);
*/
for (int i=0;i<10;i++) push(i);
for (i=0;i<5;i++) {
int *a=pop();
int *b=pop();
int c=*a+*b;
printf("%d\n",c);
}
return 0;
}

You might also like