You are on page 1of 46

ADT List using Dynamic arrays

A dynamic data structure is one that changes size, as


needed, as items are inserted or removed
Dynamic arrays are arrays that grow (or shrink) as required

Prof. Gayathri. P, SCSE, VIT University

Dynamic Array
top = 4

Prof. Gayathri. P, SCSE, VIT University

Dynamic Array
insert 5

top = 4

Prof. Gayathri. P, SCSE, VIT University

Dynamic Array
top = 5

Prof. Gayathri. P, SCSE, VIT University

Dynamic Array
top = 5

insert 2

Prof. Gayathri. P, SCSE, VIT University

Dynamic Array
top = 6

Prof. Gayathri. P, SCSE, VIT University

insert 3

Dynamic Array
top = 6

Prof. Gayathri. P, SCSE, VIT University

insert 3
!The array is full and
there is no room for a
new item!

Dynamic Array
top = 6

Prof. Gayathri. P, SCSE, VIT University

insert 3

So we will create a
new, bigger array

Dynamic Array
top = 6

insert 3

So we will create a
new, bigger array

Prof. Gayathri. P, SCSE, VIT University

10

11
9

Dynamic Array
top = 6

insert 3

copy the elements


of the old array into it

Prof. Gayathri. P, SCSE, VIT University

10

11
10

Dynamic Array
insert 3
6

copy the elements


of the old array into it

top = 6

Prof. Gayathri. P, SCSE, VIT University

10

11
11

Dynamic Array
insert 3
6

and finally insert 3


into the new array.

top = 7
6

Prof. Gayathri. P, SCSE, VIT University

10

11
12

Dynamic Array

The old array will


eventually be deleted
by garbage collector

top = 7
6

Prof. Gayathri. P, SCSE, VIT University

10

11
13

Dynamic Array Summary


Before every insertion, check to see if the array
needs to grow
Question: When growing array, how much to grow
it?

Prof. Gayathri. P, SCSE, VIT University

14

Dynamic Array Summary


Growing by doubling works well in practice,
because it grows very large very quickly
10, 20, 40, 80, 160, 320, 640, 1280,

While the copying operation is expensive it does


not have to be done often

Prof. Gayathri. P, SCSE, VIT University

15

Dynamic Array Problems


And, right after a doubling half
the array is empty
Deleting and inserting in the
middle is still a problem

Prof. Gayathri. P, SCSE, VIT University

16

Linked Lists

Prof. Gayathri. P, SCSE, VIT University

17

Linked Lists
A linked list is a linear collection of data
elements, called nodes, where the linear order
is given by means of pointers.
Each node is divided into two parts:
The first part contains the information of the
element and
The second part contains the address of the next
node (link /next pointer field) in the list.

Prof. Gayathri. P, SCSE, VIT University

18

Linked Lists
info next

info next

info next

list

null

Linear linked list

Prof. Gayathri. P, SCSE, VIT University

19

Basic Node Implementation


Self-referential structures:
Struct node
{
int info;
node *next;
}*list = 0;

Prof. Gayathri. P, SCSE, VIT University

20

Adding an Element to the front of a


Linked List

info next
list

info next
3

Prof. Gayathri. P, SCSE, VIT University

info next
8

null

21

Some Notations for use in algorithm


(Not in C programs)

List: is a pointer pointing to first node


p: is a pointer
node(p): the node pointed to by p
info(p): the information portion of the node
Next(p): the next address portion of the node
getnode(): obtains an empty node
freenode(p): makes node(p) available for reuse.
Prof. Gayathri. P, SCSE, VIT University

22

Adding an Element to the front of a


Linked List
info next
p

p = getnode()
info next

list

info next
3

Prof. Gayathri. P, SCSE, VIT University

info next
8

null

23

Adding an Element to the front of a


Linked List
info next
p

info(p) = 6

info next
list

info next
3

Prof. Gayathri. P, SCSE, VIT University

info next
8

null

24

Adding an Element to the front of a


Linked List
info next
p

next(p) = list
info next
list

info next
3

Prof. Gayathri. P, SCSE, VIT University

info next
8

null

25

Adding an Element to the front of a


Linked List
info next
p
list

list = p
info next
5

info next
3

Prof. Gayathri. P, SCSE, VIT University

info next
8

null

26

Adding an Element to the front of a


Linked List
info next
list

info next
5

info next
3

Prof. Gayathri. P, SCSE, VIT University

info next
8

null

27

C function
void binsert()
{
struct node *p;
int d;
printf ("\nEnter value to insert :");
scanf("%d",&d);
p=(struct node*)malloc(sizeof(struct node));
p->info=d;
p->next=list;
list=p;
}
Header file - alloc.h or stdlib.h
Structure members are accessed using -> operator.
Prof. Gayathri. P, SCSE, VIT University

