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

4 Stacks and Queues

This document discusses stacks and queues as data structures. It defines stacks as last-in, first-out (LIFO) structures that can be implemented using arrays or linked lists. It also defines queues as first-in, first-out (FIFO) structures with two pointers, a head and tail, that can also be implemented using arrays or linked lists. The document encourages visualizing these structures and writing code to implement push/pop and enqueue/dequeue operations on stacks and queues using arrays and linked lists.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views

4 Stacks and Queues

This document discusses stacks and queues as data structures. It defines stacks as last-in, first-out (LIFO) structures that can be implemented using arrays or linked lists. It also defines queues as first-in, first-out (FIFO) structures with two pointers, a head and tail, that can also be implemented using arrays or linked lists. The document encourages visualizing these structures and writing code to implement push/pop and enqueue/dequeue operations on stacks and queues using arrays and linked lists.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

University of the Philippines Open University

CMSC 204: Data Structures and Algorithms

STACKS AND QUEUES

STACKS

 A stack is a one-dimensional data structure in which values are entered and


removed one item at a time at one end, called the top of stack.
 A stack operates on a last-in, first-out basis, and is therefore sometimes
called a LIFO data structure.
 Stacks have to be implemented using arrays or linked lists
 Stack Structure
o Stack – a block of memory, either an array or linked list
o Top – a pointer to the top of the stack. Depending on the
implementation, it may point either to (a) the last element in the
stack or (b) to the next available space in the stack
 Stack Operations
o Push – (1) stores the datum at the top of the stack and (2) updates
the stack pointer
o Pop – (1) moves the stack pointer one position back and (2) removes
the datum from the top of the stack

Visualizing Stacks
Check out the visualization at http://visualgo.net/list.html (click on the
“Stacks” link/option).

Discuss:
Read Module 3 (page 35) in the CMSC 204 Manual. What are the
differences in the array and linked list implementations of the stack?
University of the Philippines Open University
CMSC 204: Data Structures and Algorithms

To test your understanding of stacks, try to write C# functions which will


perform push and pop operations on stacks implemented using (a)
arrays and (b) linked lists.

QUEUES

 FIFO (first-in, first-out) the reverse of the LIFO data structure


 Queue Structure
o Values are entered in one end called the tail
o Values are removed from the other end called the head
o Two pointers are needed, one pointing to the head, the other
pointing to the tail
o Queues can also be implemented using either arrays or linked lists
 Queue Operations
o Insert (enqueue) – the insert operation stores the information at the
tail of the queue
o Remove (dequeue) – the remove operation removes the information
found at the head of the queue

Visualizing Queues
Check out the visualization at http://visualgo.net/list.html (click on the
“Queue” (singly linked list) and “Dequeue” (doubly linked list)
link/option).

Discuss:
Read Module 4 (page 41) in the CMSC 204 Manual. What are the
differences in the array and linked list implementations of the queue?
University of the Philippines Open University
CMSC 204: Data Structures and Algorithms

To test your understanding of stacks, try to write C# functions which will


perform queue and dequeue operations implemented using (a) arrays
and (b) linked lists.

Takeaway Thoughts and Questions:

1. What new knowledge about stacks and queues is the most


interesting for you?
2. Give an example of a computer program/problem which will make
use of a stack.
3. Give an example of a computer program/problem which will make
use of a queue.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy