Sie sind auf Seite 1von 34

Linked List

Need of Dynamic Data


Structures
Linked  Lists  are  dynamic  data  structures:  They  can 
grow or shrink during the execution of a program
 Efficient  memory  utilization:  Memory  is  allocated 
whenever it is required and deallocated when it is no
longer needed
 Insertion  and  Deletion  are  easier  &  efficient:  It 
provides  flexibility  in  inserting  a  data  item  at  a 
specified  position  and  deletion  of  a  data  item  from 
the given position
 Many complex applications can be easily carried out 
with linked list
Continuous Implementation of
Lists
 It  can  be  done  with  help  of  continuous  memory 
allocation data structure i.e. Arrays
 For the purpose of implementing lists using arrays 
we  are  required  to  create  a  structure  of  two 
elements.
1. Info
2. Next
 And then create the array (variable) of structure.
e.g.  struct node
{ int info,next;};
struct node n[100];
A linked list node

 Each node contains a data item and a pointer to the next
node

 data next next

 The last node has no next node
 An external pointer gives access to the first node

 first
Linked list notation
Accessing nodes in a linked list

 first

 No direct access to individual nodes
 First node accessed via external pointer
 Other nodes accessed via pointer in previous 
node 
 It is a sequential access data structure
Operation on Lists

1. Traversing
2. Searching
3. Inserting
4. Deleting
TRAVERSING A LINKED LIST

1. Set PTR = START
2. Repeat steps 3 and 4 while PTR!=NULL
3. Apply PROCESS to INFO[PTR]
4. Set PTR=LINK[PTR]
5. Exit
Types of Linked Lists
1. Singly linked list
 Begins with a pointer to the first node
 Terminates with a null pointer
 Only traversed in one direction
2. Circular, singly linked
 Pointer in the last node points back to the first node
3. Doubly linked list
 Two “start pointers” – first element and last element
 Each node has a forward pointer and a backward pointer
 Allows traversals both forwards and backwards
4. Circular, doubly linked list
 Forward pointer of the last node points to the first node and 
backward pointer of the first node points to the last node
Singly Linked List

 A  singly  linked  list  is  a 


concrete  data  structure  next
consisting  of  a  sequence  of 
nodes
 Each node stores
 element
 link to the next node elem node

A B C D
Adding an item
 Three steps:
 find node after which to add

 Allocate space for a node and store data in it

 Link the new node into the existing linked list

 Two  cases  for  linking  the  node  into  the 


existing linked list
 Adding an item at the beginning of the list

 Adding an item after an existing node
Inserting at the Beginning
Inserting at the Beginning (Algo.)

INSERT(INFO,LINK,START,AVAIL,ITEM)
1.    IF  AVAIL=NULL  then    Write  OVERFLOW  and 
EXIT.
2.  NEW=AVAIL and AVAIL=LINK[AVAIL]
3.  INFO[NEW]=ITEM
4.  LINK[NEW]=START
5.  Set START=NEW
6.  EXIT.
Inserting after a given Node
INSERTLOC(INFO,LINK,START,AVAIL,ITEM)
1. IF AVAIL=NULL then Write OVERFLOW and Exit
2. Set NEW=AVAIL and AVAIL=LINK[AVAIL]
3. Set INFO[NEW]=ITEM
4. If LOC=NULL then
      Set LINK[NEW]= START and START=NEW
   Else
      Set LINK[NEW]=LINK[LOC] and LINK[LOC]=NEW
5. Exit.
Inserting into Sorted List

FIND(INFO, LINK, START, ITEM, LOC)
1. If START =NULL then Set LOC=NULL and Return.
2. If ITEM<INFO[START] then Set LOC=NULL and Return
3. Set SAVE=START and PTR=LINK[START].
4. Repeat Step 5 AND 6 While PTR!= NULL.
5. If ITEM<INFO[PTR] then Set LOC=SAVE and Return.
6. Set SAVE=PTR and PTR = LINK[PTR]
7. Set LOC= SAVE.
8. Return.

