Escolar Documentos
Profissional Documentos
Cultura Documentos
Prepared by Borisaniyabhavesh
Linked List
9/9/2011
Node
NODE
INFO LINK
FIRST
60
Singly Linked List
Linked List
9/9/2011
Insertion at beginning
FIRST = INSERT (X, FIRST)
Function : INSERT (X, FIRST) X a new element FIRST a pointer to the first element of a linked linear list INFO data field of the node LINK a pointer to the next element in the list AVAIL a pointer to the top element of the availability stack NEW a temporary pointer variable
4 Linked List 9/9/2011
Insertion at beginning
FIRST
60
AVAIL
Linked List
9/9/2011
Insertion at beginning
FIRST
60
7
Function : INSERT (X, FIRST)
AVAIL
Linked List
9/9/2011
Insertion at beginning
FIRST
60
7
Function : INSERT (X, FIRST) 1. [Underflow ?] If AVAIL = NULL then Write(AVAIL UNDERFLOW) Return(FIRST)
AVAIL
Linked List
9/9/2011
Insertion at beginning
FIRST
60
7
Function : INSERT (X, FIRST) 1. [Underflow ?] If AVAIL = NULL then Write(AVAIL UNDERFLOW) Return(FIRST) 1. [Obtain address of next free node] NEW AVAIL
NEW = AVAIL
Linked List
9/9/2011
Insertion at beginning
FIRST
60
7
Function : INSERT (X, FIRST) 1. [Underflow ?] If AVAIL = NULL then Write(AVAIL UNDERFLOW) Return(FIRST) 1. [Obtain address of next free node] NEW AVAIL 1. [Remove free node from availability stack] AVAIL LINK(AVAIL)
NEW
AVAIL
Linked List
9/9/2011
Insertion at beginning
FIRST
60
7
Function : INSERT (X, FIRST) 1. [Underflow ?] If AVAIL = NULL then Write(AVAIL UNDERFLOW) Return(FIRST) 1. [Obtain address of next free node] NEW AVAIL 1. [Remove free node from availability stack] AVAIL LINK(AVAIL) 1. [Initialize fields of new node..] INFO(NEW) X
NEW
AVAIL
10
Linked List
9/9/2011
Insertion at beginning
FIRST
60
7
Function : INSERT (X, FIRST) 1. [Underflow ?] If AVAIL = NULL then Write(AVAIL UNDERFLOW) Return(FIRST) 1. [Obtain address of next free node] NEW AVAIL 1. [Remove free node from availability stack] AVAIL LINK(AVAIL) 1. [Initialize fields of new node..] INFO(NEW) X LINK(NEW) FIRST
9/9/2011
NEW
AVAIL
11
Linked List
Insertion at beginning
FIRST
60
7
Function : INSERT (X, FIRST) 1. [Underflow ?] If AVAIL = NULL then Write(AVAIL UNDERFLOW) Return(FIRST) 1. [Obtain address of next free node] NEW AVAIL 1. [Remove free node from availability stack] AVAIL LINK(AVAIL) 1. [Initialize fields of new node..] INFO(NEW) X LINK(NEW) FIRST 1. [Return address of new node] Return(NEW) 9/9/2011
NEW
AVAIL
12
Linked List
Insertion at beginning
FIRST
60
AVAIL
13
Linked List
Function : INSERT (X, FIRST) 1. [Underflow ?] If AVAIL = NULL then Write(AVAIL UNDERFLOW) Return(FIRST) 1. [Obtain address of next free node] NEW AVAIL 1. [Remove free node from availability stack] AVAIL LINK(AVAIL) 1. [Initialize fields of new node..] INFO(NEW) X LINK(NEW) FIRST 1. [Return address of new node] Return(NEW) 9/9/2011
Insertion at beginning
Function : INSERT (X, FIRST) 1. [Underflow ?]
If AVAIL = NULL then Write(AVAILABILITY STACK UNDERFLOW) Return(FIRST)
2.
3. 4.
5.
14
Linked List
Program snippet
struct node_type{
int data; struct node_type *link;
};
struct node_type *insert (int x, struct node_type *f) { struct node_type *node; node=(struct node_type *)malloc(sizeof(node)); if(node!=NULL) { node->data=x;
node->link=f;
} else printf("memory underflow\n"); return(node); }
15
Linked List
9/9/2011
Insertion at end
FIRST = INSEND(X, FIRST)
Function : INSEND (X, FIRST) X a new element FIRST a pointer to the first element of a linked linear list INFO data field of the node LINK a pointer to the next element in the list AVAIL a pointer to the top element of the availability stack NEW a temporary pointer variable SAVE a temporary pointer variable
16 Linked List 9/9/2011
Insertion at end
FIRST
60
7
Function : INSEND (X, FIRST) 1. [Underflow ?]
NEW
5
2. 3. 4.
17
Linked List
Insertion at end
FIRST
60
5.
NEW
18
9/9/2011
Insertion at end
FIRST
60
5. 6.
NEW
19
9/9/2011
Insertion at end
FIRST
60
SAVE
5. 6.
NEW
20
9/9/2011
Insertion at end
FIRST
60
SAVE
5. 6. 5.
NEW
21
9/9/2011
Insertion at end
FIRST
60
7
SAVE
5. 6. 5.
NEW
22
9/9/2011
Insertion at end
FIRST
60
7
SAVE
5. 6. 5.
NEW
5.
23
9/9/2011
Insertion at end
FIRST
60
7
SAVE
5. 6. 5.
NEW
5.
24
9/9/2011
Insertion at end
FIRST
60
7
SAVE
5. 6. 5.
NEW
5.
5.
25
Insertion at end
FIRST
60
5. 6. 5.
5.
5.
26
Insertion at end
Function : INSEND (X, FIRST) (continue) 1. [Underflow ?]
If AVAIL = NULL then Write(AVAILABILITY STACK UNDERFLOW) Return(FIRST)
2.
3. 4.
5.
27
Linked List
Insertion at end
6.
7.
8.
9.
28
Linked List
9/9/2011
Algorithm 1. Remove a node from the availability stack 2. Set the fields of the new node 3. If the linked list is empty then return the address of the new node 4. If the node precedes all others in the list then insert the node at front of the list and return its address 5. Obtain the next node in the linked list 6. Insert the new node in the list and return address of its first node
29 Linked List 9/9/2011
[Obtain address of next free node] NEW AVAIL [Remove free node from availability stack] AVAIL LINK(AVAIL) [Copy information contents into new node] INFO(NEW) X [Is the list empty ?] If FIRST = NULL then LINK(NEW) NULL Return(NEW)
9/9/2011
31
Linked List
7. 8.
9.
32
Linked List
9/9/2011
[Find X]
Repeat thru step5 while TEMP != X and LINK(TEMP) != NULL
TEMP LINK(TEMP)
9/9/2011
35
Linked List
7.
[Delete X]
If X = FIRST (Is X the first node ?) then FIRST LINK(FIRST) else LINK(PRED) LINK(X)
8.
36
Linked List
9/9/2011