Linked List
Linked List
A B C
Item to be
tmp X inserted
A B C
curr
X
tmp=(node *) malloc(sizeof(node));
tmp->next=curr->next;
curr->next=tmp;
}
2013 Programming and Data Structure 5
Illustration: Deletion
Item to be deleted
A B C
tmp
curr
A B C
A B C
A B C
A B C
Insert
List
implementation
Delete
and the
related functions
Traverse
struct stud {
int roll;
char name[25];
int age;
struct stud *next;
};
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;
}
}
}
2013 Programming and Data Structure 30
• 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
2013 Programming and Data Structure 41
Example 2 :: Set manipulation
struct node {
int element; Structure
struct node *next;
definition
}
typedef struct node set;
intersect
minus
Set
insert
delete
size
2013 Programming and Data Structure 43
Example 3 :: Last-In-First-Out STACK
Assume:: stack contains integer elements
pop
create
STACK
isempty
isfull
dequeue
create
QUEUE
isempty
size
PUSH
top
top
POP
top
top
PUSH OPERATION
top
POP OPERATION
top
ARRAY
LINKED LIST
ARRAY
main() if (isempty(B))
{ printf (“\n B is
stack *A, *B; empty”);
create(&A); create(&B); }
push(&A,10);
push(&A,20);
ENQUEUE
front rear
DEQUEUE
front rear
struct node{
char name[30];
struct node *next;
};
typedef struct {
_QNODE *queue_front, *queue_rear;
} _QUEUE;
init_queue(&q);
command[0]='\0';
printf("For entering a name use 'enter <name>'\n");
printf("For deleting use 'delete' \n");
printf("To end the session use 'bye' \n");
while(strcmp(command,"bye")){
scanf("%s",command);
ENQUEUE DEQUEUE
0 N
front
front rearrear
typedef struct {
_ELEMENT q_elem[MAX_SIZE];
int rear;
int front;
int full,empty;
} _QUEUE;
q->rear=(q->rear+1)%(MAX_SIZE);
q->q_elem[q->rear]=ob;
return;
}
q->front=(q->front+1)%(MAX_SIZE);
temp=q->q_elem[q->front];
return(temp);
}
2013 Programming and Data Structure 80
Queue Example: Contd.
main() #include <stdio.h>
{ #include <stdlib.h>
int i,j; #include <string.h>
char command[5];
_ELEMENT ob;
_QUEUE A;
init_queue(&A);
command[0]='\0';
printf("For adding a name use 'add [name]'\n");
printf("For deleting use 'delete' \n");
printf("To end the session use 'bye' \n");
2013 Programming and Data Structure 81
Queue Example: Contd.
while (strcmp(command,"bye")!=0){
scanf("%s",command);
if(strcmp(command,"add")==0) {
scanf("%s",ob.name);
if (IsFull(&A))
printf("No more insertion please \n");
else {
AddQ(&A,ob);
printf("Name inserted %s \n",ob.name);
}
}