Prof. Sonali
Mhatre
DATA STRUCTURES AND
ANALYSIS
 Array
 List
 Tuple
 Vector
 Data Frame
 Stack
 Queue
 Linked List
DATA STRUCTURES
STACK AS A DATA STRUCTURE
STACK AS A DATA STRUCTURE
STACK AS A DATA STRUCTURE
STACK AS A DATA STRUCTURE
30
20
10
STACK AS A DATA STRUCTURE
Top
2
1
0
STACK AS A DATA STRUCTURE
 LIFO (Last In First Out)
 Push
 Pop
STACK AS A DATA STRUCTURE
QUEUE AS DATA STRUCTURE
 FIFO (First In First Out)
 Enqueue
 Deque
QUEUE AS DATA STRUCTURE
CIRCULAR QUEUE
F=-1
R=-1
10
F=0
R=0
10 20
F=0
R=1
CIRCULAR QUEUE
10 20 30
F=0
R=2
10 20 30 40 50
F=0
R=4
10 20 30 40
F=0
R=3
Queue is Full
CIRCULAR QUEUE
20 30 40 50
F=1
R=4
60 30 40 50
F=2
R=0
60 20 30 40 50
F=1
R=0
Queue is Full
CIRCULAR QUEUE
60 70 30 40 50
F=2
R=1
60 70 80 40 50
F=3
R=2
60 70 40 50
F=3
R=1
Queue is Full
Queue is Full
CIRCULAR QUEUE
60 70 80 50
F=4
R=2
60 70 80 90 50
F=4
R=3 Queue is Full
60 70 80 90
F=0
R=3
60 70 80 90 100
F=0
R=4
Queue is Full
int isFull(struct Queue * q)
{
if(((abs(q->front-q->rear))==SIZE-1) || q-
>rear==q->front-1)
return 1;
else
return 0;
}
QUEUE ISFULL
CIRCULAR QUEUE
F=-1
R=-1
Queue is empty
10
F=0
R=0
F=1
R=0
Queue is empty?
Queue is full?
20
R=1
F=1
CIRCULAR QUEUE
R=-1
F=-1
10
R=0
F=0
10 20
R=1
F=0
20
R=1
F=1
CIRCULAR QUEUE
Queue is Empty??
Queue is Full??
R=1
F=0
30
R=2
F=2
40
R=3
F=3
50
R=4
F=4
If(Front==Rear) then Front=Rear=-1;
CIRCULAR QUEUE
PRIORITY QUEUE
 Array Implementation
 Insert and Sort
 Remove and Rearrange
 Linked List Implementation
PRIORITY QUEUE IMPLEMENTATIONS
DOUBLE ENDED QUEUE
DOUBLE ENDED QUEUE
R=-1
F=-1
10 20
R = 5
F = 4
30 10 20
R = 5
F = 3 Insert Element at Front End
40 30 10 20
F = 2
R = 5
50 40 30 10 20
F = 1
R = 5
DOUBLE ENDED QUEUE
Insert Element at Front End
60 50 40 30 10 20
F = 0
R = 5
60 50 40 30 10 20 70
F = 9
R = 5
60 50 40 30 10 20 11 22 80 70
F = 6
R = 5 Queue is Full
DOUBLE ENDED QUEUE
DOUBLE ENDED QUEUE
R=-1
F=-1
10 20
R = 5
F = 4
DOUBLE ENDED QUEUE
10 20 30
R = 6
F = 4 Insert Element at Rear End
10 20 30 40
R = 7
F = 4
10 20 30 40 50 60
R = 9
F = 4
70 10 20 30 40 50 60
R = 0
F = 4 Insert Element at Rear End
DOUBLE ENDED QUEUE
70 80 10 20 30 40 50 60
R = 1
F = 4
70 80 90 11 10 20 30 40 50 60
R = 3
F = 4
Queue is Full
 Insertions and Deletions from both ends
 I/P restricted
 Input from only one end and Output from either end
 O/P restricted
 Input from either end and Output from only one end
