0% found this document useful (0 votes)
56 views26 pages

Data Structures and Algorithms Arrays and Linked LIST

The document discusses data structures and algorithms related to arrays and linked lists. It provides information on array elements, indexes, basic array operations like traversal, insertion, deletion, search and update. It also defines linked lists, the basic components of nodes and links, different types of linked lists, and basic linked list operations like insertion, deletion and display.

Uploaded by

KTW gaming
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
0% found this document useful (0 votes)
56 views26 pages

Data Structures and Algorithms Arrays and Linked LIST

The document discusses data structures and algorithms related to arrays and linked lists. It provides information on array elements, indexes, basic array operations like traversal, insertion, deletion, search and update. It also defines linked lists, the basic components of nodes and links, different types of linked lists, and basic linked list operations like insertion, deletion and display.

Uploaded by

KTW gaming
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1/ 26

Data Structures

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.

•Element − Each item stored in an array is called an element.


•Index − Each location of an element in an array has a numerical index,
which is used to identify the element.
Array Representation
Arrays can be declared in various ways in different languages. For
illustration, let's take C array declaration.

Arrays can be declared in various ways in different languages. For


illustration, let's take C array declaration.
As per the above illustration, following are the important points to be
considered.

•Index starts with 0.


•Array length is 10 which means it can store 10 elements.
•Each element can be accessed via its index. For example, we can fetch
an element at index 6 as 9.
Basic Operations

Following are the basic operations supported by an array.

•Traverse − print all the array elements one by one.


•Insertion − Adds an element at the given index.
•Deletion − Deletes an element at the given index.
•Search − Searches an element using the given index or by the value.
•Update − Updates an element at the given index.
In C, when an array is initialized with size, then it assigns
defaults values to its elements in following order.
Data Type Default Value
bool true or false
char ‘a’ or ‘b’
int 123 (whole number)
float 0.00 (7 digits)
double 0.00 (15 digits)
void
wchar_t 0
Traverse Operation
This operation is to traverse through the elements of an 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;

printf("The original array elements are :\n");


for(i = 0; i<n; i++) {
printf("LA[%d] = %d \n", i, LA[i]);
}
n = n + 1;
while( j >= k) {
LA[j+1] = LA[j];
j = j - 1; }
LA[k] = item;
printf("The array elements after insertion :\n");
for(i = 0; i<n; i++) {
printf("LA[%d] = %d \n", i, LA[i]);
}
}
Output
The original array elements are :
LA[0] = 1
LA[1] = 3
LA[2] = 5
LA[3] = 7
LA[4] = 8

The array elements after insertion :


LA[0] = 1
LA[1] = 3
LA[2] = 5
LA[3] = 10
LA[4] = 7
LA[5] = 8
Deletion Operation
Deletion refers to removing an existing element from the array and re-organizing all elements of an
array.

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

Circular Doubly Linked


Linked List Representation
Linked list can be visualized as a chain of nodes, where every node points to the
next node.

As per the above illustration, following are the important points to be considered.

•Linked List contains a link element called first.


•Each link carries a data field(s) and a link field called next.
•Each link is linked with its next link using its next link.
•Last link carries a link as null to mark the end of the list.
Types of Linked List

Following are the various types of linked list.

•Simple Linked List − Item navigation is forward only.


•Doubly Linked List − Items can be navigated forward and backward.
•Circular Linked List − Last item contains link of the first element as
next and the first element has a link to the last element as previous.
Basic Operations

Following are the basic operations supported by a list.

•Insertion − Adds an element at the beginning of the list.


•Deletion − Deletes an element at the beginning of the list.
•Display − Displays the complete list.
•Search − Searches an element using the given key.
•Delete − Deletes an element using the given key.
Insertion Operation
Adding a new node in linked list is a more than one step activity. We shall learn this with
diagrams here. First, create a node using the same structure and find the location where it
has to be inserted.

Imagine that we are inserting a node B (NewNode), between A (LeftNode)


and C (RightNode). Then point B.next to C −

It should look like this


Now, the next node at the left should point to the new node.

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

You might also like