Data Structures and Algorithms Arrays and Linked LIST
Data Structures and Algorithms Arrays and Linked LIST
and Algorithms -
Arrays
Array is a container which can hold a fix number of items and these items
should be of the same type. Most of the data structures make use of arrays
to implement their algorithms. Following are the important terms to
understand the concept of Array.
Example
Following program traverses and prints the elements of an array:
#include <stdio.h>
int main() {
int LA[] = {1,3,5,7,8};
int item = 10, k = 3, n = 5;
int i = 0, j = n;
printf("The original array elements are :\n");
for(i = 0; i<n; i++) {
printf("LA[%d] = %d \n", i, LA[i]);
}
}
When we compile and execute the above program, it produces the following result −
Output
The original array elements are :
LA[0] = 1
LA[1] = 3
LA[2] = 5
LA[3] = 7
LA[4] = 8
Insertion Operation
Insert operation is to insert one or more data elements into an array. Based on the
requirement, a new element can be added at the beginning, end, or any given index of array.
Here, we see a practical implementation of insertion operation, where we add data at the
end of the array −
Example
Following is the implementation of the above algorithm −
#include <stdio.h>
int main() {
int LA[] = {1,3,5,7,8};
int item = 10, k = 3, n = 5;
int i = 0, j = n;
Algorithm
Consider LA is a linear array with N elements and K is a positive integer such that K<=N.
Following is the algorithm to delete an element available at the Kth position of LA.
1. Start
2. 2. Set J = K
3. 3. Repeat steps 4 and 5 while J < N
4. 4. Set LA[J] = LA[J + 1]
5. 5. Set J = J+1
6. 6. Set N = N-1 7.
7. Stop
Search Operation
You can perform a search for an array element based on its value or its index.
Algorithm
Consider LA is a linear array with N elements and K is a positive integer such
that K<=N. Following is the algorithm to find an element with a value of ITEM using
sequential search.
1. Start
2. 2. Set J = 0
3. 3. Repeat steps 4 and 5 while J < N
4. 4. IF LA[J] is equal ITEM THEN GOTO STEP 6
5. 5. Set J = J +1
6. 6. PRINT J, ITEM
7. 7. Stop
Update Operation
Update operation refers to updating an existing element from the array at a
given index.
Algorithm
Consider LA is a linear array with N elements and K is a positive integer
such that K<=N. Following is the algorithm to update an element available
at the Kth position of LA.
1. Start
2. 2. Set LA[K-1] = ITEM
3. 3. Stop
Data Structure and
Algorithms - Linked
List
A linked list is a sequence of data structures, which are connected together via
links.
Linked List is a sequence of links which contains items. Each link contains a
connection to another link. Linked list is the second most-used data structure
after array. Following are the important terms to understand the concept of
Linked List.
•Link − Each link of a linked list can store a data called an element.
•Next − Each link of a linked list contains a link to the next link called Next.
•LinkedList − A Linked List contains the connection link to the first link called
First.
Example Code:
Class Node {
Int data;
Node next;
Node(int data) { • Node nodeA = new Node(6);nodeA.next = nodeB;
this.data = data ; • Node nodeB = new Node(3);nodeB.next = nodeC;
}
} • Node nodeC = new Node(4);nodeC.next = nodeD;
• Node nodeD = new Node(2);nodeD.next = nodeE;
• Node nodeE = new Node(1);
Double Linked List
Simple Linked List
As per the above illustration, following are the important points to be considered.
This will put the new node in the middle of the two. The new list should look like this −
Deletion Operation
Deletion is also a more than one step process. We shall learn with pictorial
representation. First, locate the target node to be removed, by using searching
algorithms.
The left (previous) node of the target node now should point to the next node of the
target node −
This will remove the link that was pointing to the target node. Now, using the
following code, we will remove what the target node is pointing at.
We need to use the deleted node. We can keep that in memory otherwise we can
simply deallocate memory and wipe off the target node completely.
Reverse Operation
This operation is a thorough one. We need to make the last node to be pointed
by the head node and reverse the whole linked list.
First, we traverse to the end of the list. It should be pointing to NULL. Now,
we shall make it point to its previous node −
We have to make sure that the last node is not the last node. So we'll have some temp
node, which looks like the head node pointing to the last node. Now, we shall make all
left side nodes point to their previous nodes one by one.
Except the node (first node) pointed by the head node, all nodes should point to their
predecessor, making them their new successor. The first node will point to NULL.
We'll make the head node point to the new first node by using the temp node.
THANKYOU FOR
LISTENING!!!
NAME OF REPORTERS (GROUP 4)
Anadon, John Aldrin
Castillo, Christian
Cornesio, Christian Jumil
Del Rosario, Carl Joseph
Encinas, Jennifer
Ilacad, Ranel
Laude, Riley Brandon
Medenilla, Jose