Download Linked Lists - WordPress.com

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Lattice model (finance) wikipedia , lookup

Red–black tree wikipedia , lookup

Quadtree wikipedia , lookup

Interval tree wikipedia , lookup

B-tree wikipedia , lookup

Binary search tree wikipedia , lookup

Linked list wikipedia , lookup

Transcript
Data Structures &
Algorithms
Atif Zohaib Khan
Linked Lists


Linked list is an ordered set of data elements called nodes,
each containing a link to its successor i.e. next node (and
sometimes its predecessor i.e. previous nodes).
Each node consists of two parts. A data element and
reference to the next node.
Representation in Memory



Linked List requires two linear
arrays INFO and LINK.
INFO is information part and LINK
is next pointer field.
Each Linked List has its own
pointer variable giving location of
its first node.
Types of Linked List



Singly Linked List − Item Navigation is forward only.
Doubly Linked List − Items can be navigated forward and
backward way.
Circular Linked List − Last item contains link of the first
element as next and first element has link to last element as
prev.
Single Linked List

Nodes contains link to next node only and last node points to
null.
Basic Operations





Traverse − Processing each element exactly once.
Insertion − add an element at the beginning of the list.
Deletion − delete an element at the beginning of the list.
Display − displaying complete list.
Search − search an element using given key.
Traversing



Traversing means to process each node exactly once.
It uses a pointer variable PTR which points to the node that is
currently being processed.
LINK[PTR] points to the next node to be processed.
Algorithm for Traversing
1.
2.
3.
4.
5.
Set PTR : = START.
Repeat Steps 3 and 4 while PTR ≠ NULL.
Apply PROCESS to INFO[PTR].
Set PTR : = LINK[PTR].
Exit.
Print Information at each Node
1.
2.
3.
4.
5.
Set PTR : = START.
Repeat Steps 3 and 4 while PTR ≠ NULL.
Write : INFO[PTR].
Set PTR : = LINK[PTR].
Return.
Searching in Linked List

Searching a specific ITEM from Linked List can be done
using two ways:
–
–
When Linked List is Unsorted.
When Linked List is Sorted.
Searching Algorithm for Unsorted List
Set PTR : = START.
2. Repeat Step 3 while PTR ≠ NULL:
3.
If ITEM : = INFO[PTR], then:
Set LOC : = PTR, and Exit.
Else:
Set PTR : = LINK[PTR].
4. Set LOC: = NULL.
5. Exit.
1.
Searching Algorithm for Sorted List
1.
2.
3.
4.
5.
Set PTR : = START.
Repeat Step 3 while PTR ≠ NULL:
If ITEM < INFO[PTR], then:
Set PTR: = LINK[PTR].
Else if ITEM : = INFO[PTR], then
Set LOC : = PTR, and Exit.
Else:
Set LOC: = NULL, and Exit.
Set LOC: = NULL.
Exit.
Overflow and Underflow

When new data is to be inserted and there is no available
space i.e. the free storage space is empty, this situation is
called Overflow.
–

It will occur in Linked List when there is insertion and AVAIL = NULL.
When we want to delete some data from a data structure that
is empty, this situation is called Underflow.
–
It will occur when there is deletion and START = NULL.
Insertion Operation

Item can be inserted at:
–
–
–
Beginning of list
End of list
Between any two nodes
Insertion Operation
NewNode.next −> RightNode;
Insertion Operation
LeftNode.next −> NewNode;
Deletion Operation
LeftNode.next −> TargetNode.next;
Deletion Operation
TargetNode.next −> NULL;
Garbage Collection


Garbage Collection (GC) is a form of
automatic
memory
management.
The garbage Collector attempts to
reclaim garbage, or memory occupied by
objects that are no longer in use by the
program.
The Garbage Collection takes place when
there is only some minimum amount of
space or no space at all left in the freestorage list, or when the CPU is idle and has
time to do collection
Garbage Collection in Linked List


It is mechanism by which
memory space of deleted
objects becomes available
for further use.
A special list is maintained
which consists of unused
memory cells has its own
pointer, is called list of
available space.
Dangling Reference
A dangling reference is
a reference to an object that
no longer exists. Garbage is
an object that cannot be
reached through a reference.
Circular Linked List

Circular
–
–

Last node point to first node
Draw like Circle
When using Circular single linked list
–
–
Every node in list had the same position
Needn’t Head, Tail
Doubly / Two Way Linked List

It is linear collection of data elements, called nodes, where
each node N is divided into three parts:
–
–
–
An information field INFO which contains data.
A pointer field FORW which contains the location of next node.
A pointer field BACK which contains the location of preceding node.
Doubly / Two Way Linked List

This type of list also requires two list pointer variables:
–
–
FIRST which points to first node in the list.
LAST which points to last node in the list.
Implementation in C++
#include<iostream>
#include<cstdio>
#include<cstdlib>
using namespace std;
struct node {
int x;
node *next;
};
int main()
{
node *root;
// This won't change, or we would lose the list in
memory
node *conductor; // This will point to each node as it traverses the list
root = new node; // Sets it to actually point to something
root->next = 0; // Otherwise it would not work well
root->x = 12;
conductor = root; // The conductor points to the first node
if ( conductor != 0 ) {
while ( conductor->next != 0)
conductor = conductor->next;
}
conductor->next = new node; // Creates a node at the end of the list
conductor = conductor->next; // Points to that node
conductor->next = 0;
// Prevents it from going any further
conductor->x = 42;
conductor = root;
if ( conductor != 0 ) { //Makes sure there is a place to start
while ( conductor->next != 0 ) {
cout<< conductor->x<<endl;
conductor = conductor->next;
}
cout<< conductor->x<<endl;
}
}
Implementation in C++
#include<iostream>
using namespace std;
typedef struct item {
int val;
item * next;
} item;
int main() {
item * head = NULL;
int i;
for(i=1;i<=10;i++) {
item * curr = new item;
curr->val = i;
curr->next = head;
head = curr;
}
while(head) {
cout << head->val << " ";
head = head->next ;
}
return 0;
}
Arrays vs Linked Lists
Arrays




Physically Contiguous
Fixed Length
Access Elements by Index
Insertion/Removal is Costly
Linked Lists




Logically Contiguous Only
Changeable Length
Access Elements by
Traversal
Insertion/Removal is Efficient