Stacks

Bodhak stacks using c tutorial in this chapter we will discuss stacks of Data Structures. The Concepts are well explained with the programs.

A stack is a data structure in which insertions and deletions can only be done at the top. A common example of a stack, which permits the selection of only its end elements, is a stack of books.

 

STACKS using c:

Objectives

Define a stack

Describe the operations on a stack

Implement a stack as a special case of a linked list

Describe applications of stacks

What is a Stack?

A stack is a data structure in which insertions and deletions can only be done at the top.

A common example of a stack, which permits the selection of only its end elements, is a stack of books.

A person desiring a book can pick up only the book at the top, and if one wants to place a plate on the pile of plates, one has to place it on the top.

You would have noticed that whenever a book is placed on top of the stack, the top of the stack moves upward to correspond to the new element (the stack grows).

And, whenever a book is picked, or removed from the top of the stack, the top of the stack moves downward to correspond to the new highest element (the stack shrinks).

Characteristics of a Stack:

When you add an element to the stack, you say that you push it on the stack (it is always done at the top), and if you delete an element from a stack, you say that you pop it from the stack( again from the top of the stack).

Since the last item pushed onto the stack is always the first item to be popped from the stack, a stack is also called as a Last In, First Out or a LIFO structure.

Unlike an array that is static in terms of its size, a stack is a dynamic data structure.

Since the definition of the stack provides for the insertion and deletion of nodes into it, a stack can grow and shrink dynamically at runtime.

An ideal implementation of a stack would be a special kind of linked list in which insertions and deletions can only be done at the top of the list.