ppt dsa
ppt dsa
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.
July 21, 2009 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);
}
Out
In
A
B
C B A
In Out
C B A B C
Also called a
STACK
sub
mul Complex
Number
div
read
print
July 21, 2009 Programming and Data Structure 39
Example 2 :: Set manipulation
struct node {
int element;
Structure
struct node *next;
}
definition
typedef struct node set;
intersect
minus
Set
insert
delete
size
July 21, 2009 Programming and Data Structure 41
Example 3 :: Last-In-First-Out STACK
pop
create
STACK
isempty
isfull
dequeue
create
QUEUE
isempty
size
PUSH
top
top
POP
top
top
PUSH OPERATION
top
POP OPERATION
top
ARRAY
July 21, 2009 Programming and Data Structure 55
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
July 21, 2009 Programming and Data Structure 56
Popping an element from the stack
ARRAY
July 21, 2009 Programming and Data Structure 57
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;
}
}
July 21, 2009 Programming and Data Structure 58
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).
ENQUEUE
front rear
DEQUEUE
front rear
struct node{
char name[30];
struct node *next;
};
typedef struct {
_QNODE *queue_front, *queue_rear;
} _QUEUE;
July 21, 2009 Programming and Data Structure 67
_QNODE *enqueue (_QUEUE *q, char x[])
{
if(q->queue_rear==NULL)
_QNODE *temp;
{
temp= (_QNODE *)
q->queue_rear=temp;
malloc (sizeof(_QNODE));
q->queue_front=
if (temp==NULL){
q->queue_rear;
printf(“Bad allocation \n");
}
return NULL;
else
}
{
strcpy(temp->name,x);
q->queue_rear->next=temp;
temp->next=NULL;
q->queue_rear=temp;
}
return(q->queue_rear);
July 21, 2009
}
Programming and Data Structure 68
char *dequeue(_QUEUE *q,char x[])
{ else{
_QNODE *temp_pnt; strcpy(x,q->queue_front->name);
temp_pnt=q->queue_front;
if(q->queue_front==NULL){ q->queue_front=
q->queue_rear=NULL; q->queue_front->next;
printf("Queue is empty \n"); free(temp_pnt);
return(NULL); if(q->queue_front==NULL)
} q->queue_rear=NULL;
return(x);
}
}
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);
July 21, 2009 Programming and Data Structure 71
if(!strcmp(command,"enter")) {
scanf("%s",val);
if((enqueue(&q,val)==NULL))
printf("No more pushing please \n");
else printf("Name entered %s \n",val);
}
if(!strcmp(command,"delete")) {
if(!isEmpty(&q))
printf("%s \n",dequeue(&q,val));
else printf("Name deleted %s \n",val);
}
} /* while */
printf("End session \n");
July 21, 2009
}
Programming and Data Structure 72
Problem With Array Implementation
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);
}July 21, 2009 Programming and Data Structure 77
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");
July 21, 2009 Programming and Data Structure 78
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);
}
}
if (strcmp(command,"delete")==0) {
if (IsEmpty(&A))
printf("Queue is empty \n");
else {
ob=DeleteQ(&A);
printf("Name deleted %s \n",ob.name);
}
}
} /* End of while */
printf("End session \n");
}