Data Structure and Algorithm Lesson 2
Data Structure and Algorithm Lesson 2
Data Structure and Algorithm Lesson 2
STRUCTURES
Arrays Trees
Linked Lists Heaps
Stacks Hash tables
Queues Graphs
Introduction to Arrays
An array is a collection of items of the same variable type
that are stored at contiguous memory locations.
2
Basic Terminologies of Array
Array Index:
In an array, elements are identified by
their indexes. Array index starts from
0.
Array element:
Elements are items stored in an array
and can be accessed by their index.
Array Length:
The length of an array is determined by
the number of elements it can contain.
Linked Lists
A linked list is a linear data structure
consisting of a sequence of elements
called nodes, where each node
contains both the data and a reference
(or pointer) to the next node in the
sequence.
5
Types of Linked Lists
There are several types of linked lists, but the two
most common ones are:
1.Singly Linked List: In this type of linked list, each
node contains data and a pointer to the next node in
the sequence. The last node points to null, indicating
the end of the list.
2.Doubly Linked List: Each node in a doubly linked
list contains data, a pointer to the next node, and a
pointer to the previous node. This allows for traversal
in both directions. 6
OPERATIONS ON LINKED LISTS:
1.Insertion: Adding a new node to the
list.
2.Deletion: Removing a node from the
list.
3.Traversal: Iterating through the list to
access or manipulate its elements.
4.Search: Finding a specific element
within the list.
5.Concatenation: Combining two linked
lists into one.
7
Insertion:
Example: Suppose we have a singly linked list with nodes containing
integers. To insert a new node with the value 10 at the end of the list:
Linked List: 'H' -> 'e' -> 'l' -> 'l' -> 'o' -> NULL
Traversal Output: Hello
Search:
Example: Consider the same linked list from the traversal example. If we
want to search for the character 'l' in the list:
Linked List: 'H' -> 'e' -> 'l' -> 'l' -> 'o' -> NULL
Search for 'l': Found at index 2 and 3
Concatenation:
Example: Suppose we have two singly linked lists, list1 and list2, with the
following elements:
struct Node {
int data;
struct Node* next;
};
return 0;
}
Stacks:
•Definition and properties of stacks (Last In First Out - LIFO).
•Implementation of stacks using arrays or linked lists.
•Operations on stacks (push, pop, peek).
•Applications of stacks in expression evaluation, function
calls, and backtracking algorithms.
16
Definition and Properties of Stacks
(Last In First Out - LIFO):
18
Operations on Stacks (push, pop, peek):
19
Applications of Stacks
•Expression Evaluation
•Function Calls
•Backtracking Algorithms
20