Linear Linked Lists
Linear Linked Lists
Example:
struct node {
int data; // data
struct node *next; // pointer to next element
};
How to create nodes
struct node *np = (struct node*) malloc(sizeof(struct node));
A B C
head
A B C
head tail
A B C
Step 1:
[INITIALIZE] SET PTR = START
Step 2:
Repeat Steps 3 and 4 while PTR != NULL
Step 3:
Apply Process to PTR->DATA
Step 4:
SET PTR = PTR->NEXT
[END OF LOOP]
Step 5: EXIT
1 7 3 4 2 6 5 X
PTR
1 7 3 4 2 6 5 X
PTR
1 7 3 4 2 6 5 X
PTR
1 7 3 4 2 6 5 X
PTR
Searching a Linked List
ALGORITHM TO SEARCH A LINKED LIST
Step 1: [INITIALIZE] SET PTR = START
Step 2: Repeat Step 3 while PTR != NULL
Step 3: IF VAL = PTR->DATA
SET POS = PTR
Go To Step 5
ELSE
SET PTR = PTR->NEXT
[END OF IF]
[END OF LOOP]
Step 4: SET POS = NULL // not found
Step 5: EXIT // found, output POS
1 7 3 4 2 6 5 X
LOC
Deleting the Last Node
START
1 7 3 4 2 6 5 X
LOC
Deleting the Last Node
START
1 7 3 4 2 6 5 X
LOC
Deleting the Last Node
START
1 7 3 4 2 6 5 X
LOC
Deleting the Last Node
START
1 7 3 4 2 6 5 X
LOC
Deleting the Last Node
START
1 7 3 4 2 6 5 X
LOC
Deleting the Node After a Given Node
START
1 7 3 4 2 6 5 X
START
1 7 3 4 2 6 5 X