INSERT(INFO,LINK,START,AVAIL,ITEM)
1. Call FIND(INFO, LINK, START. ITEM, LOC)
2. Call INSERTLOC(INFO,LINK,START,AVAIL,ITEM)
Deleting the Node Following a given 
Node

DEL(INFO,LINK,START,AVAIL,LOC,LOCP)
1. IF LOCP=NULL, THEN, Set START=LINK[START]
   ELSE Set LINK[LOCP]=LINK[LOC]
2. Set LINK[LOC]=AVAIL and AVAIL=LOC
3. Exit.

Here, LOCP is Predecessor of Location.
Deleting the Node with the given ITEM 
of information
FIND(INFO,LINK,START,ITEM,LOC,LOCP)

1. If START =NULL then Set LOC=NULL and
LOCP=NULL and Return
2. If INFO[START] == ITEM then
  LOC=START  and  LOCP  =  NULL  and  
Return
3. Set SAVE = START and 
PTR = LINK[START]
4. Repeat step 5 and 6 while PTR!=NULL
5.  If  INFO[PTR]=ITEM  then  Set  LOC=PTR 
and LOCP=SAVE and Return
6. Set SAVE=PTR and PTR=LINK[PTR].
7. Set LOC=NULL.
8. Return
DEL(INFO,LINK,START,AVAIL,LOC,LOCP)
1. Call  FIND(INFO,LINK,START,ITEM,LOC,LOCP)
2. If LOC=NULL then Write : ITEM not in list and Exit
3. If LOCP=NULL, THEN
Set START=LINK[START]
Else
Set LINK[LOCP]=LINK[LOC]
4. Set LINK[LOC]=AVAIL and AVAIL=LOC
5. Exit.
Header linked lists

   Header linked list is a linked list which always 
contains  a  special  node  called  the  Header 
Node, at the beginning of the list.

Header linked list

Grounded Header linked list Circular Header linked list


 Grounded Header  linked lists
 The last node contains null pointer.

A B C X

Head
 Circular linked lists
 The  last  node  points  to  the  first  node  of 
the list

A B C

Head

 How do we know when we have finished 
traversing the list? 
Comparison between Array &
Dynamic Implementation of
Linked List
ARRAY LINKED LIST
1.  Linear  collection  of  data  1. Linear collection of nodes
elements
2.  Consecutive  memory  2.  Not  Consecutive  memory 
allocation allocation
3.  Allow  Sequential  &  Random  3. Allow only Sequential Access
Access of data of data
4. Insertions / Deletions are not  4.  Insertions  /  Deletions  are 
efficient  efficient with a constant time
5.  Can't  add  any  number  of  5.  Can  add  any  number  of 
elements at run time elements in the list at run time
6.  No  extra  cost  of  saving  6. Extra cost of saving address 
address of next element of next element in each node
Circular Lists

 The  last  node  contains  pointer  to  the  first 


node of the list
 We  can  begin  at  any  node  and  traverse  the 
list  until  we  reach  the  same  node  where  we 
started

A B C

START
Inserting an element in the
beginning of a Circular List
1. If AVAIL==NULL then Write Overflow and Return
2. Set NEW=AVAIL and AVAIL=LINK[AVAIL]
3. INFO[NEW]=ITEM
4. Set PTR=START
5. Repeat Step 6 while LINK[PTR]!=START
6. PTR=LINK[PTR]
7. Set LINK[NEW]=START
8. Set LINK[PTR]=NEW
9. Set START=NEW
10. Exit
Inserting an element at the end of
a Circular List
1. If AVAIL==NULL then Write Overflow and Return
2. Set NEW=AVAIL and AVAIL=LINK[AVAIL]
3. Set INFO[NEW]=ITEM
4. Set LINK[NEW]=START
5. Set PTR=START
6. Repeat Step 7 while LINK[PTR]!=START
7. PTR=LINK[PTR]
8. Set LINK[PTR]=NEW
9. Exit
Deleting the First node from
Circular List
1. If START==NULL then Write Underflow and
Return
2. Set PTR=START
3. Repeat STEP 4 while LINK[PTR]!=START
4. Set PTR=LINK[PTR]
5. Set LINK[PTR]=LINK[START]
6. LINK[START]=AVAIL and AVAIL=START
7. START=LINK[PTR]
8. Exit
Deleting the Last node from
Circular List
1. If START==NULL then Write Underflow and
Return
2. Set PTR=START
3. Repeat STEP 4 while LINK[PTR]!=START
4. Set SAVE=PTR and PTR=LINK[PTR]
5. Set LINK[SAVE]=START
6. LINK[PTR]=AVAIL and AVAIL=PTR
7. Exit
Doubly Linked List
 A  Doubly  linked  list  is  a  two  way  linked  list, 
