array implementation and linked list as datat structure
TRANSCRIPT
ARRAY IMPLEMENTATION AND LINKED LIST AS A
DATA STRUCTUREPRESENTED BY:
AKSHAY WADALKAR
An array is a collection of elements of similar datatype.
Contiguous memory allocation takes place.
An array is a DS in which we can access every element directly using position variable .
It is rather an organizational concept.
Array elements can be accessed individually.
Syntax: datatype nameofarray [dimension];
ARRAY
Two types of array-1. Single dimensional
single for loop.
2. Multidimensional
nesting of for loop.
Types of array
Array can be of integer ,character and string.
Integer and character array can be implemented by same logic
Implementation of string array is quiet different from the two.
We can study the array implementation using integer array.
Single dimensional array
7143258058
169
23
a[0] i=0
a[1] i=1
a[2] i=2
a[3] i=3
a[4] i=4
a[5] i=5
a[6] i=6
a[7] i=7
a[8] i=8
a[9] i=9
int a[10]={7,1,32,58,0,5,8,16,9,23};
Integer array “a”.
It is of dimension 10 (from 0 to 9).
Take positing variable i.
Its storage will be continuous 20 bytes(2 bytes each).
Creation of integer array
ALGORITHM FOR INSERTION(1 diamension)
1. DECLARATIONN SIZE
2. OPERATIONRepeat for i= 0 to (size-1)arr[i]= numend repeat
3. OUTPUTRETURN(arr[i])
DECLARATIONi rowsj coloumn OPERATION
◦ Repeat for i=0 to (rows-1) Repeat for j=0 to (coloumn-1)
Array[i][j]=num End repeat
◦ End repeat OUTPUT
Return(Array[i][j])
ALGORITHM FOR INSERTION(2 diamension)
No need to declare large number of variables individually.
Variables are not scattered in memory , they are stored in contiguous memory.
Ease the handling of large no of variables of same datatype.
Advantages
Rigid structure.
Can be hard to add/remove elements.
Cannot be dynamically resized in most languages.
Memory loss.
LIMITATION
LINKED LISTAS A DATA STRUCTURE
Each element (node) inside a linked list is linked to the previous node and successor (next) node.
This allows for more efficient insertion and deletion of nodes.
WHAT IS A LINKED LIST?
5 3 14 2
continued
Each item has a data part (one or more data members), and a link that points to the next item.
One natural way to implement the link is as a pointer; that is, the link is the address of the next item in the list.
It makes good sense to view each item as an object, that is, as an instance of a class.
We call that class: Node
The last item does not point to anything. We set its link member to NULL. This is denoted graphically by a self-loop
Definition Details
Insert a new item◦ At the head of the list, or◦ At the tail of the list, or◦ Inside the list, in some designated position
Search for an item in the list◦ The item can be specified by position, or by
some value Delete an item from the list
◦ Search for and locate the item, then remove the item, and finally adjust the surrounding pointers
Operations on Linked Lists
Suppose you want to find the item whose data value is A
You have to search sequentially starting from the head item rightward until the first item whose data member is equal to A is found.
At each item searched, a comparison between the data member and A is performed.
Searching for an Item
•Since nodes in a linked list have no names, we use two pointers, pre (for previous) and cur (for current).
•At the beginning of the search, the pre pointer is null and the cur pointer points to the first node.
•The search algorithm moves the two pointers together towards the end of the list.
LOGIC FOR SEARCHING A LINKED LIST
ALGORITHM Declaration
◦ Current 0 Searching
◦ for (current = first; current != NULL; current = current->next)
◦ if (searchItem == current(data)) ◦ return (current);◦ Break
Output◦ return (NULL);
Example
Insertion of an Element at the Head :
B efore the insertion :
hea d
nex t nex t nex t
ele m ent ele m ent ele m ent
R om e S eattle To ronto
H a v e a n e w n o d e :
h e a d
n e x t
e l e m e n t
n e x t n e x tn e x t
e l e m e n t e l e m e n t e l e m e n t
B a l t i m o r e R o m e S e a t t l e T o r o n t o
Node x = new Node();x.setElement(new String(“Baltimore”));The following statement is not correct:x.element = new String(“Baltimore”));
A f t e r t h e i n s e r t i o n :
h e a d
n e x t
e l e m e n t
n e x t n e x tn e x t
e l e m e n t e l e m e n t e l e m e n t
B a l t i m o r e R o m e S e a t t l e T o r o n t o
x.setNext(head);head = x;
Deleting an Element at the Head :
B efo re th e d e le tio n :
h ea d
n ex t
e le m en t
n ex t n ex tn ex t
e le m en t e le m en t e le m en t
B alt im ore R om e S ea tt le T o ronto
R e m o v e t h e n o d e f r o m t h e l i s t :
h e a d
n e x t
e l e m e n t
n e x t n e x tn e x t
e l e m e n t e l e m e n t e l e m e n t
B a l t i m o r e R o m e S e a t t l e T o r o n t o
head = head.getNext();
A f te r th e d e le t io n :
h e a d
n e x t n e x t n e x t
e le m e n t e le m e n t e le m e n t
R o m e S e a tt le T o ro n t o
Insertion of an Element at the Tail :
Before the insertion:
head
next next next
element element element
Rome Seattle Toronto
tail
H a v e a n e w n o d e :
h e a d
n e x t
e l e m e n t
n e x t n e x t n e x t
e l e m e n t e l e m e n t e l e m e n t
B a l t i m o r eR o m e S e a t t l e T o r o n t o
t a i l
Node x = new Node( );x.setElement(new String(“Baltimore”));x.setNext(null);tail.setNext(x);tail = x;
A f t e r t h e i n s e r t i o n :
h e a d
n e x t
e l e m e n t
n e x t n e x t n e x t
e l e m e n t e l e m e n t e l e m e n t
B a l t i m o r eR o m e S e a t t l e T o r o n t o
t a i l
Deleting an Element at the Tail :
Deletion of an element at the tail of a singly linked list takes more effort. The difficulty is related with the fact that the last node does not have a link to the previous node which will become the new tail of the list.
B e f o r e t h e d e l e t i o n :
h e a d
n e x t
e l e m e n t
n e x t n e x t n e x t
e l e m e n t e l e m e n t e l e m e n t
B a l t i m o r eR o m e S e a t t l e T o r o n t o
t a i l
R e m o v e t h e n o d e : H o w c a n w e f i n d t h e n e w t a i l ?
n e x t
e l e m e n t
n e x t n e x t n e x t
e l e m e n t e l e m e n t e l e m e n t
B a l t i m o r eR o m e S e a t t l e T o r o n t o
h e a d t a i l ?
should be removed
Singly Linked Lists and Arrays
Singly linked list Array Elements are stored in linear order, accessible with links. Do not have a fixed size. Cannot access the previous element directly. No binary search.
Elements are stored in linear order, accessible with an index. Have a fixed size. Can access the previous element easily. Binary search.
CS314 Linked Lists 31
Linked lists are dynamic, they can grow or shrink as necessary
Linked lists are non-contiguous; the logical sequence of items in the structure is decoupled from any physical ordering in memory
Advantages of linked lists
•A linked list is a very efficient data structure for sorted list that will go through many insertions and deletions.
•A linked list is a dynamic data structure in which the list can start with no nodes and then grow as new nodes are needed. A node can be easily deleted without moving other nodes, as would be the case with an array.
•For example, a linked list could be used to hold the records of students in a school. Each quarter or semester, new students enroll in the school and some students leave or graduate.
Applications of linked lists
THANK YOU…………….!!!