Download Linked List - Narayana Info Solutions

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
5/24/2017 3:21 AM
1
Allen Newell
Cliff Shaw
Herbert Simon
We Owe To These
5/24/2017 3:21 AM
2
History
Linked lists were developed in 1955-56 by Allen Newell, Cliff Shaw and
Herbert Simon at RAND Corporation as the primary data structure for
their Information Processing Language. IPL was used by the authors to
develop several early artificial intelligence programs, including the Logic
Theory Machine, the General Problem Solver, and a computer chess
program. Reports on their work appeared in IRE Transactions on
Information Theory in 1956, and several conference proceedings from
1957-1959, including Proceedings of the Western Joint Computer
Conference in 1957 and 1958, and Information Processing
(Proceedings of the first UNESCO International Conference on
Information Processing) in 1959. The now-classic diagram consisting of
blocks representing list nodes with arrows pointing to successive list
nodes appears in "Programming the Logic Theory Machine" by Newell
and Shaw in Proc. WJCC, February 1957. Newell and Simon were
recognized with the ACM Turing Award in 1975 for having "made basic
contributions to artificial intelligence, the psychology of human cognition,
and list processing".
5/24/2017 3:21 AM
3
LINK LIST
•
•
•
•
•
•
•
Introduction .
Why we use Linked List ?
Overview of various Link Lists .
Implementation of Link List .
Operations .
Advantage and Disadvantages .
Applications .
5/24/2017 3:21 AM
4
Introduction
List : The term “List” refers to linear collection of data
items.Following figure shows a simple list of data items :
Milk
Eggs
Butter
Tomato
Apple
Orange
Linked List : Linked list is a linear data structure.
Linked list
are special list of some data elements linked to one another. The
logical addressing is represented by having each element
pointing to the next element. Each element is called a node
which has two parts.
(i) Info i.e. information part
(ii)5/24/2017
Next 3:21
i.e.AMthe Address part
5
This stores the data
Of the user .
Information
Next
This stores the address
of next node .
5/24/2017 3:21 AM
6
Some Key Points :
1.NULL Pointer: The link field of the last
node of the linked list contains
NULL rather than a valid address. It is a NULL pointer
and indicates the end of the list.
2.External Pointer : It is a pointer to the
very first node in the linked list, it
enables us to access the entire linked list.
5/24/2017 3:21 AM
7
3.Empty List : If the nodes are not present
in a linked list, then it is
called an empty linked list or simply empty list. It
is also called the Null list.
The value of the external pointer will be
zero for an empty link list. A linked list can be
made an empty linked list by assigning a NULL
value to the external pointer .That is for example
start=NULL;
5/24/2017 3:21 AM
8
Representation of a Node in a
Linked list :
Struct node
{
int a;
struct node* next;
}typedef struct node Node;
Node *start;
5/24/2017 3:21 AM
9
Why We Need Linked List
Suppose we have a memory in fragmented form
i.e. although there is space is availabe
In the memory but It is not contigious, and we
have to allocate maximum of the memory.
For example : Suppose if we have memory
of 256KB in fragmented form i.e. not conigious,
and we have to store 10 records in which each
of the record contain Name, Address, and
Course of the Student.
5/24/2017 3:21 AM
10
256
128 K B
64 K B
32 K B
5/24/2017 3:21 AM
32 K B
0
11
Then we can use the Linked List to
store these records with the help of
Dynamic Memory Allocation schem.
DMA can be implemented by :
Malloc()
Calloc()
Realloc()
Free()
5/24/2017 3:21 AM
12
Overview Of various Link Lists
 Singly Linked List
 Doubly Linked List
 Circular Linked List
 Circular Doubly Linked List
