O'Reilly logo

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

C++ Data Structures and Algorithms

Book Description

Learn how to build efficient, secure and robust code in C++ by using data structures and algorithms - the building blocks of C++

About This Book
  • Use data structures such as arrays, stacks, trees, lists, and graphs with real-world examples
  • Learn the functional and reactive implementations of the traditional data structures
  • Explore illustrations to present data structures and algorithms, as well as their analysis, in a clear, visual manner
Who This Book Is For

This book is for developers who would like to learn the Data Structures and Algorithms in C++. Basic C++ programming knowledge is expected.

What You Will Learn
  • Know how to use arrays and lists to get better results in complex scenarios
  • Build enhanced applications by using hashtables, dictionaries, and sets
  • Implement searching algorithms such as linear search, binary search, jump search, exponential search, and more
  • Have a positive impact on the efficiency of applications with tree traversal
  • Explore the design used in sorting algorithms like Heap sort, Quick sort, Merge sort and Radix sort
  • Implement various common algorithms in string data types
  • Find out how to design an algorithm for a specific task using the common algorithm paradigms
In Detail

C++ is a general-purpose programming language which has evolved over the years and is used to develop software for many different sectors. This book will be your companion as it takes you through implementing classic data structures and algorithms to help you get up and running as a confident C++ programmer.

We begin with an introduction to C++ data structures and algorithms while also covering essential language constructs. Next, we will see how to store data using linked lists, arrays, stacks, and queues. Then, we will learn how to implement different sorting algorithms, such as quick sort and heap sort. Along with these, we will dive into searching algorithms such as linear search, binary search and more. Our next mission will be to attain high performance by implementing algorithms to string datatypes and implementing hash structures in algorithm design. We'll also analyze Brute Force algorithms, Greedy algorithms, and more.

By the end of the book, you'll know how to build components that are easy to understand, debug, and use in different applications.

Style and approach

Readers will be taken through an indispensable list of data structures and algorithms so they can confidently begin coding in C++.

Downloading the example code for this book You can download the example code files for all Packt books you have purchased from your account at http://www.PacktPub.com. If you purchased this book elsewhere, you can visit http://www.PacktPub.com/support and register to have the files e-mailed directly to you.

