Home

Data Structures

Data structures store data in an organized way, define the types of values that can be stored, and specify the operations that can be performed on that data.

The 4 most common operations are create/add/insert, read/get/access, update, and delete/remove. Commonly abbreviated as CRUD.

Ordered means that the data structure retains the order in which elements are added.

Abstract data types

Abstract data types(ADTs) define which operation can be used, but not how the data is to be stored and organized.

Stack

Last in first out(LIFO) - The last item added to the collection is the first one to be taken out.

Common operations

Pseudocode Implementation

Common use cases

Queues

First in first out(FIFO) - The first item added to the collection si the first one to be taken out.

Common operations

Pseudocode Implementation

Common use cases

Lists

Common operations

Common use cases

Concrete data structures

Concrete data structures are specific implementations of ADTs that define how data is stored and how operations are performed on memory.

Linked List

// Destructor
LinkedList::~LinkedList() {
	while (top) {
		ListNode* deleteNode = top;
		top = top->next;
		delete deleteNode;
	}
}
ListInsertNodeAfter(list, currentNode, newNode) {
   // Special case for empty list
   if (list⇢head == null) {
      list⇢head = newNode
      list⇢tail = newNode
   }
   else if (currentNode == list⇢tail) {
      list⇢tail⇢next = newNode
      list⇢tail = newNode
   }
   else {
      newNode⇢next = currentNode⇢next
      currentNode⇢next = newNode
   }
}
ListInsertAfter(list, currentItem, newItem) {
   currentNode = ListSearch(list, currentItem)
   if (currentNode != null) {
      newNode = Allocate new singly-linked list node
      newNode⇢data = newItem
      newNode⇢next = null
      ListInsertNodeAfter(list, currentNode, newNode)
      return true
   }
   return false
}
ListRemoveNodeAfter(list, curNode) {
   // Special case, remove head
   if (curNode is null) {
      sucNode = list⇢head⇢next
      list⇢head = sucNode

      if (sucNode is null) { // Removed last item
         list⇢tail = null
      }
   }
   else if (curNode⇢next is not null) {
      sucNode = curNode⇢next⇢next
      curNode⇢next = sucNode

      if (sucNode is null) { // Removed tail
         list⇢tail = curNode
      }
   }
}
ListRemove(list, itemToRemove) {
   // Traverse to the node with data equal to itemToRemove, 
   // keeping track of the previous node in the process
   previous = null
   current = list⇢head
   while (current != null) {
      if (current⇢data == itemToRemove) {
         ListRemoveNodeAfter(list, previous)
         return true
      }
         
      // Advance to next node
      previous = current
      current = current⇢next
   }
      
   // Not found
   return false
}
class IntNode {
public:
   IntNode(int dataInit = 0, IntNode* nextLoc = nullptr);
   void InsertAfter(IntNode* nodeLoc); // inserts the nodeLoc after this node.
   IntNode* GetNext();
   void PrintNodeData();
private:
   int dataVal;
   IntNode* nextNodePtr;
};