where each node N is divided into three parts:
1. An  information  field  INFO  which  contains  the 
data of N
2. A pointer FORW which contains the location of 
the next node in the list
3. A  pointer  field  BACK  which  conatins  the 
location of the preceding node in the list
4. The BACK pointer of first node and the FORW 
pointer of the last node will contain NULL
Inserting a new node at the
beginning
1. If AVAIL==NULL, then Write OVERFLOW 
and Return
2. Set NEW=AVAIL and AVAIL=LINK[AVAIL]
3. Set INFO[NEW]=ITEM
4. Set BACK[NEW]=NULL
5. BACK[START]=NEW
6. Set FORW[NEW]=START
7. Set START=NEW
8. Exit
Inserting a new node after
given
1. NUM
If AVAIL==NULL, then Write OVERFLOW and 
Return
2. Set NEW=AVAIL and AVAIL=LINK[AVAIL]
3. Set INFO[NEW]=ITEM
4. Set PTR=START
5. Repeat Step 6 while INFO[PTR]!=NUM
6. Set PTR=FORW[PTR]
7. Set FORW[NEW]=FORW[PTR]
8. Set BACK[NEW]=PTR
9. Set FORW[PTR]=NEW
10. Set PTR=FORW[NEW]
11. Set BACK[PTR]=NEW
12. Exit
Inserting a new node before given
NUM
1.
2.
If AVAIL==NULL, then Write OVERFLOW and Return
Set NEW=AVAIL and AVAIL=LINK[AVAIL]
3. Set INFO[NEW]=ITEM
4. Set PTR=START
5. Repeat Step 6 while INFO[PTR]!=NUM
6. Set PTR=FORW[PTR]
7. Set FORW[NEW]=PTR
8. If BACK[PTR]==NULL, THEN goto Step 9 ELSE goto Step 12
9. Set START=NEW
10. BACK[NEW]=NULL
11. BACK[PTR]=NEW and Return
12. Set BACK[NEW]=BACK[PTR]
13. Set BACK[PTR]=NEW
14. PTR=BACK[NEW]
15. FORW[PTR]=NEW
16. Exit
Deleting the First node from
Doubly Linked List
1. If  START==NULL,  Then  write 
UNDERFLOW and Exit
2. Set PTR=START
3. Set START=FORW[START]
4. Set BACK[START]=NULL
5. FORW[PTR]=AVAIL and AVAIL=PTR
6. Exit
Deleting a node after given
NUM
1. If  START==NULL,  Then  write  UNDERFLOW  and 
Exit
2. Set PTR=START
3. Repeat Step 4 while INFO[PTR]!=NUM
4. Set PTR=FORW[PTR]
5. Set TEMP=FORW[PTR]
6. FORW[PTR]=FORW[TEMP]
7. Set SAVE=FORW[TEMP]
8. Set BACK[SAVE]=PTR
9. FORW[TEMP]=AVAIL and AVAIL=TEMP
10. Exit
Deleting a node before given
NUM
1. If  START==NULL,  Then  write  UNDERFLOW  and 
Exit
2. Set PTR=START
3. Repeat Step 4 while INFO[PTR]!=NUM
4. Set TEMP=PTR and PTR=FORW[PTR]
5. Set SAVE=BACK[TEMP]
6. FORW[SAVE]=PTR
7. BACK[PTR]=BACK[TEMP]
8. FORW[TEMP]=AVAIL and AVAIL=TEMP
9. Exit

Das könnte Ihnen auch gefallen