05.LINKED LISTS

SYNTAX

template <class Type>
struct Node
{
  Type info;
  Node<Type> *next;
};

DEFINITIONS

STRUCTURE (STRUCT)

  • similar to a "class" concept; however, the members of the structure are by default public while the members of a class are by default private.

    • The difference is purely about the default access specifier.

    • structs are more commonly used for defining nodes in a linked list, rather than a class due to simplicity, clarity and direct access to the node's members (info & next)

      • classes are better used for complex behavior or encapsulation, like making the data and next members private and providing getter/setter functions, or if you want to integrate the node into a larger object-oriented design

LINKED LIST

  • a collection of components (aka nodes)

    • linked list is not similar to array; they are complex objects called nodes

      • in an array, the elements have positions; however, in linked list, there are no positions - it simply have elements related together using the pointer "next"

  • every node (except the last node) contains the address of the next node (a pointer to the next node)

  • every node has two components

    • data to store the relevant information

    • a link to point to the next node

ABSTRACT DATA TYPES

  • a type (or class) for objects whose behavior is defined by a set of values & a set of operations

  • the definition of ADT only mentions what operations are to be performed, but not how these operations will be implemented

TRAVERSING LINKED LIST

  • define a temporary pointer (current) initialized to the value of the pointer selfFirst

    • if you have a linked list of many nodes, where each node is pointing to the next and the last node is null.

      • "selfFirst" will always point to the first node

      • current will print the information where its currently at and then moves to the next node and print its value

DESTROYLIST()

DELETENODE()

IMPLEMENTATION

UML

VISUALIZATION

SOURCE CODE

HEADER FILE: linkedList.h

SOURCE FILE: implementation.cpp

Last updated