Linked List: April 2, 2007 Programming and Data Structure 1
Linked List: April 2, 2007 Programming and Data Structure 1
A B C
A B C
Item to be
X inserted
A B C
A B C
• For insertion:
– A record is created holding the new item.
– The next pointer of the new record is set to link
it to the item which is to follow it in the list.
– The next pointer of the item which is to precede
it must be modified to point to the new item.
• For deletion:
– The next pointer of the item immediately
preceding the one to be deleted is altered, and
made to point to the item following the deleted
item.
April 2, 2007 Programming and Data Structure 6
Array versus Linked Lists
A B C
head
A B C
A B C
• Creating a list
• Traversing the list
• Inserting an item in the list
• Deleting an item from the list
• Concatenating two lists into one
Insert
List
implementation
Delete
and the
related functions
Traverse
head
roll
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;
node *head;
………
insert (&head);
p = *head;
if (p->roll == rno)
/* Delete the first element */
{
*head = p->next;
free (p);
}
sub
mul Complex
Number
div
read
print
April 2, 2007 Programming and Data Structure 38
Example 2 :: Set manipulation
struct node {
int element;
Structure
struct node *next;
}
definition
typedef struct node set;
intersect
minus
Set
insert
delete
size
April 2, 2007 Programming and Data Structure 40
Example 3 :: Last-In-First-Out STACK
pop
create
STACK
isempty
isfull
In Out
C B A B C
dequeue
create
QUEUE
isempty
size
Out
In
B A
C B A
ARRAY
April 2, 2007 Programming and Data Structure 52
void push (stack **top, int element)
{
stack *new;
new = (stack *) malloc(sizeof(stack));
if (new == NULL)
{
printf (“\n Stack is full”);
exit(-1);
}
new->value = element;
new->next = *top;
*top = new;
}
LINKED LIST
April 2, 2007 Programming and Data Structure 53
Popping an element from the stack
ARRAY
April 2, 2007 Programming and Data Structure 54
int pop (stack **top)
{
int t;
stack *p;
if (*top == NULL)
{
printf (“\n Stack is empty”);
exit(-1); LINKED LIST
}
else
{
t = (*top)->value;
p = *top;
*top = (*top)->next;
free (p);
return t;
}
}
April 2, 2007 Programming and Data Structure 55
Checking for stack empty
int isempty (stack s) int isempty (stack *top)
{ {
if (s.top == -1) if (top == NULL)
return 1; return (1);
else else
return (0); return (0);
} }
main() if (isempty(B))
{ printf (“\n B is
stack *A, *B; empty”);
create(&A); create(&B); }
push(&A,10);
push(&A,20);
• Basic idea:
– Create a linked list to which items would be
added to one end and deleted from the other
end.
– Two pointers will be maintained:
• One pointing to the beginning of the list (point from
where elements will be deleted).
Rear
• Another pointing to the end of the list (point where
new elements will be inserted).
Front
April 2, 2007 Programming and Data Structure 61
Declaration
struct fifo {
int value;
struct fifo *next;
};
typedef struct fifo queue;