Docsity

Data Structures and Algorithms: Array and Linked List Implementation, Lecture notes of Data Structures and Algorithms

Toggle sidebar
bg1
Lim, Eliza Joyce B. Data Structures and Algorithm March 23, 2022
BSCS 1-1 SOL 4
Array and Linked List Implementation
As said in the previous lesson, an array is a fixed sized sequential collection of
elements of same data types that share a common name. The elements of an array can
be accessed individually with an index. Syntax: type array-name[size]. For a single
dimensional array, it will need a single for loop. Array can be of integer, character and
string. Integer and array can be implemented by same logic, like the example below.
String array is quite different.
Int a[10] = {7,14,32,45,65,0,26,11,42,16};
The example is integer array “a” with
dimension 10 (from 0 to 9). It has indexes
and data. On the other hand, a
multidimensional array will need nesting of
for loops.
Advantages of using an array is that there is no need to
declare large number of variables individually, variables are not scattered in memory but
rather in a contiguous memory, and it eases the handling of large number of variables of
same datatype. The limitations are: it has rigid structure, can be hard to add/remove
elements, cannot be dynamically resized in most languages, and memory loss.
Another topic is the linked list. Each element (node) inside a linked list is linked to
the previous node and successor (next) node. This allows for more efficient insertion
and deletion of nodes.
data
link
data
link
data
null
Node
Head
Node
Node
Each item has a data part (one or more data members), and a link that points to
the next item. Way to implement the link is as a pointer—the link is the address of the
next item in the list. The last item, however does not point to anything (set as NULL).
This is denoted graphically by a self-loop.
Operations on linked lists are insert, search, and delete. Insert a new item at
the head of the list or at the tail of the list, or inside the list in some designated position.
Search for an item in the list—the item can be specified by position or by some value.
And delete an item from the list: search and locate the item first, then remove the item
and then adjust the surrounding pointers. Let's say you're looking for an item with the
7
32
45
65
0
26
11
42
16
a[0]
a[1]
a[2]
a[3]
a[4]
a[5]
a[6]
a[7]
a[8]
a[9]
1. Declaration:
N size
2. Operation:
Repeat for i=0 to (size-1)
arr[i]=num
End repeat
3. Output:
Return(arr[i])
1. Declaration:
i rows
j column
2. Operation:
Repeat for i=0 to (rows-1)
Repeat for j=0 to (column-1)
Array[i][j]=num
End repeat
End repeat
3. Output:
Return(Array[i][j])
pf2

Partial preview of the text

Download Data Structures and Algorithms: Array and Linked List Implementation and more Lecture notes Data Structures and Algorithms in PDF only on Docsity!

Lim, Eliza Joyce B. Data Structures and Algorithm March 23, 2022 BSCS 1-1 SOL 4

Array and Linked List Implementation

As said in the previous lesson, an array is a fixed sized sequential collection of elements of same data types that share a common name. The elements of an array can be accessed individually with an index. Syntax: type array-name[size]. For a single dimensional array , it will need a single for loop. Array can be of integer, character and string. Integer and array can be implemented by same logic, like the example below. String array is quite different.

Int a[10] = {7,14,32,45,65,0,26,11,42,16}; The example is integer array “a” with dimension 10 (from 0 to 9). It has indexes and data. On the other hand, a multidimensional array will need nesting of for loops.

Advantages of using an array is that there is no need to declare large number of variables individually, variables are not scattered in memory but rather in a contiguous memory, and it eases the handling of large number of variables of same datatype. The limitations are: it has rigid structure, can be hard to add/remove elements, cannot be dynamically resized in most languages, and memory loss.

Another topic is the linked list. Each element (node) inside a linked list is linked to the previous node and successor (next) node. This allows for more efficient insertion and deletion of nodes.

data link  data link  data null Node

Head

Node Node

Each item has a data part (one or more data members), and a link that points to the next item. Way to implement the link is as a pointer—the link is the address of the next item in the list. The last item, however does not point to anything (set as NULL). This is denoted graphically by a self-loop.

Operations on linked lists are insert, search, and delete. Insert a new item at the head of the list or at the tail of the list, or inside the list in some designated position. Search for an item in the list—the item can be specified by position or by some value. And delete an item from the list: search and locate the item first, then remove the item and then adjust the surrounding pointers. Let's say you're looking for an item with the

a[0] a[1] a[2] a[3] a[4] a[5] a[6] a[7] a[8] a[9]

  1. Declaration: N size
  2. Operation: Repeat for i=0 to (size-1) arr[i]=num End repeat
  3. Output: Return(arr[i]) 1. Declaration: i rows j  column 2. Operation: Repeat for i=0 to (rows-1) Repeat for j=0 to (column-1) Array[i][j]=num End repeat End repeat 3. Output: Return(Array[i][j])

Lim, Eliza Joyce B. Data Structures and Algorithm March 23, 2022 BSCS 1-1 SOL 4

data value A. You must search sequentially, starting from the head item rightward until you find the first item whose data member equals A. Comparison between the data member and A is conducted for each item searched.

Since nodes in a linked list have no names, we use two pointers, pre (for previous) and cur (for current). At the beginning of the search, the pre pointer is null and the cur pointer points to the first node. The search algorithm moves the two pointers together towards the end of the list.

Insertion of an element can be at the head or at the tail. Assume the 1000, 2000, 3000 are addresses of an item.

Rome 2000 ^ Seattle 3000 ^ Toronto null 1000

1000 Head

Tail Deletion of an element at the head :

Seattle 3000  Toronto null 2000

2000 Head

Tail Deletion of an element at the ta il of a singly linked list takes more effort. The difficulty is related with the fact that the last node does not have a link to the previous node which will become the new tail of the list.

Array is the sequential representation of a list. While, Linked List is a linked representation of a list. Unlike array, elements in a linked list are stored in linear order, accessible with links; do not have a fixed size; cannot access the previous element directly; and no binary search. Its advantages are: they’re dynamic, they can grow or shrink as necessary; and they are non-contiguous, meaning the logical sequence of items in the structure is decoupled from any physical ordering in memory.

A linked list very efficient for a sorted list that will be subjected to numerous insertions and deletions. A linked list is a dynamic data structure that can begin with no nodes and grow when new nodes are required. As with an array, a node can be simply deleted without causing other nodes to move. A linked list, for example, could be used to store student records in a school.