Documente Academic
Documente Profesional
Documente Cultură
Session outline
append(&p,11);
append(&p,2);
append(&p,14);
append(&p,17);
append(&p,99);
clrscr();
display(p);
printf(\n No of elements in theDLL=%d\n,count(p));
addbeg(&p,33);
addbeg(&p,55);
display(p);
display(p);
printf(\n no of elements in the DLL =
%d\n,count(p));
addafter(p,4,66);
addafter(p,2,96);
display(p);
printf(\n no of elements in the DLL =%d,count(p));
Copyright Amity University
delete(&p,55);
delete(&p,2);
delete(&p,99);
display(p);
printf(\n no of elements in the DLL=
%D\n,count(p));
}
else
{ temp=*s;
while(temp->next != NULL)
{temp=temp->next;}
r->data=num;
r->prev=temp;
r->next =NULL;
temp->next=r;
}}
void main()
{
struct node *s=NULL;
int i;
clrscr();
push(&s,14);
push(&s,-3);
push(&s,18);
push(&s,29);
push(&s,31);
push(&s,16);
Copyright Amity University
i=pop(&s);
printf(\nitem popped %d,i)
i=pop(&s);
printf(\nitem popped %d,i)
i=pop(&s);
printf(\nitem popped %d,i)
delstack(&s);
getch();
}
Continued
temp=*top;
Item=temp->data;
*top=(*top)->link;
free(temp);
return item;
}
/*deallocates memory */
void delstack(struct node **top)
{
struct node *temp;
If(*top==NULL)
return;
while(*top!=NULL)
{
temp=*top;
*top=(*top)->link;
free(temp);
}
Copyright Amity University
output
Item popped :16
Item popped :31
Item popped :29
..Continued
In the pop() function ,first we are checking
,whether or not a stack is empty. If the stack is
empty then a message stack is empty gets
displayed. If the stack is not empty then the
topmost item gets removed from the list.
initqueue(&a);
addq(&a,11);
addq(&a,-8);
addq(&a,23);
addq(&a,19);
addq(&a,15);
addq(&a,16);
addq(&a,28);
i=delq(&a);
printf(\n item extracted %d,i);
i=delq(&a);
printf(\n item extracted %d,i);
i=delq(&a);
printf(\n item extracted %d,i);
delqueue(&a);
getch();
}
q->rear->link=temp;
q->rear=q->rear->link;
}
/*removes an element from the queue */
int delq(struct queue *q)
{
struct node *temp;
int item;
If(q->front==NULL)
{ printf(\n queue is empty);
return NULL; }
Copyright Amity University
item=q->front->data;
temp=q->front;
q->front=q->front->link;
free(temp);
return item;
}
/* deallocates memory */
void delqueue(struct queue *q)
{
struct node *temp;
If(q->front == NULL)
return;
while (q->front!=NULL)
{
temp=q->front;
q->front=q->front->link;
free(temp); }
Copyright Amity University
START
ADD AT BEGINNING
ptr=head
temp=(struct node*)malloc(size of(struct node))
temp->data=num;
temp->link=head;
while(ptr->link!=head)
ptr=ptr->link;
ptr->link=temp;
head=temp;
ADD AT LAST
ptr=head;
while(ptr->link!=head)
ptr=ptr->link;
temp=(struct node*)malloc(size of(struct node))
ptr->link=temp;
ptr->link=head;
Header
node
head
Trees
Tree Data structure is mainly used to
represent data containing a hierarchical
relationship between elements .
Tree
Collection of nodes or
finite set of nodes
Tree
Tree levels
Two different approaches
Tree levels
Tree
Tree
Tree
Binary Trees
Binary Tree
Binary tree
Binary Tree
Tree Traversals
Pre-Order
NLR
In-Order
LNR
Post-Order
LRN
Tree Traversals
Pre-Order(NLR)
1, 3, 5, 9, 6, 8
Tree Traversals
In-Order(LNR)
5, 3, 9, 1, 8, 6
Tree Traversals
Post-Order(LRN)
5, 9, 3, 8, 6, 1
EXAMPLE:
40
20
10
70
30
25
35
28
90
50
55
53
60
Answers
Q.No
1
2
3
4
5
c
a
a
b
a
77