Professional Documents
Culture Documents
Dynamic Array
top = 4
Dynamic Array
insert 5
top = 4
Dynamic Array
top = 5
Dynamic Array
top = 5
insert 2
Dynamic Array
top = 6
insert 3
Dynamic Array
top = 6
insert 3
!The array is full and
there is no room for a
new item!
Dynamic Array
top = 6
insert 3
So we will create a
new, bigger array
Dynamic Array
top = 6
insert 3
So we will create a
new, bigger array
10
11
9
Dynamic Array
top = 6
insert 3
10
11
10
Dynamic Array
insert 3
6
top = 6
10
11
11
Dynamic Array
insert 3
6
top = 7
6
10
11
12
Dynamic Array
top = 7
6
10
11
13
14
15
16
Linked Lists
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.
18
Linked Lists
info next
info next
info next
list
null
19
20
info next
list
info next
3
info next
8
null
21
22
p = getnode()
info next
list
info next
3
info next
8
null
23
info(p) = 6
info next
list
info next
3
info next
8
null
24
next(p) = list
info next
list
info next
3
info next
8
null
25
list = p
info next
5
info next
3
info next
8
null
26
info next
5
info next
3
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
info next
5
info next
3
info next
8
null
29
p = list
info next
list
p
info next
5
info next
3
info next
8
null
30
list = next(p)
info next
list
info next
3
info next
8
null
31
x = info(p)
info next
x=6
list
info next
3
info next
8
null
32
freenode(p)
info next
x=6
list
info next
3
info next
8
null
33
info next
x=6
list
info next
3
info next
8
null
34
X0
X0
X1
X1
X1
X2
X2
X2
X3
X4
X3
X3
X5
X4
X4
X6
X5
X5
X6
X6
35
list
X0
X1
X2
X3
X4
X5
X6 null
X3
X4
X5
X6 null
list
X0
X1
X2
36
37
list
X0
X1
X2
X3
X4
X5
X6 null
x =X3
p
list
X0
X1
X2
X3
X4
X5
X6 null
38
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
Head
41
Head
42
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
44
LL Application
Polynomial Manipulation
Refer PDF File
45
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
46