5/24/2017 3:21 AM
13
1. Singly Linked List : Singly linked list is
one in which all
nodes are linked together in some sequential
manner. Hence, it is also called Linear Linked
List. Clearly it has beginning and the end. The
problem with this list is that we can not access
the predecessor of nodes from the current
node. i.e. we can not traverse back in this list.
This problem can be overcome by using
Doubly Linked List.
Start
5/24/2017 3:21 AM
N
14
2. Doubly Linked List : Doubly Linked List
is one in which all
nodes are linked together by multiple links
which help in accessing both the successor
node (next node) and predecessor node
(previous node) for any arbitrary node in the
list, that is we can traverse both the direction
in the linked list.
Start
5/24/2017 3:21 AM
N
N
15
3. Circular Linked List : Circular linked list
is one which has no
beginning and no end. A singly linked list can
be made a circular linked list by simply attach
the address of very first node in the link field
of the last node.
Start
5/24/2017 3:21 AM
16
4. Circular Doubly Linked List : is one
which has both
the successor pointer and predecessor pointer
in circular manner. It is shown in the following
figure :
Start
5/24/2017 3:21 AM
N
17
Link List Operations
•
•
•
•
•
•
•
Creation
Insertion
Deletion
Traversing
Searching
Concatenation
Display
5/24/2017 3:21 AM
18
First we are performed the operations
on Singly Linked List
Creation of LinkList :
Algorithm :
Step 1. Create node
Step 2. if node is NULL then memory can not
be allocated to the node.
Step 3. else
if start is NULL
then start=node;
5/24/2017 3:21 AM
19
else
ptr=start;
while ptr->next is not equal to NULL
ptr=ptr->next;
ptr->next=node;
Step 4. Exit.
5/24/2017 3:21 AM
20
Insertion of node at the beginning
Algorithm :
Step1. Start
Step2. get node from the memory
Step3. Check overflow
if node is NULL then memory can
not be allocated to the node.
exit;
5/24/2017 3:21 AM
21
else
node->info=item;
node->next=start;
start =node;
Step5. Exit
5/24/2017 3:21 AM
22
Insertion at the end of the Linked List :
Algorithm :
Step1.Start
Step2.Get node from the memory.
Step3.Check Overflow
if node =NULL then overflow
exit;
5/24/2017 3:21 AM
23
Else if start =NULL then
start=node;
else
p=start;
while p->next is not equal to NULL
p=p->next
p->next=item;
Step 4. Exit
5/24/2017 3:21 AM
24
Deletion a node from Linked List
Algorithm :
Step1. Start
Step 2. Check Underflow
if start =NULL
then underflow
Step3.
else
5/24/2017 3:21 AM
25
if start->info=item
start=start->next
else
p=q=start;
while p->next not equal to
NULL
if p->next->info=item
delitem=p->next->info
p->next=p->next->next;
Step4. Exit.
5/24/2017 3:21 AM
26
Implementation
Linked List as a Stack
Linked List as a Queue
5/24/2017 3:21 AM
27
Doubly Link List
So far we have studied singly link list .One
of the most striking disadvantages of it is
that the inability to traverse the list in the
backward direction .In most of real world
applications it is necessary to traverse the
list in both the direction .The most
appropriate data structure for such an
application is a DOUBLY LINK LIST .
A doubly link list is one in which
all nodes are linked together by multiple
number of links which help in accessing
both the successor node and predecessor
node .It provides bi-directional traversing .
Each node in doubly link list has two link fields . These
are used to point to the successor and predecessor
nodes. It can be illustrated by the following figure :
prev
Data
next
The LEFT link points to the predecessor node
and RIGHT link points to the successor node .
5/24/2017 3:21 AM
29
Inserting node at beginning
•
•
•
•
Allocate memory for the new node .
Assign value to the data field of the node .
Assign LEFT and RIGHT links to NULL .
Make the RIGHT link of the new node to point
to the head node of list and make left link of
head node to point to new node .
• Finally reset the head pointer .That is make it
to point to new node which has inserter at the
beginning .
5/24/2017 3:21 AM
30
Inserting node at the end
• Allocate memory for the new node .
• Assign values to the data field of the new
node .
• If the list is not empty then traverse the list till
the last and make the RIGHT link of the last
node to point the new node and LEFT link of
the new node to point the last node.
5/24/2017 3:21 AM
31
Deleting node from beginning
• If the list is empty then display the message
“Empty list-No deletion” .
• Otherwise make the head pointer to point to
the second node and if the second node is not
null then make its LEFT link to point to NULL .
• Free the first node .
5/24/2017 3:21 AM
32
Deleting node from the end
• If the list is empty then display the message
“Empty list-No deletion” .
• Otherwise traverse the list till the last but one
node and make the Right link of the last but
one node to point to NULL .
• Free the last node .
5/24/2017 3:21 AM
33
Circular Link List
It is just a singly link list in which the link field of
the last node contains the address of first node of
the list .That is the link field of the last node does
not point to NULL rather it points back to the
beginning of the link list .
A circular link list has no end.Therefore
it is necessary to establish the FIRST and LAST
nodes in such a link list .It is useful if we set the
external pointer to point the last node .From this
conversion we can easily locate the FIRST node of
the list .
Inserting node at beginning
• Allocate memory for new node .
• Assign values to its data field .
• Make the link part of new node point to the
head node .
• Finally reset the head pointer. That is make it
to point to the inserted node .
5/24/2017 3:21 AM
35
Inserting node at the end
• Allocate memory for the new node .
• Assign values to the data field of the node .
• If the list is not empty then traverse the list till
the last and make the link of the last node to
point the new node and link of new node
should point to the head node .
5/24/2017 3:21 AM
36
Deleting node from beginning
• If the list is empty then display the message
Empty list-No deletion .
• Otherwise make the head pointer to point to
second node .
• Free the first node .
5/24/2017 3:21 AM
37
Deleting node from the end
• If the list is empty then display the message
Empty list-No deletion .
• Otherwise traverse the list till the last but one
node and make the link to point to head node
.
• Free the last node .
5/24/2017 3:21 AM
38
Operations . . .
•
•
•
•
Traversing
Searching
Concatenation
Display
5/24/2017 3:21 AM
39
Advantages
• Dynamic data structures .
• Efficient memory utilization .
• Easier and efficient insertion , deletion .
• Use in complex applications .
5/24/2017 3:21 AM
40
Disadvantages
• More memory required .
• Cumbersome data access .
5/24/2017 3:21 AM
41
Applications
• Polynomial operations .
• Dynamic programming .
• Base of other data structures .
5/24/2017 3:21 AM
42
Applications
O.S maintain a link list of free and used memory
To solve complex applications
5/24/2017 3:21 AM
43
data
data
data
data data
data
data
data
data
data
data
data
data
data
data
data
data
data
data
data
data
data
data
5/24/2017 3:21 AM
data
data
data
data
data
44