Table of Contents

  1. Title Page
  2. Copyright and Credits
    1. C++ Data Structures and Algorithms
  3. Packt Upsell
    1. Why subscribe?
    2. PacktPub.com
  4. Contributors
    1. About the author
    2. About the reviewer
    3. Packt is searching for authors like you
  5. Preface
    1. Who this book is for
    2. What this book covers
    3. To get the most out of this book
      1. Download the example code files
      2. Download the color images
      3. Conventions used
    4. Get in touch
      1. Reviews
  6. Learning Data Structures and Algorithms in C++
    1. Technical requirements
    2. Introduction to basic C++
      1. Creating your first code in C++
      2. Enhancing code development experience with IDE
      3. Defining the variables using fundamental data types
      4. Controlling the flow of the code
        1. Conditional statement
        2. Loop statement
      5. Leveraging the variable capability using advanced data types
    3. Developing abstract data types
      1. Applying C++ classes to build user-defined ADTs
      2. Playing with templates
        1. Function templates
        2. Class templates
        3. Standard Template Library
    4. Analyzing the algorithm
      1. Asymptotic analysis
      2. Worst, average, and best cases
      3. Big Theta, Big-O, and Big Omega
      4. Recursive method
      5. Amortized analysis
    5. Summary
    6. QA section
    7. Further reading
  7. Storing Data in Lists and Linked Lists
    1. Technical requirements
    2. Getting closer to an array
    3. Building a List ADT
      1. Fetching an item in the List
      2. Inserting an item into the List ADT
      3. Finding out the index of a selected item in the List ADT
      4. Removing an item from the List ADT
      5. Consuming a List ADT
    4. Introduction to node
    5. Building a Singly Linked List ADT
      1. Fetching an item in the LinkedList class
      2. Inserting an item in the LinkedList class
      3. Getting the index of the selected item in the LinkedList
      4. Removing an item from the LinkedList ADT
      5. Consuming the LinkedList ADT
    6. Building the Doubly Linked List ADT
      1. Refactoring the Node<T> data type
      2. Refactoring several operations in the LinkedList ADT
        1. Removing an element
        2. Inserting an element
      3. Consuming the DoublyLinkedList ADT
    7. Applying List and LinkedList using STL
      1. std::vector
      2. std::list
    8. Summary
    9. QA section
    10. Further reading
  8. Constructing Stacks and Queues
    1. Technical requirements
    2. Building a Stack ADT
      1. Fetching the item's value in the Stack ADT
      2. Pushing the items of the Stack ADT
      3. Popping the items from the Stack ADT
      4. Consuming a Stack ADT
        1. Another example of Stack ADT implementation
    3. Building a Queue ADT
      1. Getting a value from Queue ADT
      2. Inserting an element into the Queue ADT
      3. Removing an element from the Queue ADT
      4. Consuming the Queue ADT
    4. Building a Deque ADT
      1. Fetching a value from a Deque
      2. Enqueueing an element into the Deque ADT
      3. Dequeuing an element from the Deque ADT
      4. Consuming the Deque ADT
    5. Summary
    6. QA section
    7. Further reading
  9. Arranging Data Elements Using a Sorting Algorithm
    1. Technical requirements
    2. Bubble sort
    3. Selection sort
    4. Insertion sort
    5. Merge sort
    6. Quick sort
    7. Counting sort
    8. Radix sort
    9. Summary
    10. QA section
    11. Further reading
  10. Finding out an Element Using Searching Algorithms
    1. Technical requirements
    2. Linear search
      1. Developing a linear search algorithm
      2. Implementing the linear search algorithm
    3. Binary search
      1. Developing binary search algorithm
      2. Implementing binary search algorithm 
    4. Ternary search
      1. Developing ternary search algorithm
      2. Applying the ternary search algorithm
    5. Interpolation search
      1. Developing interpolation search algorithm
      2. Applying interpolation search algorithm
    6. Jump search
      1. Developing jump search algorithm
      2. Applying jump search algorithm
    7. Exponential search
      1. Developing exponential search algorithm
      2. Invoking the ExponentialSearch() function
    8. Sublist search
      1. Designing sublist search algorithm
      2. Performing sublist search algorithm
    9. Summary
    10. QA section
    11. Further reading
  11. Dealing with the String Data Type
    1. Technical requirement
    2. String in C++
      1. Constructing a string using character array
      2. Using std::string for more flexibility features
    3. Playing with words
      1. Rearranging a word to create an anagram
      2. Detecting whether a word is a palindrome
    4. Constructing a string from binary digits
      1. Converting decimal to binary string
      2. Converting binary string to decimal
    5. Subsequence string
      1. Generating subsequences from a string
      2. Checking whether a string is a subsequence of another string
    6. Pattern searching
    7. Summary
    8. QA section
    9. Further reading
  12. Building a Hierarchical Tree Structure
    1. Technical requirements
    2. Building a binary tree ADT
    3. Building a binary search tree ADT
      1. Inserting a new key into a BST
      2. Traversing a BST in order
      3. Finding out whether a key exists in a BST
      4. Retrieving the minimum and maximum key values
      5. Finding out the successor of a key in a BST
      6. Finding out the predecessor of a key in a BST
      7. Removing a node based on a given key
      8. Implementing the BST ADT
    4. Building a balanced BST (AVL) ADT
      1. Rotating nodes
      2. Inserting a new key
      3. Removing a given key
      4. Implementing AVL ADT
    5. Building a binary heap ADT
      1. Checking if the heap is empty
      2. Inserting a new element into the heap
      3. Fetching the element's maximum value
      4. Removing the maximum element
      5. Implementing a binary heap as a priority queue
    6. Summary
    7. QA section
    8. Further reading
  13. Associating a Value to a Key in a Hash Table
    1. Technical requirement
    2. Getting acquainted with hash tables
      1. Big data in small cells
      2. Storing data in a hash table
      3. Collision handling
    3. Implementing a separate chaining technique
      1. Generating a hash key
      2. Developing an Insert() operation
      3. Developing a Search() operation
      4. Developing a Remove() operation
      5. Developing an IsEmpty() operation
      6. Applying a HashTable ADT using a separate chaining technique in the code
    4. Implementing the open addressing technique
      1. Developing the Insert() operation
      2. Developing a Search() operation
      3. Developing the Remove() operation
      4. Developing an IsEmpty() operation
      5. Developing a PrintHashTable() operation
      6. Applying an HashTable ADT using a linear probing technique in the code
    5. Summary
    6. QA section
    7. Further reading
  14. Implementation of Algorithms in Real Life
    1. Technical requirements
    2. Greedy algorithms
      1. Solving the coin-changing problem
      2. Applying the Huffman coding algorithm
    3. Divide and conquer algorithms
      1. Solving selection problems
      2. Solving matrix multiplication calculations
    4. Dynamic programming
      1. Fibonacci numbers
      2. Dynamic programming and the coin-change problem
    5. Brute-force algorithms
      1. Brute-force search and sort
      2. Strengths and weaknesses of brute-force algorithms
    6. Randomized algorithms
      1. Rаndоm algorіthm classification
      2. Random number generators
      3. Applications of randomized algorithms
    7. Backtracking algorithms
      1. Arranging furniture in a new house
      2. Playing tic-tac-toe
    8. Summary
    9. QA section
    10. Further reading
  15. Other Books You May Enjoy
    1. Leave a review - let other readers know what you think