You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: array/README.md
+1-1
Original file line number
Diff line number
Diff line change
@@ -76,7 +76,7 @@ func main() {
76
76
77
77
Accessing an element within an array using an index has O(1) time complexity. This means that regardless of the size of the array, read and write operations for a given element can be performed in constant time.
78
78
79
-
While arrays are useful for certain tasks, searching an unsorted array can be a time-consuming O(n) operation. Since the target item could be located anywhere in the array, every element must be checked until the item is found. Due to this limitation, alternative data structures such as trees and hash tables are often more suitable for search operations.
79
+
While arrays are useful for certain tasks, searching an unsorted array can be a time-consuming O(n) operation. Since the target item could be located anywhere in the array, every element must be checked until the item is found. Due to this limitation, alternative data structures such as [linked-lists](../linkedlist/), [trees](../tree/) and [hash tables](../hashtable/) are often more suitable for search operations.
80
80
81
81
Addition and deletion operations are O(n) operations in Arrays. Removing an element can create an empty slot that must be eliminated by shifting the remaining items. Similarly, adding items to an array may require shifting existing items to create space for the added item. These inefficiencies can make alternative data structures, such as [trees](../tree) or [hash tables](../hashtable), more suitable for managing operations involving additions and deletions.
Copy file name to clipboardExpand all lines: linkedlist/README.md
+1-1
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,6 @@
1
1
# Linked List
2
2
3
-
Linked lists are a collection of nodes, each capable of storing at least one data element and linked to the next node via a reference. One of the key advantages of linked lists over [arrays](../array)is their dynamic size, which allows for items to be added or removed without necessitating the resizing or shifting of other elements.
3
+
Linked lists are a collection of nodes, each capable of storing at least one data element and linked to the next node via a reference. One of the key advantages of linked lists over [arrays](../array)has traditionally been seen their dynamic size, which allows for items to be added or removed without necessitating the resizing or shifting of other elements. As shown in [queues](../queue/) with benchmarks this is different in Go due to clever slice sizing techniques used. In practice you can use slices in Go most of the time without a significant performance penalty.
4
4
5
5
Two types of linked lists exist: singly linked lists, in which each node is linked only to the next node, and doubly linked lists, in which each node is connected to both the next and previous nodes.
Copy file name to clipboardExpand all lines: stack/README.md
+20-2
Original file line number
Diff line number
Diff line change
@@ -4,6 +4,24 @@ Stacks are data structures that operate on the Last-In-First-Out (LIFO) principl
4
4
5
5
One way to visualize stacks is to think of a backpack where items are placed and later removed in reverse order, with the last item added to the bag being the first item removed.
6
6
7
+
The following diagram shows the state of a stack of size 5 when numbers 1 to 4 are pushed to the stack and 4 numbers are popped from the stack. The outcome is reversion of the inserted numbers.
8
+
9
+
```ASCII
10
+
[Figure 1] Push 1,2,3,4 to a stack and then pop 4 times.
11
+
12
+
┌───┐┌───┐┌───┐┌───┐┌───┐ ┌───┐┌───┐┌───┐┌───┐
13
+
│ ││ ││ ││ ││ │ │ ││ ││ ││ │
14
+
├───┤├───┤├───┤├───┤├───┤ ├───┤├───┤├───┤├───┤
15
+
│ ││ ││ ││ ││ 4 │ │ ││ ││ ││ │
16
+
├───┤├───┤├───┤├───┤├───┤ ├───┤├───┤├───┤├───┤
17
+
│ ││ ││ ││ 3 ││ 3 │ │ 3 ││ ││ ││ │
18
+
├───┤├───┤├───┤├───┤├───┤ ├───┤├───┤├───┤├───┤
19
+
│ ││ ││ 2 ││ 2 ││ 2 │ │ 2 ││ 2 ││ ││ │
20
+
├───┤├───┤├───┤├───┤├───┤ ├───┤├───┤├───┤├───┤
21
+
│ ││ 1 ││ 1 ││ 1 ││ 1 │ │ 1 ││ 1 ││ 1 ││ │
22
+
└───┘└───┘└───┘└───┘└───┘ └───┘└───┘└───┘└───┘
23
+
```
24
+
7
25
## Implementation
8
26
9
27
In Go, stacks can be implemented using doubly [linked lists](../linkedlist/) or [arrays and slices](../array/). Here is a linked list implementation:
@@ -75,13 +93,13 @@ func pop() (int, error) {
75
93
76
94
Push and pop operations in stacks are considered O(1) operations, making them highly efficient. Additionally, many machines have built-in stack instruction sets, further increasing their performance. Stacks' unique efficiency and usefulness have solidified their place as one of the most fundamental data structures, second only to [arrays](../array).
77
95
78
-
Resizing the slice and item shifting maybe necessary in the slice implementation, hence traditionally this implementation is seen as O(n). As shown in the complexity of [queue](../queue/README.md) because of the intelligent ways Go resizes the slices this is not a problem and the slice implementation of both stack and queue will perform better than the linked list implementation.
96
+
Resizing the slice and item shifting maybe necessary in the slice implementation, hence traditionally this implementation is seen as O(n). As shown in the complexity of [queue](../queue/README.md) because of the intelligent ways Go resizes the slices this is not a problem and the slice implementation of both stack and queue in Go will perform better than the linked list implementation.
79
97
80
98
## Application
81
99
82
100
Stacks are helpful when LIFO operations are desired. Many [graph](../graph) problems are solved with stacks.
83
101
84
-
During process execution, a portion of memory known as the "stack" is reserved to hold stack frames. Whenever a function is called, relevant data such as parameters, local variables, and return values are stored within a frame to be accessed after the function has been completed. When an excessive number of function calls or an infinite recursive function are made, the computer's ability to store all of this information is exceeded. This results in the well-known stack overflow error.
102
+
During process execution in operating systems, memory is divided into "stack" and "heap". The stack portion of the memory is used whenever a function is called. Relevant data such as parameters, local variables, and return values are stored within a frame in a stack to be popped after the function has been completed. When an excessive number of function calls or an infinite recursive function are made, the computer's ability to store all of this information is exceeded. This results in the well-known stack overflow error.
0 commit comments