Unit Ii: 18Csc201J - Data Structures and Algorithms
Unit Ii: 18Csc201J - Data Structures and Algorithms
To access all the elements, we must use a loop. That is, we can access all the elements of an array by varying the
value of the subscript into the array. But note that the subscript must be an integral value or an expression that
evaluates to an integral value.
Example of array declaration
int Ar[10];
• To access an individual element we must apply a subscript to array named Ar.
• A subscript is a bracketed expression.
• The expression in the brackets is known as the index.
• First element of array has index 0.
Ar[0]
• Second element of array has index 1, and so on.
Ar[1], Ar[2], Ar[3],…
0 1 2 3 4 5 6 7 8 9
Ar -- -- -- -- -- -- -- -- -- --
Array applications
• Given a list of test scores, determine the maximum and minimum
scores.
• Read in a list of student names and rearrange them in alphabetical
order (sorting).
• Given the height measurements of students in a class, output the
names of those students who are taller than average.
Operations on array elements
• Consider
int Ar[10], i = 7, j = 2, k = 4;
Ar[0] = 1;
Ar[i] = 5;
Ar[j] = Ar[i] + 3;
Ar[j+1] = Ar[i] + Ar[0];
Ar[Ar[j]] = 12;
cin >> Ar[k];
Array Initialization
int Ar[10] = {9, 8, 7, 6, 5, 4, 3, 2, 1, 0};
0 1 2 3 4 5 6 7 8 9
Ar 9 8 7 6 5 4 3 2 1 0
Ar[3] = -1;
0 1 2 3 4 5 6 7 8 9
Ar 9 8 7 -1 5 4 3 2 1 0
Array Initialization
OPERATIONS ON ARRAYS
There are a number of operations that can be preformed on arrays.
These operations include:
• Traversing an array
• Inserting an element in an array
• Searching an element in an array
• Deleting an element from an array
• Merging two arrays
• Sorting an array in ascending or descending order
Algorithm for array traversal
Searching the list for a particular value. It can be applied where the number of elements
cannot be predicted beforehand.
A B C
Circular Linked list
The pointer from the last element in the list points back to the first element.
head
A B C
Doubly linked list
head tail
A B C
Singly Linked List (SLL)
Singly Linked List (SLL)
• In C, we can create a node using the following code:
struct node
{
int data;
struct node *next;
};
Operations on SLL:
1. Traversal
2. Searching
3. Insertion
4. Deletion
Traversing a Linked List
• Traversing a linked list means accessing the nodes of the list in order
to perform some processing on them. Remember a linked list always
contains a pointer variable START which stores the address of the first
node of the list.
Searching for a Value in a Linked List
Searching a linked list means to find a particular element in the linked
list. As already discussed, a linked list consists of nodes which are
divided into two parts, the information part and the next part. So
searching means finding whether a given value is present in the
information part of the node or not.
Inserting a New Node in a Linked List
• Case 1: The new node is inserted at the beginning.
• Case 2: The new node is inserted at the end.
• Case 3: The new node is inserted after a given node.
• Case 4: The new node is inserted before a given node.
OVERFLOW:
Overflow is a condition that occurs when AVAIL = NULL or no free
memory cell is present in the system. When this condition occurs, the
program must give an appropriate message.
Inserting a Node at the Beginning of a Linked
List
Algorithm:
Inserting a Node at the Beginning of a Linked
List
Example:
Inserting a Node at the End of a Linked List
Algorithm:
Inserting a Node After a Given Node in a Linked
List
Algorithm:
Inserting a Node Before a Given Node in a
Linked List
• Algorithm:
Deleting a Node from a Linked List
Consider three cases and how deletion is done in each case.
• Case 1: The first node is deleted.
• Case 2: The last node is deleted.
• Case 3: The node after a given node is deleted.
Deleting the First Node from a Linked List
Algorithm:
Deleting the Last Node from a Linked List
• Algorithm:
Deleting the Node After a Given Node in a
Linked List
• Algorithm:
CIRCULAR LINKED LIST
CIRCULAR LINKED LIST
In a circular linked list, the last node contains a pointer to the first node
of the list. We can have a circular singly linked list as well as a circular
doubly linked list.
While traversing a circular linked list, we can begin at any node and
traverse the list in any direction, forward or backward, until we reach
the same node where we started.
Thus, a circular linked list has no beginning and no ending
Operations on a Circular Linked List
1. Insertion
2. Deletion
3. Traversal
4. Search
Inserting a New Node in a Circular Linked List
• Case 1: The new node is inserted at the beginning of the circular
linked list.
Algorithm:
Inserting a New Node in a Circular Linked List
• Case 2: The new node is inserted at the end of the circular linked list.
Algorithm:
Deleting a Node from a Circular Linked List
Case 1: The first node is deleted.
Algorithm:
Deleting a Node from a Circular Linked List
• Case 2: The last node is deleted.
DOUBLY LINKED LISTS
DOUBLY LINKED LISTS
• A doubly linked list or a two-way linked list is a more complex type of
linked list which contains a pointer to the next as well as the previous
node in the sequence.
• Therefore, it consists of three parts—data, a pointer to the next node,
and a pointer to the previous node.
Inserting a New Node in a Doubly Linked List
Insertion is done in following cases:
Case 1: The new node is inserted at the beginning
• Case 2: The new node is inserted at the end.
• Case 3: The new node is inserted after a given node.
• Case 4: The new node is inserted before a given node.
Inserting a Node at the Beginning of a Doubly
Linked List
Algorithm:
Inserting a Node at the End of a Doubly Linked
List
Algorithm:
Inserting a Node After a Given Node in a
Doubly Linked List
Algorithm:
Deleting a Node from a Linked List
Deletion can be done in following ways:
• Case 1: The first node is deleted.
• Case 2: The last node is deleted.
• Case 3: The node after a given node is deleted.
• Case 4: The node before a given node is deleted..
Deleting the First Node from a Doubly Linked
List
• Algorithm:
Deleting the Last Node from a Doubly Linked
List
• Algorithm:
Deleting the Node After a Given Node in a
Doubly Linked List
• Algorithm:
Deleting the Node Before a Given Node in a
Doubly Linked List
• Algorithm:
Cursor based implementation of a Linked List
Cursor based implementation of a Linked List
Many languages, such as BASIC and FORTRAN, do not support pointers. If linked lists are
required and pointers are not available, then an alternate implementation must be used.
The alternate method we will describe is known as a cursor implementation.
The two important items present in a pointer implementation of linked lists are
1. The data is stored in a collection of structures. Each structure contains the data and a
pointer to the next structure.
2. A new structure can be obtained from the system's global memory by a call to malloc
and released by a call to free.
Our cursor implementation must be able to simulate this. The logical way to satisfy
condition 1 is to have a global array of structures. For any cell in the array, its array index
can be used in place of an address.
We must now simulate condition 2 by allowing the equivalent of malloc and free for cells in
the CURSOR_SPACE array. To do this, we will keep a list (the freelist) of cells that are not in
any list. The list will use cell 0 as a header.
Cursor based implementation of a Linked List
• To perform an malloc, the first element (after the header) is removed
from the freelist.
Cursor based implementation of a Linked List
• An initialized CURSOR_SPACE looks as below:
Cursor based implementation of a Linked List
• Given this, the cursor implementation of linked lists is
straightforward. For consistency, we will implement our lists with a
header node. The routine for allocation and free is given below:
Cursor based implementation of a Linked List
• Example of a cursor implementation of linked lists contains two lists.
Cursor based implementation of a Linked List
• Function to test whether a linked list is empty--cursor implementation
Polynomial arithmetic
APPLICATIONS OF LINKED LISTS
• Linked lists can be used to represent polynomials and the different
operations that can be performed on them. In this section, we will see how
polynomials are represented in the memory using linked lists.
• Polynomial Representation:
• Let us see how a polynomial is represented in the memory using a linked
list. Consider a polynomial 6x^3 + 9x^2 + 7x + 1.
• Every individual term in a polynomial consists of two parts, a coefficient
and a power.
• Here, 6, 9, 7, and 1 are the coefficients of the terms that have 3, 2, 1, and 0
as their powers respectively.
• Every term of a polynomial can be represented as a node of the linked
list.
• The following polynomial equation can be represented in the form of
linked list as follows:
• operations on polynomials can also be performed using linked list.
• Structure of a node :
Create a polynomial linked list
Add two polynomials using linked list
Difference of two polynomials using Linked
list
Displaying a polynomial using linked list
Add a node to the existing polynomial