0% found this document useful (0 votes)
150 views19 pages

Cpts 122: Instructor - Nadra Guizani Cpts 122 (January 17, 2020) Washington State University

This document provides an overview of data structures that will be covered in the CptS 122 course. It discusses what data structures are and provides examples like arrays, structs, lists, stacks, queues, and binary search trees. The document explains that dynamic data structures that can grow and shrink at runtime will be implemented in C and C++ using pointers, structs, malloc(), free(), and realloc(). Key concepts like self-referential structures and dynamic memory allocation are also introduced.

Uploaded by

andrew
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)
150 views19 pages

Cpts 122: Instructor - Nadra Guizani Cpts 122 (January 17, 2020) Washington State University

This document provides an overview of data structures that will be covered in the CptS 122 course. It discusses what data structures are and provides examples like arrays, structs, lists, stacks, queues, and binary search trees. The document explains that dynamic data structures that can grow and shrink at runtime will be implemented in C and C++ using pointers, structs, malloc(), free(), and realloc(). Key concepts like self-referential structures and dynamic memory allocation are also introduced.

Uploaded by

andrew
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/ 19

CptS 122

Instructor – Nadra Guizani


CptS 122 (January 17, 2020)
Washington State University
Data Structures
What is a Data Structure

• is a data organization, management, and storage format


that enables efficient access and modification.
- More precisely, a data structure is a collection of data values,
- the relationships among them,
- and the functions or operations that can be applied to the data.
• Defined and Applied for a particular application or task
Examples of Data Structures

• Ones you know: • Ones we will learn


• Arrays • Lists
• Structs in C • Stacks
• Queues
• Binary Search Trees
Array == Data Structure?

• Collection of related data items


• Can be accessed and modified by the same variable name
and index
• Data is the same type
Struct == Data Structure?
• A collection of related fields or variables under one name

• The fields are contiguous in memory

• But so far everything has been static. Once a size of the data
structure has been defined that is all we can modify.
Data Structures 2.0
• In this course we will focus on dynamic data structures
• Data structures will be able to grow and shrink at run-time
• Implementing in both C/C++
• Lists
• Stacks
• Queues
• Binary Trees and Binary Search Trees (BSTs)
Lists
• Collection of data items
that are lined up in a row
like format
• Insertion and deletion can
happen anywhere
• Examples:
• Grocery store list
• Music/Movie Collection
• Etc.
Stacks
• Restricted lists
• Insertions and deletions
may be made at one end
only
• These are Last In, First
Out (LIFO) structures
Queues
• Also a restricted list
• Insertions are made at the
back of the queue and
deletions are made from the
front
• These are First In, First Out
(FIFO) structures
• Examples: Waiting in line for
a ticket, appointment, or
grocery
BSTs
• require linked data items
• Efficient for searching and sorting
of data
• May represent directories on a file
system, etc.
Implementation of Dynamic Structures
• Implementing in both C and C++
• All will have growing and shrinking properties
• Implemented heavily dependent through both
• Pointers
• Structs – –self-referential structures for linked implementations
Self-Referential Structure
• struct which contains a pointer field that represents an address of a struct of
the same type
• Example:
Dynamic Allocation in C
• The growing and shrinking properties may be achieved through functions
located in <stdlib.h> including:
- malloc() for allocating/growing memory
- free() for de-allocating/shrinking memory
- realloc()for resizing memory
- Also consider calloc()
malloc()
malloc()
• Assume the following:
Node *pItem = NULL;
• How to use malloc()
pItem = (Node *) malloc (sizeof (Node));
// Recall malloc ( ) returns a void *,
// which should be typecasted
free()
• free (pItem);
// Requires the pointer to the memory to be
// de-allocated
realloc()
• pItem = realloc (pItem, sizeof (Node) * 2);
// Allocates space for two Nodes and
// returns pointer to beginning of resized //
memory
Book Reference
• C++: How to Program (10th ed.) HK 13.1 – 13.3,
• C: How to Program (8th ed.) DD C 12.1 – 12.3

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