CSC 2200 Lecture Notes - Lecture 5: Abstract Data Type, Linked List, Data Element
11 views5 pages
19 Jul 2018
School
Department
Course
Professor

CSC 2200 Lecture 5
Stacks
• Stack
o Is an abstract data type , commonly used in most programming languages
o Behaves like a real world stack
o Example
▪ Deck of cards
▪ Pile of plates
o Allows operations at one end only
o Only access the top element of a stack
o LIFO data structure
▪ Last in first out
o The element which is placed (inserted or deleted) last, is accessed first
o Insertion operation is called PUSH operation and removal operation is called POP
operation
o
• Stack representation
o A stack can be implemented by means of Array, structure, pointer, and linked list
o Stack can either be a fixed size one or it may have a sense of dynamic resizing
• Basic operations
o May involve initializing the stack, using it and then de-initializing
o A stack is used for the following two primary operations
▪ Push()
• Storing element on the stack
▪ Pop()
• Removing an element from the stack
▪ Peek()
• Get the top data element of the stack without removing it
▪ isFull()
• checks if stack is full
▪ isEmpty()
• checks if stack is empty
o At all times, we maintain a pointer to the last pushed data on the stack
o As this pointer always represents the top of the stack, hence named top
o The top pointer provides top value of the stack without removing it
• Peek()
int peek()
{