Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 15

Data Structure and Algorithm

By- Astha Tripathi


Linked List

• A linked list is a linear data structure, in which the elements are not stored at
contiguous memory locations. The elements in a linked list are linked using pointers
as shown in the below image:
Cont..
• A linked list is a linear data structure that includes a series of connected nodes. Here, each node stores
the data and the address of the next node.
• You have to start somewhere, so we give the address of the first node a special name called HEAD.
Also, the last node in the linked list can be identified because its next portion points to NULL.
How to represent linked list
• struct node
• {
• int data;
• struct node *next;
• };
Types of linked list
There are three common types of Linked List.
• Singly Linked List
• Doubly Linked List
• Circular Linked List

Singly Linked List


It is the most common. Each node has data and a pointer to the next node.
Cont..
Cont..
Doubly Linked List
We add a pointer to the previous node in a doubly-linked list. Thus, we can go in either direction:
forward or backward.
Cont..
Circular Linked List
A circular linked list is a variation of a linked list in which the last element is linked to the first element.
This forms a circular loop.
Characteristics of Linked List

• Linked list is a linear data structure.


• Linked list nodes are stored randomly in memory and are allocated during run time.
• Each node consists of value/data and a pointer/link which is the address of the next node in
the linked list.
• Linked list uses extra memory to store links.
• Linked List can grow or shrink at any point of time easily.
• No need to know the size of the elements during initialization of the linked list.
• First node of the linked list is called as Head
• Last node next always points to null.
Linked list is a linear data structure and allows following basic operation:

• Insertion : adds a new element to the linked list


• Deletion : delete existing element form the linked list
• Searching : search for an element by its value in the linked list
• Traversal : traverse all elements starting form head in the linked list
Array data structure
An array is a collection of items stored at contiguous memory locations. The idea is to store multiple
items of the same type together. This makes it easier to calculate the position of each element by simply
adding an offset to a base value, i.e., the memory location of the first element of the array (generally
denoted by the name of the array)

There are some of the properties of an array that are listed as follows -
• Each element in an array is of the same data type and carries the same size that is 4 bytes.
• Elements in the array are stored at contiguous memory locations from which the first element is stored
at the smallest memory location.
• Elements of the array can be randomly accessed since we can calculate the address of each element of
the array with the given base address and the size of the data element.
Cont..

• Index starts with 0.


• The array's length is 10, which means we can store 10 elements.
• Each element in the array can be accessed via its index.
Memory allocation of an array

As stated above, all the data elements of an array


are stored at contiguous locations in the main
memory. The name of the array represents the
base address or the address of the first element in
the main memory. Each element of the array is
represented by proper indexing.

We can define the indexing of an array in the


below ways
• 0 (zero-based indexing): The first element of the
array will be arr[0].
• 1 (one-based indexing): The first element of the
array will be arr[1].
• n (n - based indexing): The first element of the
array can reside at any random index number.
Array vs. Linked list
Questions
Question 1: 15, 10, 20, 8, 12, 18, 25, 19, 30, 16 insert these keys in BST.

Question 2: The preorder traversal sequence of a binary search tree is-


30 , 20 , 10 , 15 , 25 , 23 , 39 , 35 , 42
What one of the following is the postorder traversal sequence of the same tree?
A-10 , 20 , 15 , 23 , 25 , 35 , 42 , 39 , 30
B-15 , 10 , 25 , 23 , 20 , 42 , 35 , 39 , 30
C-15 , 20 , 10 , 23 , 25 , 42 , 35 , 39 , 30
D-15 , 10 , 23 , 25 , 20 , 35 , 42 , 39 , 30

Question 3 : Preorder : A B D E C F G
Inorder : D B E A F C G
What is postorder ?

You might also like