28

Removing an Element from the front


of a Linked List
info next
list

info next
5

info next
3

Prof. Gayathri. P, SCSE, VIT University

info next
8

null

29

Removing an Element from the front


of a Linked List

p = list
info next
list
p

info next
5

info next
3

Prof. Gayathri. P, SCSE, VIT University

info next
8

null

30

Removing an Element from the front


of a Linked List
info next
p

list = next(p)

info next
list

info next
3

Prof. Gayathri. P, SCSE, VIT University

info next
8

null

31

Removing an Element from the front


of a Linked List
info next
p

x = info(p)

info next
x=6
list

info next
3

Prof. Gayathri. P, SCSE, VIT University

info next
8

null

32

Removing an Element from the front


of a Linked List
info next

freenode(p)

info next
x=6

list

info next
3

Prof. Gayathri. P, SCSE, VIT University

info next
8

null

33

Removing an Element from the front


of a Linked List

info next
x=6

list

info next
3

Prof. Gayathri. P, SCSE, VIT University

info next
8

null

34

Element x is inserted between the third and


fourth elements in an array
X0

X0

X0

X1

X1

X1

X2

X2

X2

X3

X4

X3

X3

X5

X4

X4

X6

X5

X5

X6

X6

Prof. Gayathri. P, SCSE, VIT University

35

Inserting an item x into a list at


particular position
p

list

X0

X1

X2

X3

X4

X5

X6 null

X3

X4

X5

X6 null

list

X0

X1

X2

Prof. Gayathri. P, SCSE, VIT University

36

Inserting an item x into a list at


particular position
p = list
ctr = 1
While(ctr<pos-1) && (next(p) !=0)
{ p = next(p)
ctr++ }
q=getnode();
info(q)=x;
next(q)=next(p);
next(p)=q;
Prof. Gayathri. P, SCSE, VIT University

37

Deleting an item x3 from a list


p

list

X0

X1

X2

X3

X4

X5

X6 null

x =X3
p

list

X0

X1

X2

X3

Prof. Gayathri. P, SCSE, VIT University

X4

X5

X6 null

38

Deleting an item x3 from a list or End


Deletion
q=list
While (info(q) != key)
{p = q
q = next(q)}
x=info(q);
next(p)=next(q);
freenode(q);
Prof. Gayathri. P, SCSE, VIT University

39

End insertion
q=getnode();
info(q)=x;
next(q)=0;
p = list
While (next(p) != 0)
p = next(p)
Next(p) = q
Prof. Gayathri. P, SCSE, VIT University

40

Variations of Linked Lists


Circular linked lists
The last node points to the first node of the list

Head

How do we know when we have finished traversing


the list? (Tip: check if the pointer of the current
node is equal to the head.)
Prof. Gayathri. P, SCSE, VIT University

41

Variations of Linked Lists


Doubly linked lists
Each node points to not only successor but the
predecessor
There are two NULL: at the first and last nodes in
the list
Advantage: given a node, it is easy to visit its
predecessor. Convenient to traverse lists backwards

Head

Prof. Gayathri. P, SCSE, VIT University

42

Linked Lists vs. Arrays


The major advantage of the linked list over the
array is that data are easily inserted and
deleted.
It is not necessary to shift elements of a linked
list to make room for a new elements or to
delete an element.

Prof. Gayathri. P, SCSE, VIT University

43

Contd
Array
Sequential organization of
elements
Static memory allocation
Elements are physically and
logically adjacent
Insertion and deletion takes
more time
Less memory
Must fix the size in advance

Linked List
Order of elements is given
by links
Dynamic memory allocation
Logically adjacent
Less time
More memory because next
node address stored
No need to fix the size

Prof. Gayathri. P, SCSE, VIT University

44

LL Application
Polynomial Manipulation
Refer PDF File

Prof. Gayathri. P, SCSE, VIT University

45

Stacks & Queues using SLL


Stacks

Queues

Struct StackNode
{ int data;
StackNode *next;
}*top = 0;
Push:
pop:
P = getnode()
temp = top;
Info(p) = item
top = next(top)
Next(p) = top
freenode(temp)
Top = p

Struct QueueNode
{ int data;
QueueNode *next;
}*rear = 0, *front = 0;
Enqueue:
Dequeue:
P=getnode()
temp = front
Info(p) = item
front=next(front)
Next(p) = 0
freenode (temp)
If (front ==0)
Front = rear = p
Else next(rear) = p
rear = p

Prof. Gayathri. P, SCSE, VIT University

46

You might also like