Order
interface
and a good implementation
ADT
is implemented with Core Data Structures
Primitives
, Structures
, Arrays
, Linked Lists
, Trees
, Graphs
Integers
, Node
fixed
elements that have the same type. int my_array[4] = {2, 5, 7, 8};
Array Doubling
, Time Complexity
could be reduced to O(1)
Huge
Array int my_arrray[10000];
ADT Operation
node
contains a value
.Pointer
is an Address
that point to the block of memory
.entity
.entity
is a node
. struct Node {
int value;
struct Node* next;
}
Linked List = (Nodes(Value+Pointer))
Node
, that contains two attributesStruct
as a kind of container of multiple attributesAttribute
also called as Member Variable
or Member Fields
Node Structure
contains attributes which are Value
and Pointer
Node
structure could be used to make Trees
and Graphs
struct LinkedList {
struct Node* head;
}
Node* head = new Node();
Node* second = new Node();
Node* third = new Node();
head -> value = 1;
second -> value = 2;
third -> value = 3;
head -> next = second;
second -> next = third;
class Node:
def __init__(self, value=0, next=None):
self.value = value
self.next = next
Unlike arrays, we don't reserve multiple slots of contiguous memory for our linked lists.
So, the nodes are not arranged sequentially in memory.
Since we implemented Nodes
to point memory address, we could save lots of time.
access specific index
quickly.Time Complexity
is O(1)
.Linked List
, there is no way knowing what the address of the third node
is,Head Node
.Time Complexity
is O(n)
(In Search/Get
).Insert/Remove
process, the time complexity is O(1)
.Length
can easily change, and does not need to know in advancedArrays
need to be allocated with some fixed amount of memory
O(1)
and constant space O(1)
.Linked Lists
, if there is a variable pointed to the tail
of list, the time comlexity is O(1)
. Otherwise, the time complexity is O(n)
.Arrays
, the time complexity is O(n)
. function getItem (head, n) {
count = 0
current = head
while current is not NULL:
if current == n
return value // return value of nth element
current = current.next
handle errors
}
function Search (head, x) {
current = head
while current is not NULL
if current == x
return true
current = current.next
return false
}
function insertFront (head, x) {
Make newNode with x as the value
newNode.next = head
Assign the new node as the head
}
function remove (head, x) {
current = head
if current.value == x
head = current.next
free current
return
while current is not NULL
if current.value == x
break
previous = current
current = current.next
if current is NULL
return
previous.next = curent.next
free current
}
Length
Length
: O(n) struct LinkedList {
Node* head;
}
Length
: O(1), get the help with Node* tail
struct LinkedList {
Node* head;
Node* tail;
int length;
}
Push/Pop
Array
and Linked List
List ADT
push()
, pop()
, top()
, isempty()
First Element
.All elements which on top of it
struct stack {
int items[];
int top;
int maxsize = 10;
}
psudo code: Pushing new element
stack = new Stack
function push (stk, x) {
if stk.top == stk.maxsize
overflow error handling
else
stk.items[top] = x
stk.top = stk.top + 1
}
psudo code: Pop off elements
function pop(stk) {
if stk.top == 0
underflow error handling
else
pop element
stk.top = stk.top - 1
return stk.items[stk.top] // return popped value
}
pseudo code: Peak
function peak (stk) {
return stk.items[stk.top-1]
}
function is_empty (stk) {
return stk.top == 0
}
struct stack {
Node* head
Node* tail // Allow us to push elements in O of one time,
// But, we still need to traverse through the whole linked list to remove the
last element. Thus, that's not a great solution.
int count
}
The solution is actually to add a new node, and assign it as the New Head
Unlike the implementation of a fixed size array, we don't need to check if the stack is full or not,
there is no limit to the size of stack.
function push (stk.x) {
newNode = new Node
newNode.value = x
newNode.next = stk.head
stk.head = newNode
stk.cout = stk.count + 1
}
C code
struct Node {
int value;
Node* next;
}
pesudo code: Pop off the element at the Front
function pop (stk, x) {
if stk.head == NULL
underflow error handling
else
r = stk.head.value
stk.head = stk.head.next
stk.cout = stk.count - 1
return r
}