UNIT 4 Link List
UNIT 4 Link List
• {
• int data;
• };
• struct node * n;
A B C
Item to be
tmp X inserted
A B C
curr
X
A B C
tmp
curr
A B C
}
Spring 2012 Programming and Data Structure 21
Circular Link list
Circular Linked List is a variation of
Linked list in which the first element
points to the last element and the last
element points to the first element.
A B C
A B C
A B C
Insert
List
implementation
Delete and the
related functions
Traverse
name next
age
A B C
node *head;
………
head = create_list();
p = head;
while (p != NULL)
{
printf ("\nNode %d: %d %s %d", count,
p->roll, p->name, p->age);
count++;
p = p->next;
}
printf ("\n");
}
node *head;
………
display (head);
p = *head;
{
q = p;
p = p->next;
} The pointers
q and p
if (p == NULL) /* At the end */ always point
{ to consecutive
q->next = new; nodes.
new->next = NULL;
}
else if (p->roll == rno)
/* In the middle */
{
q->next = new;
new->next = p;
}
}
}
Spring 2012 Programming and Data Structure 49
• To be called from main() function as:
node *head;
………
insert (&head);
p = *head;
if (p->roll == rno)
/* Delete the first element */
{
*head = p->next;
free (p);
}
{
q = p;
p = p->next;
}
B A
C B A
C B A B C
Also called a
STACK
sub
mul Complex
Number
div
read
print
Spring 2012 Programming and Data Structure 60
Example 2 :: Set manipulation
struct node {
int element; Structure
struct node *next;
definition
}
typedef struct node set;
intersect
minus
Set
insert
delete
size
Spring 2012 Programming and Data Structure 62
Example 3 :: Last-In-First-Out STACK
Assume:: stack contains integer elements
pop
create
STACK
isempty
isfull
dequeue
create
QUEUE
isempty
size
PUSH
to
p
to
p
POP
to
p
to
p
PUSH OPERATION
top
POP OPERATION
top
ARRAY
LINKED LIST
ARRAY