DOUBLE ENDED QUEUE
SELF REFERENTIAL STRUCTURES
struct Complex
{
int real;
int imaginary;
}
In main()
Complex c[3];
3
4
-5
7
2
5
……
65004
65005
65006
65007
65008
65009
65010
65011
65012
65013
65014
65015
……
c[0]
c[1]
c[2]
c[0]= 3+4i
c[1]= -5+7i
c[2]= 2+5i
struct Complex
{
int real;
int imaginary;
struct Complex * next
}
In main()
Complex *c;
c->next;
c->next->next;
SELF REFERENTIAL STRUCTURES
c 3+4i
next
-5+7i
next
2+5i
next
NULL
3
4
72004
……
65004
65005
65006
65007
65008
65009
65010
65011
65012
65013
65014
65015
……
c
SELF REFERENTIAL STRUCTURES
-5
7
62012
……
72004
72005
72006
72007
72008
72009
72010
72011
72012
72013
72014
72015
……
next
2
5
NULL
……
62012
62013
62014
62015
62016
62017
62018
62019
62020
62022
62023
62024
……
next
 Self Referential Structure
LINKED LIST
10 next
20 next
30 next
40 NULL
10 next
20 next
30 next
40 NULL
LINKED LIST
tmp
tmp
tmp
tmp
tmp = tmp ->next
10 next
20 next
30 next
40 NULL
LINKED LIST
Start/First
tmp
tmp
tmp
tmp
10 next
20 next
30 next
40 NULL
LINKED LIST
start s
10 next
20 next
30 next
40 NULL
LINKED LIST (ADD AT BEGINNING)
start
s
50 NULL
nn
next
start
10 next
20 next
30 next
40 NULL
LINKED LIST (ADD AT END)
start
t
50 NULL
nn
s
t
t
t
next
struct Node n; //Static
struct Node * n = (struct Node *)
malloc (sizeof (struct Node));
//Dynamic
MALLOC (DYNAMIC MEMORY
ALLOCATION)
……
65004
65005
65006
65007
65008
65009
65010
65011
65012
65013
65014
65015
……
n
MALLOC (DYNAMIC MEMORY
ALLOCATION)
……
65004
65005
65006
65007
65008
65009
65010
65011
65012
65013
65014
65015
……
n 65004
10 next
20 next
30 next
40 next
LINKED LIST(ADD AT A POSITION)
start t
s
100 NULL
nn
t
t
50 NULL
Position=4
next
Count=1
Count=2
Count=3
10 next
20 next
30 next
40 next
LINKED LIST(ADD AT A POSITION)
start t
s
100 NULL
nn
t
t
50 NULL
Position=4
next
Count=1
Count=2
Count=3
?
t=s;
while(c<pos-1)
{
t=t->next;
c++;
}
nn->next=t->next;
t->next=nn;
LINKED LIST(ADD AT A POSITION)
10 next
20 next
30 next
40 next
LINKED LIST(ADD BEFORE)
start t
s
100 NULL
nn
t
t
50 NULL
Before = 40
t
pt
null
10 next
20 next
30 next
40 next
LINKED LIST(ADD BEFORE)
start t
s
100 NULL
nn
t
t
50 NULL
Before = 40
next
t
pt
pt
pt
10 next
20 next
30 next
40 next
LINKED LIST (ADD BEFORE)
start t
s
100 NULL
nn
t
t
50 NULL
Before = 40
next
t
pt
pt
pt
t=s;
pt=NULL;
while(t!=NULL)
{
if(t->a==b)
break;
pt=t;
t=t->next;
}
if(pt==NULL)
{
nn->next=t;
s=nn;
}
else
{
pt->next=nn;
nn->next=t;
}
LINKED LIST (ADD BEFORE)
10 next
20 next
30 next
40 next
LINKED LIST (ADD AFTER)
start t
s
100 NULL
nn
t
t
50 NULL
After = 30
next
t=s;
while(t!=NULL)
{
if(t->a==b)
break;
t=t->next;
}
nn->next=t->next;
t->next=nn;
LINKED LIST(ADD AFTER)
10 next
20 next
30 next
40 NULL
LINKED LIST (DELETE BEGINNING)
start
s
s
start
s=s->next;
10 next
20 next
30 next
40 next
LINKED LIST (DELETE END)
start t
s
t
t
50 NULL
t
pt
pt
pt
pt
t
pt->next=NULL;
NULL
t=s;
pt=NULL;
while(t->next!=NULL)
{
pt=t;
t=t->next;
}
if(pt==NULL)
s=NULL;
else
pt->next=NULL;
LINKED LIST (DELETE END)
10 next
20 next
30 next
40 next
LINKED LIST (DELETE IN BETWEEN NODE)
start t
s
t
t
50 NULL
pt
pt
pt->next=t->next;
Delete = 30
10 next
20 next
30 NULL
LINKED LIST (REVERSING A LIST)
start t
s
nt
nnt
t
nt nnt
NULL
s
NULL
while(nt!=NULL)
next
t
nt
s
start
while(nt!=NULL)
{
nt->next=t;
t=nt;
s=nt;
nt=nnt;
nnt=nnt->next;
}
LINKED LIST (REVERSING A LIST)
10 next
20 next
30 next
40 NULL
CIRCULAR LINKED LIST
start s
next
10 next
20 next
30 next
40 NULL
CIRCULAR LINKED LIST (ADD AT
BEGINNING)
start
s
next
100 NULL
nn
next
l
start
s
if(s==NULL)
{
s=nn;
s->next=s;
}
else
{
struct Node * l=getLast(s);
nn->next=s;
l->next=nn;
s=nn;
CIRCULAR LINKED LIST (ADD AT
BEGINNING)
10 next
20 next
30 next
40 NULL
CIRCULAR LINKED LIST (ADD AT END)
start
s
next
100 NULL
nn
next
l
if(s==NULL)
{
s=nn;
s->next=s;
}
else
{
struct Node * l=getLast(s);
nn->next=s;
l->next=nn;
}
CIRCULAR LINKED LIST (ADD AT END)
10 next
20 next
30 next
40
CIRCULAR LINKED LIST (DELETE
BEGINNING)
start
s
next
l
start
s
struct Node * l=getLast(s);
if(s==l)
s=NULL;
else
{
l->next=s->next;
s=s->next;
}
CIRCULAR LINKED LIST (DELETE
BEGINNING)
10 next
20 next
30 next
40
CIRCULAR LINKED LIST (DELETE END)
start
s
next
l
t
t
t
struct Node * l=getLast(s);
if(s==l)
s=NULL;
else
{
struct Node * t=s;
while(t->next!=l)
t=t->next;
t->next=s;
}
CIRCULAR LINKED LIST (DELETE END)
 Double pointer
 Pointer to previous Node
 Pointer to Next Node
struct Node
{
int a;
struct Node * prev;
struct Node * next;
}
DOUBLY LINKED LIST
a
prev next
DOUBLY LINKED LIST
NULL 100 next
prev 200 next
prev 300 next
prev 400 next
prev 500 NULL
start
DOUBLY LINKED LIST (ADD AT
BEGINNING)
NULL 100 next
prev 200 next
prev 300 next
prev 400 next
prev 500 NULL
start
NULL 1000 NULL
nn
next
prev
s
start
s
if(s==NULL)
s=nn;
else
{
nn->next=s;
s->prev=nn;
s=nn;
}
DOUBLY LINKED LIST (ADD AT
BEGINNING)
DOUBLY LINKED LIST (ADD AT END)
NULL 100 next
prev 200 next
prev 300 next
prev 400 next
prev 500 NULL
start
NULL 1000 NULL
nn
s
t
t
t
t
t
next
prev
if(s==NULL)
s=nn;
else
{
struct Node * t;
t=s;
while(t->next!=NULL)
t=t->next;
t->next=nn;
nn->prev=t;
}
DOUBLY LINKED LIST (ADD AT END)
NULL 100 next
prev 200 next
prev 300 next
prev 400 NULL
start
NULL 1000 NULL
s
t
t
Position = 3
Count = 1
Count = 2
next
prev
DOUBLY LINKED LIST (ADD AT A
POSITION)
t=s;
while(c<pos-1)
{
t=t->next;
c++;
}
t->next->prev=nn;
nn->next=t->next;
nn->prev=t;
t->next=nn;
DOUBLY LINKED LIST (ADD AT A
POSITION)
NULL 100 next
prev 200 next
prev 300 next
prev 400 NULL
start
NULL 1000 NULL
s
t
t
Before = 300
next
prev
DOUBLY LINKED LIST (ADD BEFORE)
t
pt=NULL
pt
pt
if(pt==NULL)
{
nn->next=t;
t->prev=nn;
s=nn;
}
DOUBLY LINKED LIST (ADD BEFORE)
else
{
pt->next=nn;
t->prev=nn;
nn->next=t;
nn->prev=pt;
}
Without pt ????????
DOUBLY LINKED LIST (ADD BEFORE)
NULL 100 next
prev 200 next
prev 300 next
prev 400 NULL
start
NULL 1000 NULL
s
t
t
Before = 300
next
prev
DOUBLY LINKED LIST (ADD BEFORE)
t
t->prev->next=nn;
nn->prev=t->prev;
t->prev=nn;
nn->next=t;
DOUBLY LINKED LIST (ADD BEFORE)
NULL 100 next
prev 200 next
prev 300 next
prev 400 NULL
start
NULL 1000 NULL
s
t
t
After = 200
next
prev
DOUBLY LINKED LIST (ADD AFTER)
nn->next=t->next;
t->next->prev=nn;
t->next=nn;
nn->prev=t;
DOUBLY LINKED LIST (ADD AFTER)
NULL 100 next
prev 200 next
prev 300 next
prev 400 next
prev 500 NULL
start
DOUBLY LINKED LIST (DELETE
BEGINNING)
s
NULL
s
start
s->next->prev=NULL;
s=s->next;
DOUBLY LINKED LIST (DELETE
BEGINNING)
NULL 100 next
prev 200 next
prev 300 next
prev 400 NULL
start
s
DOUBLY LINKED LIST (DELETE END)
t
pt=NULL
t
pt
t
pt
t
pt
NULL
t=s;
pt=NULL;
while(t->next!=NULL)
{
pt=t;
t=t->next;
}
if(pt==NULL)
s=NULL;
else
pt->next=NULL;
DOUBLY LINKED LIST (DELETE END)
Without pt ????????
DOUBLY LINKED LIST (DELETE END)
NULL 100 next
prev 200 next
prev 300 next
prev 400 NULL
start
s
DOUBLY LINKED LIST (DELETE AT A
POSITION)
t
pt=NULL
t
pt
t
pt
Position = 3
Count = 1
Count = 2
t=s;
pt=NULL;
while(c<pos)
{
pt=t;
t=t->next;
c++;
}
if(pt==NULL)
s=NULL;
else
{
pt->next=t->next;
t->next->prev=pt;
}
DOUBLY LINKED LIST (DELETE AT A
POSITION)
NULL 100 next
prev 200 next
prev 300 next
prev 400 NULL
start
s
DOUBLY LINKED LIST (DELETE AFTER)
t
t
After = 200
t->next=t->next->next;
t->next->prev=t;
DOUBLY LINKED LIST (DELETE AFTER)
NULL 100 next
prev 200 next
prev 300 next
prev 400 NULL
start
s
DOUBLY LINKED LIST (DELETE BEFORE)
t
pt=NULL
t
pt
t
pt
Before = 400
ppt=NULL
ppt
pt
ppt
t=s;
pt=NULL;
ppt=NULL;
while(t!=NULL)
{
if(t->a==b)
break;
ppt=pt;
pt=t;
t=t->next;
}
ppt->next=pt->next;
t->prev=pt->prev;
DOUBLY LINKED LIST (DELETE BEFORE)
Array Linked List
Data elements are stored in contiguous
locations in memory.
New elements can be stored anywhere and a
reference is created for the new element
using pointers.
Array elements can be accessed randomly
using the array index.
Random accessing is not possible in linked
lists. The elements will have to be accessed
sequentially.
Insertion and Deletion operations are costlier
since the memory locations are consecutive
and fixed.
Insertion and Deletion operations are fast
and easy in a linked list.
Memory is allocated during the compile time
(Static memory allocation).
Memory is allocated during the run-time
(Dynamic memory allocation).

Stack, Queue, Linked List.pptx